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.StackOverflowErr or
-------------------------
but, when i replaced doGet(request,response); with doPost(request,response);
the error dissapeared.
any reason why?
thanks
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.StackOverflowErr
-------------------------
but, when i replaced doGet(request,response); with doPost(request,response);
the error dissapeared.
any reason why?
thanks
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.
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.
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
I can't think of a reason offhand why that in itself would cure a stack overflow
ASKER
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
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
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
whereas the post method sends them seperately
ASKER
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.HttpSer vlet;
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().getRea lPath("/") +"WEB-INF" + File.separator +"files"+ File.separator;
RequestHandler.setStringFi leBase(dir ectory);
//String realpath=getServletContext ().getReal Path("/");
//System.out.println("real path" +realpath);
requestHandlerFactory = RequestHandlerFactory.getI nstance();
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
try
{
BaseRequest baseObject =(BaseRequest) readSerializedObject(reque st);
RequestHandler handler =(RequestHandler) requestHandlerFactory.getH andler(bas eObject.ge tRequestTy pe());
handler.setHttpObjects(req uest,respo nse);
handler.respond(baseObject );
}
catch(ClassNotFoundExcepti on 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(httpServ letRequest .getInputS tream());
Object readObject = ois.readObject();
return readObject;
}
}
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.HttpSer
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().getRea
RequestHandler.setStringFi
//String realpath=getServletContext
//System.out.println("real
requestHandlerFactory = RequestHandlerFactory.getI
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
try
{
BaseRequest baseObject =(BaseRequest) readSerializedObject(reque
RequestHandler handler =(RequestHandler) requestHandlerFactory.getH
handler.setHttpObjects(req
handler.respond(baseObject
}
catch(ClassNotFoundExcepti
{
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(httpServ
Object readObject = ois.readObject();
return readObject;
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Are you effectively saying all you did was change the letters 'Get' to 'Post'?