• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Invoke JSP from Servlet

 
Daena Buyasta
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64959
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Sonny Gill
Ranch Hand
Posts: 1211
IntelliJ IDE Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could get a request dispatcher from the ServletContext, and forward the request to it. Check out the Javadocs for RequestDispatcher.
 
Daena Buyasta
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 823
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64959
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Many thanks (and for being patient)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic