aspose file tools*
The moose likes Servlets and the fly likes Which Method Process Forward Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Which Method Process Forward" Watch "Which Method Process Forward" New topic
Author

Which Method Process Forward

Alec Lee
Ranch Hand

Joined: Jan 28, 2004
Posts: 569
If I forward a request via a RequestDispatcher to another servlet, which method (doGet, doPost, etc) would end up called in the new servlet?

Another related scenario is if I use <%@ page errorPage="MyServlet" %> in my JSP which causes an exception and MyServlet is a Servlet (rather than JSP), which method of my MyServlet is called? I think neither doGet nor doPost is a logical choice because there is no Http client and the actual client is my JSP page throwing an exception!
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Originally posted by Alec Lee:
If I forward a request via a RequestDispatcher to another servlet, which method (doGet, doPost, etc) would end up called in the new servlet?


When a servlet receives a request it first handles it in it's service method.
Service then calls the getMethod object of the request to find out which type of request this is and calls the appropriate method.
Becuase the same request object is used in both servlets, the servlet forwarded to will handle the request with the same method that the forward-er did.

By the way: The same principals apply to your error page question.
If you are going to use a servlet as your error page, you should implement both doPost and doGet. The simplest way to do this is to have doGet call doPost or vice verca.

In other words:
If it was a POST request it will be handled in doPost.
If it was a GET request, it will be handled in doGet.
[ September 27, 2005: Message edited by: Ben Souther ]

Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Which Method Process Forward