aspose file tools*
The moose likes Servlets and the fly likes Which kind(POST/GET) of request  is used for JSp Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Which kind(POST/GET) of request  is used for JSp" Watch "Which kind(POST/GET) of request  is used for JSp" New topic
Author

Which kind(POST/GET) of request is used for JSp

Sony Agrawal
Ranch Hand

Joined: Oct 04, 2009
Posts: 143
I have the following method in the servlet.



in Forum.jsp I am doing:


If i use the dispatcher the output in jsp is POST
but if i use sendRedirect the output in jsp is GET.

Could someoone explain the behavior

Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42276
    
  64
A client-side redirect causes the browser to issue a GET request; it's something rather different than a server-side forward.


Ping & DNS - my free Android networking tools app
Sony Agrawal
Ranch Hand

Joined: Oct 04, 2009
Posts: 143
Ulf Dittmer wrote:A client-side redirect causes the browser to issue a GET request; it's something rather different than a server-side forward.

I am sorry to say that i did not understand your post.. could you elaborate please
Brij Garg
Ranch Hand

Joined: Apr 29, 2008
Posts: 234

public void doPost(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException{

req.getRequestDispatcher("Forum.jsp").forward(req,res);
// res.sendRedirect("Forum.jsp"); // PRG pattren
}



In case of RequestDispatcher, client(browser) is not aware about the forward request. Server sent the request to the resource. This is POST request.

In case of sendReDirect, response first goes to the client(browser) and then new request sent to the resource ( in your case Forum.jsp). It is the browser which sends the new request. This is Get Request.
Sony Agrawal
Ranch Hand

Joined: Oct 04, 2009
Posts: 143
I just tried the simple example:

JSP


servlet 1:



Servlet2:The above servlet's control reaches to this servlet



If i have select from the second form i.e the one with POST request the its POST all the way irrespective of the dropdown option selected
If i have select from the FIRST form i.e the one with GET request the its GET all the way irrespective of the dropdown option selected
Please explain
Brij Garg
Ranch Hand

Joined: Apr 29, 2008
Posts: 234

If i have select from the second form i.e the one with POST request the its POST all the way irrespective of the dropdown option selected
If i have select from the FIRST form i.e the one with GET request the its GET all the way irrespective of the dropdown option selected
Please explain


I did not get what you want to ask.

Can you please explain your doubt or can you post the output for the code you posted.?
Sony Agrawal
Ranch Hand

Joined: Oct 04, 2009
Posts: 143
If i select the drop down which does a post(form method=post), then both sendRedirect and dispatcher does POST..
Output:
Post method of the Controller
doPost method is called : POST


If i select the drop down which does a get(form method=get), then both sendRedirect and dispatcher does GET..
output:
Get method of the Controller
doGet method is called : GET

So where is logic of client side and server side ???

Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42276
    
  64
Sony Agrawal wrote:req.getParameter("option") == "sendRedirect")

String comparison is done using the "equals" method, not using the "==" operator.
Sony Agrawal
Ranch Hand

Joined: Oct 04, 2009
Posts: 143
Thanks for pointing out that mistake Ulf Dittmer, that cleared my doubt
Smitesh Shinde
Ranch Hand

Joined: Feb 16, 2008
Posts: 45

Sony,
Whether it is sendRedirect or forward the http method used will be same as of the original request which is being redirected or forwarded.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42276
    
  64
Whether it is sendRedirect or forward the http method used will be same as of the original request which is being redirected or forwarded.

No. After a redirect, the browser always uses GET.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Which kind(POST/GET) of request is used for JSp