• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Sony Agrawal
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A client-side redirect causes the browser to issue a GET request; it's something rather different than a server-side forward.
 
Sony Agrawal
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 234
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 234
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sony Agrawal wrote:req.getParameter("option") == "sendRedirect")

String comparison is done using the "equals" method, not using the "==" operator.
 
Sony Agrawal
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for pointing out that mistake Ulf Dittmer, that cleared my doubt
 
Smitesh Shinde
Ranch Hand
Posts: 45
Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic