Link to home
Start Free TrialLog in
Avatar of frankieDub
frankieDub

asked on

Combining doGet() & doPost()?

I think this is the right place to ask this question.
I'm taking in a value to a servlet and sending out a value from a servlet. I know how to use the required doget and dopost method. However I don't know how to combine them.

probelm -->

public void doPut(etc..

{
DataInputStream dis = new DataInputStream(request.getInputStream());
String person= dis.readUTF();


do an sql query using this value person......

and then with the result -   send it back to the client in the following method..

public void doGet(etc..
{

PrintWriter out = response.getWriter();
   out.println(value);

___________________

The problem I get is that the result or value I get from the sql  query in the doPost method query is not recognised when I try and use it in the doGet method body.

How do I get around this scenario?
Avatar of Mick Barry
Mick Barry
Flag of Australia image

just have doGet() and doPost() call the same method to handle the request.
SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
public void doGet(...
{
   handleRequest(...
}

public void doPost(...
{
   handleRequest(...
}

public void handleRequest(...
{
   // process request here
}
public void doPut(HttpServletRequest req, HttpServletResponse res)
{
   // use the response from here
}
Avatar of frankieDub
frankieDub

ASKER

Thanks for the replies:

But I'm still confused, because don't you require the doPost to take in the request and doget to send it back out, surely another method can't do the processing?

Sorry, guess I need a more explicit example
No you just need the references to the request and response
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
The request will come in through doGet or doPost and you will service your doPut with the references to the request and response, having passed them to doPut.
>    handleRequest(reg, res);

that should of course be:

   handleRequest(req, res);

:)

Is this the right idea? Would you mind taking a quick look at my code? btw you can ignore the lack of prepared statements and other correct practice issues - i'll get around to them later. Thanks ;-)


public class test extends HttpServlet {

public void handleRequest(HttpServletRequest req, HttpServletResponse res)throws IOException, ServletException
{
   

DataInputStream dis = new DataInputStream(req.getInputStream());

String person = dis.readUTF();

Connection con = null;
   


      try
{

      Class.forName(this.driverName).newInstance();
      con = DriverManager.getConnection(this.connURL);
      Statement statement = con.createStatement();


ResultSet rs = statement.executeQuery("select name from users where phone = ('"+person+"')");
     
while(rs.next())
    {

   name = rs.getString("name");
    }

PrintWriter out = res.getWriter();
   out.println(name);

 
      }

    catch (Exception e)
    {
     return (e.toString());
    }
    finally
    {
      try
      {
        if (con != null)
          con.close();  //try to close the db connection
      }
      catch (SQLException sqle)
      {
        return (sqle.toString());
      }
    }
   

}



public void doPost(HttpServletRequest req,
HttpServletResponse res)
throws IOException, ServletException
{

handleRequest(req, res);
}

public void doGet(HttpServletRequest req,
HttpServletResponse res)
throws IOException, ServletException
{
handleRequest(req, res);

}


}
That's the right pattern
How are you calling this servlet btw?
from a midlet
yes, but I missed that you were using the requests input stream.
You can't do that with a get, only a post.

looks like you servlet can only handle post requests.
how are you passing the person when using get?

perhaps you should pass person as a request parameter instead.
String person = request.getParameter("person");
I'm wondering why not

String person = req.getParameter("name");

?
I pass person to the client as follows...


ResultSet rs = statement.executeQuery("select name from users where phone = ('"+person+"')");
     
while(rs.next())
    {

   name = rs.getString("name");
    }

PrintWriter out = res.getWriter();
   out.println(name);

seemed to work when I was testing passing data to the client
I was talking about how you pass the data to the server.
but if I use getparameters how do i read in the vlaue from the midlet? Surely i still have to use String person = dis.readUTF();?
what you are doing is fine, it will just only work with POST.

If you want it to also work with GET then pass the person as a paramatere:

http://server/servlet/GetPersonServlet?person=objects
I pass data using DataOutputStream
You can read the parameter via getParameter. Here's an example of calling a servlet from a midlet:

http://forums.devarticles.com/t6220/sdc81d1813948f8629ad46a37f069f252.html
>  Surely i still have to use String person = dis.readUTF();?

no you use:

String person = req.getParameter("person");
> I pass data using DataOutputStream

Thats fine, but as I said above it will only work with POST.
Depends on whether you want to also support GET.
ok, so if i take out the doGet method I will be able to raed in data and send it back out?
thanks objects & CEHJ!
If you only need to support your midlet, you may as well. This article (better) suggests using POST for comms:

http://developers.sun.com/techtopics/mobility/midp/ttips/clientserv/
8-)