File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes Invoke JSP from Servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Invoke JSP from Servlet" Watch "Invoke JSP from Servlet" New topic
Author

Invoke JSP from Servlet

Daena Buyasta
Greenhorn

Joined: Aug 20, 2004
Posts: 7
Wondering is it possible to invoke / pass data to a JSP page from a servlet (without redirects)?

Basically want to use JSP in a similar way to Velocity, from a servlet - something like;



Many thanks.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60046
    
  65

This is a very common thing to do and is the best way to design web apps (controlling data flow in the servlet and letting a JSP render the view) and is called "forwarding".

You obtain a RequestDispatcher from the request via request.getRequestDispatcher( urlOfJsp ) and then call the forward() method of the dispatcher. Any data you want to pass to the JSP can be placed on the request as attributes.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Sonny Gill
Ranch Hand

Joined: Feb 02, 2002
Posts: 1211

You could get a request dispatcher from the ServletContext, and forward the request to it. Check out the Javadocs for RequestDispatcher.
Daena Buyasta
Greenhorn

Joined: Aug 20, 2004
Posts: 7

This is a very common thing to do and is the best way to design web apps (controlling data flow in the servlet and letting a JSP render the view) and is called "forwarding".


So the parameters are passed over HTTP in effect, as query string variables in the URL?

Can you tell me is the browser itself actually forwarded to the JSP page or is it a private HTTP connection between the servlet and JSP page (i.e the servlet acts as a kind of proxy)?

What makes me nervous about forwarding the actual browser is it's an additional page request and may mean extra delay in loaded the final page. Hence was wondering if it's possible to use a JSP page is a simple template, within a Servlet.
Julian Kennedy
Ranch Hand

Joined: Aug 02, 2004
Posts: 823
Hi Daena,

Your question is a reasonable one but I think you'd benefit from reading up about the JSP/servlet lifecycle. Basically there are two different things that can happen: redirection and forwarding. Redirection is where a response is sent back to the browser as you describe and the browser then redirects the request as directed. Forwarding is something that happens at the server-side, typically within the servlet (app) context, without referring back to the browser. You have nothing to fear.

Jules
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60046
    
  65


So the parameters are passed over HTTP in effect, as query string variables in the URL?


No. As I indicated in my reply, they are passed as request attributes. This does not limit you to text as request parameters would.


Can you tell me is the browser itself actually forwarded to the JSP page or is it a private HTTP connection between the servlet and JSP page (i.e the servlet acts as a kind of proxy)?


As Julian posted, the browser takes no part in forwarding.
Daena Buyasta
Greenhorn

Joined: Aug 20, 2004
Posts: 7
Many thanks (and for being patient)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Invoke JSP from Servlet
 
Similar Threads
Is there a way to use JSP in place of JDOM or StringBuffers
Handling dynamic configuration data
jsp-servlet error
Combo box building
Cannot Invoke Servlets