Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

doGet v/s doPost

Posted on 2004-11-07
10
Medium Priority
?
797 Views
Last Modified: 2010-05-18
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
0
Comment
Question by:jaggernat
  • 5
  • 3
  • 2
10 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 12519861
>>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
 
LVL 92

Expert Comment

by:objects
ID: 12519899
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
 
LVL 10

Author Comment

by:jaggernat
ID: 12519926
yes, all i did was to change get to post
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 92

Expert Comment

by:objects
ID: 12519940
then that sounds fine.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12519947
>>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
 
LVL 10

Author Comment

by:jaggernat
ID: 12520416
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
 
LVL 92

Expert Comment

by:objects
ID: 12520427
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
 
LVL 92

Expert Comment

by:objects
ID: 12520431
the get method includes the parameters in the url of the request,
whereas the post method sends them seperately
0
 
LVL 10

Author Comment

by:jaggernat
ID: 12520488
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
 
LVL 92

Accepted Solution

by:
objects earned 400 total points
ID: 12520663
> 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

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses
Course of the Month15 days, 1 hour left to enroll

577 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question