• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

How to include servlet response in a JSP?

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
author & internet detective
Posts: 40211
816
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Max Qua
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks,

it is a picture created at run time.
 
Jeanne Boyarsky
author & internet detective
Posts: 40211
816
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Marshal
Posts: 67451
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Max Qua
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Posts: 67451
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
reply
    Bookmark Topic Watch Topic
  • New Topic