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
LVL 10
jaggernatAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CEHJCommented:
>>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'?
0
objectsCommented:
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.
0
jaggernatAuthor Commented:
yes, all i did was to change get to post
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

objectsCommented:
then that sounds fine.
0
CEHJCommented:
>>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
0
jaggernatAuthor Commented:
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
0
objectsCommented:
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

0
objectsCommented:
the get method includes the parameters in the url of the request,
whereas the post method sends them seperately
0
jaggernatAuthor Commented:
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;
      }
}
0
objectsCommented:
> do you have any reason why doPost() and doGet() are both used in this servlet.

as i mentioned earlier, it is so the servlet can handle both get and post requests
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.