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 How to include servlet response in a JSP? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "How to include servlet response in a JSP?" Watch "How to include servlet response in a JSP?" New topic
Author

How to include servlet response in a JSP?

Max Qua
Greenhorn

Joined: Sep 27, 2008
Posts: 20
Hi,

I have the following problem. I developed a servlet able to return a string with some information.
I would like to see this information from a JSP page. In other words, I would like to encapsule in the jsp page, the info requested to the servlet.
Is it possible (without using Ajax)? In this case the servlet returns a piece of HTML text or a simple string. If there is a way, could it be possible to use the same mechanism for a JSP to ask a servlet to include a picture in the page?
Many thanks

/Massimo
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30116
    
150

Massimo,
Normally, you would have your servlet forward to a JSP. So rather than having the servlet return the string/html, it would put the String as an attribute in the request. Then the JSP it forwarded to would get the attribute from the request and display it as part of the page. I highly recommend you use this pattern (MVC) rather than complicating things with AJAX.

For the image, is this an image that exists on the filesystem or one that you are creating at runtime?


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Max Qua
Greenhorn

Joined: Sep 27, 2008
Posts: 20
Thanks,

it is a picture created at run time.
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30116
    
150

Originally posted by Max Qua:
it is a picture created at run time.

I'd store it on the file system temporarily and use the same technique. There's likely a better way though.
[ October 11, 2008: Message edited by: Jeanne Boyarsky ]
Max Qua
Greenhorn

Joined: Sep 27, 2008
Posts: 20
Hi,

I tryed with
<div>
<jsp:include page="myservlet" />
</div>

and it works! There is no need to forward requests to the servlet and then to another JSP!

/Max
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60780
    
  65

Yes, that will work but is considered a poor way to construct web apps. It's better to learn accepted best practices and to adopt them.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Max Qua
Greenhorn

Joined: Sep 27, 2008
Posts: 20
In this case the servlet output is a simple string with numerical data that could change. For instance the number of logged users in a particular state.
It is a dynamical information that I would like to embed in a jsp.
I have only a problem in using it. In order to have the data updated after a refresh of the page I should use flush="true", but in this case I got an exception:

Any hint?

My JSP has a form for username and password. The first time everything is ok, the status data is updated, but when I submit the form I got the exception.
Someone could help me to understand what's happening?



Here is the exception:

12-ott-2008 10.08.13 org.apache.catalina.core.StandardWrapperValve invoke
GRAVE: Servlet.service() for servlet jsp threw exception
java.lang.IllegalStateException
at org.apache.catalina.connector.ResponseFacade.sendRedirect(ResponseFacade.java:435)
at org.apache.taglibs.standard.tag.common.core.RedirectSupport.doEndTag(Unknown Source)
at org.apache.jsp.index_jsp._jspx_meth_c_005fredirect_005f0(index_jsp.java:341)
at org.apache.jsp.index_jsp._jspx_meth_c_005fif_005f2(index_jsp.java:273)
at org.apache.jsp.index_jsp._jspService(index_jsp.java:133)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:374)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:337)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:286)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
at java.lang.Thread.run(Unknown Source)
[ October 12, 2008: Message edited by: Max Qua ]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60780
    
  65

This is one of the problems that you can run into when using servlets in an improper manner. Accepted best-practices are set into place because they outline what works and avid pitfalls that can arise through improper use.

The proper way to display dynamic data in a JSP is to have a page controller servlet that gets control first, gathers any data needed on the JSP, and then forwards to the JSP for display using the JSTL and EL.

Please read this article for more information.
 
Consider Paul's rocket mass heater.
 
subject: How to include servlet response in a JSP?
 
Similar Threads
Servlet to JSP
Can you invoke a servlet on the context path page?
is it possible to find out which servlet forwarded request to a JSP page
Can I include a jsp page in servlet
servlet to JSP to HTML to JSP