We help IT Professionals succeed at work.

Combining doGet() & doPost()?

frankieDub
frankieDub asked
on
890 Views
Last Modified: 2008-02-01
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?
Comment
Watch Question

Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
just have doGet() and doPost() call the same method to handle the request.
CERTIFIED EXPERT
Top Expert 2016
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
public void doGet(...
{
   handleRequest(...
}

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

public void handleRequest(...
{
   // process request here
}
CERTIFIED EXPERT
Top Expert 2016

Commented:
public void doPut(HttpServletRequest req, HttpServletResponse res)
{
   // use the response from here
}

Author

Commented:
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
CERTIFIED EXPERT
Top Expert 2016

Commented:
No you just need the references to the request and response
Java Developer
CERTIFIED EXPERT
Top Expert 2010
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
CERTIFIED EXPERT
Top Expert 2016

Commented:
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.
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
>    handleRequest(reg, res);

that should of course be:

   handleRequest(req, res);

:)

Author

Commented:

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);

}


}
CERTIFIED EXPERT
Top Expert 2016

Commented:
That's the right pattern
CERTIFIED EXPERT
Top Expert 2016

Commented:
How are you calling this servlet btw?

Author

Commented:
from a midlet
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
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.
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
String person = request.getParameter("person");
CERTIFIED EXPERT
Top Expert 2016

Commented:
I'm wondering why not

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

?

Author

Commented:
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
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
I was talking about how you pass the data to the server.

Author

Commented:
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();?
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
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

Author

Commented:
I pass data using DataOutputStream
CERTIFIED EXPERT
Top Expert 2016

Commented:
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
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
>  Surely i still have to use String person = dis.readUTF();?

no you use:

String person = req.getParameter("person");
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
> 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.

Author

Commented:
ok, so if i take out the doGet method I will be able to raed in data and send it back out?
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
yes

Author

Commented:
thanks objects & CEHJ!
CERTIFIED EXPERT
Top Expert 2016

Commented:
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/
CERTIFIED EXPERT
Top Expert 2016

Commented:
8-)
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.