This week's book giveaway is in the General Computing forum.
We're giving away four copies of Arduino in Action and have Martin Evans, Joshua Noble, and Jordan Hochenbaum on-line!
See this thread for details.
The moose likes JSP and the fly likes Servlet output into JSP template Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » JSP
Reply Bookmark "Servlet output into JSP template" Watch "Servlet output into JSP template" New topic
Author

Servlet output into JSP template

Kevin P Smith
Ranch Hand

Joined: Feb 18, 2005
Posts: 362
I hope this can be done, else I'n gonna hve to rethink things here...

I have a reporting tool which does quite a complicated search (SQL), so rather than have all the workings of the search in a messy JSP, I've dumped it all into a Servlet, which the uses PrintWriter to output the results.

What I really want is for the results to be outputted(is that a word?) to an existing JSP template page.

So it would be great if there was some sort of JSP <% input Servlet results here function %> rather than using a PrintWriter to output the entire page.

Any offers?

Anyone understand a word I just said?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 56547
    
  14

That' a common thing to do. Processing is performed in a Servlet, the data is attached to the request as a scoped variable (or variables) using the setAttribute() method, and a request dispatcher is used to forward to a JSP page where the data is used to render the view.


[Smart Questions] [JSP FAQ] [Books by Bear] [Bear's FrontMan] [About Bear]
Stefan Evans
Bartender

Joined: Jul 06, 2005
Posts: 1008
alternatively you can just <jsp:include> the servlet onto a JSP page.
Kevin P Smith
Ranch Hand

Joined: Feb 18, 2005
Posts: 362
Cheers for that guys, but how do I actually get the data to the JSP?

I've tried:-
String sCounter = request.getAttribute("counter"); but this causes the following error: Type mismatch: cannot convert from Object to String


So I tried:-
String sCounter = (String)request.getAttribute("counter"); but this causes my Servlet to crash out, displaying a blank browser creen (no errors reported in logs)
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 56547
    
  14

Search the logs for the exception causing the "crash out".
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Servlet output into JSP template
 
Similar Threads
JSPs, Servlets and iFrames
Is there a way to use JSP in place of JDOM or StringBuffers
Submitting a table in a form
creating a template jsp page
HttpURLConnection onSubmit problem.