• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Invoke JSP from Servlet

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1211
Mac IntelliJ IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 823
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Many thanks (and for being patient)
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic