Link to home
Start Free TrialLog in
Avatar of jaggernat
jaggernat

asked on

doGet v/s doPost

what exactly is the difference between

  doGet(request,response);
and
   doPost(request,response);

i had a servlet program with doGet(request,respnse), on  execution, it gave me an error called :
-----------------------
root cause
java.lang.StackOverflowError
-------------------------
but, when i replaced   doGet(request,response); with  doPost(request,response);
the error dissapeared.

any reason why?

thanks
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

>>but, when i replaced   doGet(request,response); with  doPost(request,response);


Are you effectively saying all you did was change the letters 'Get' to 'Post'?
doGet handles get requests, doPost handles post requests.

if you want to handle both the same then have doGet() call doPost() (as it appears you have done)

The stack overflow would be caused by a method calling itself.
Avatar of jaggernat
jaggernat

ASKER

yes, all i did was to change get to post
then that sounds fine.
>>yes, all i did was to change get to post

I can't think of a reason offhand why that in itself would cure a stack overflow
what is exactly meant by doPost() in servlets?
I am little confused. As i was reading comments from One expert, he said that doPost() is like posting information to the server(secure information like credit cards..ect).

If we are posting information to the server , why is it written in the servlet??

and as i understand, doGet() is getting information from the server..

any advice appreciated.

thanks
See my earlier comment, doGet is for handling get requests ie. using get method, and post is for post requests

eg.

<form method="get" action="MyServlet">

when this is submitted the doGet() will be called

<form method="post" action="MyServlet">

when this is submitted the doPost() will be called

the get method includes the parameters in the url of the request,
whereas the post method sends them seperately
ok,,i got it objects.
now for instance , i have written an application which has front end in swing. The objective of this front end is to communicate with the server and access the files in the server.

now , this is the servelt which is accessed by swing front-end. This servlet contains doPost() which has the main functionality. It also has doGet() which contains doPost(request,response);

do you have any reason why doPost() and doGet() are both used in this servlet.



package FileExchange.servlets;

import javax.servlet.http.HttpServlet;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;
import FileExchange.*;

public class FileExchangeServlet extends HttpServlet
{
      private RequestHandlerFactory requestHandlerFactory = null;
      public void init() throws ServletException
      {
            String directory = getServletContext().getRealPath("/")+"WEB-INF" + File.separator +"files"+ File.separator;
            RequestHandler.setStringFileBase(directory);
                //String realpath=getServletContext().getRealPath("/");
                //System.out.println("real path" +realpath);

            requestHandlerFactory = RequestHandlerFactory.getInstance();
      }
      public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
      {
            try
            {
                  BaseRequest baseObject =(BaseRequest) readSerializedObject(request);
                  RequestHandler handler =(RequestHandler) requestHandlerFactory.getHandler(baseObject.getRequestType());
                  handler.setHttpObjects(request,response);

                  handler.respond(baseObject);
            }
            catch(ClassNotFoundException cnfe)
            {
                  cnfe.printStackTrace();
            }
            catch (IOException ioe)
            {
                  ioe.printStackTrace();
            }
      }
      public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
      {
            doPost(request,response);
        }

 private Object readSerializedObject (HttpServletRequest httpServletRequest) throws IOException, ClassNotFoundException
      {
       ObjectInputStream ois = new ObjectInputStream(httpServletRequest.getInputStream());
       Object readObject = ois.readObject();
       return readObject;
      }
}
ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia 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