Solved

Stream custom image from servlet via jsp call

Posted on 2003-11-02
10
745 Views
Last Modified: 2010-08-05
I have a jsp that calls a servlet to display an image created on the fly.  The image display works fine when the call to the servlet is made via a jsp:forward, but does not display when I do a jsp:include.  I have additionaly functionality in the jsp page that I would rather not write out from the servlet, so that's the reason for the jsp->servlet call.

Again, I'd like to use <jsp:include page="/servlet/TestServlet" />

The relevant code in "TestServlet" is as follows:

    public void doGet(HttpServletRequest req, HttpServletResponse res)
      throws IOException, ServletException
    {
      res.setContentType("image/jpeg");
      
      ServletOutputStream out = res.getOutputStream();
      BufferedOutputStream bout = new BufferedOutputStream(out);

               /* do some code here */

                    JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(bout);
      JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bi);
      param.setQuality(50f, true);
      encoder.encode(bi, param);

                bout.close();
                out.close();
       }

Thanks for your help.

Anyone here ever use ImageJ API to do on-the-fly image generation?  Additional question and points.
0
Comment
Question by:sapientconceptions
  • 4
  • 2
  • 2
  • +2
10 Comments
 
LVL 15

Expert Comment

by:jimmack
ID: 9667896
I think I'm right in saying that if you do a jsp:forward, then the servlet sets the content type for the response.  If you include, the content type stays the same as the first page.

No.  I haven't used ImageJ ;-)
0
 
LVL 3

Expert Comment

by:applekanna
ID: 9668062
cant we access the request dispathcer in the JSP page between <%%>

Just a thought, if you want me to elaborate do mail again.

Cheers!
0
 

Author Comment

by:sapientconceptions
ID: 9668235
applekanna please elaborate.
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 3

Expert Comment

by:applekanna
ID: 9668262
<%

    ServletContext servletContext = getServletContext();
    RequestDispatcher requestDispatcher = servletContext.getRequestDispatcher("/servlet/TestServlet");
    requestDispatcher.include(request,response);

   //add ur code to do what you want in the JSP
 
%>
0
 
LVL 27

Accepted Solution

by:
rrz earned 250 total points
ID: 9671939
><jsp:include page="/servlet/TestServlet" />
Why not use ?
<img src="/servlet/TestServlet"  width="xxx" height="yyy"/>
0
 
LVL 14

Expert Comment

by:kennethxu
ID: 9672848
rrz is right!
DON'T ACCEPT THIS COMMENT AS ANSWER
0
 

Author Comment

by:sapientconceptions
ID: 9673759
Thanks for the help everyone.  

RRZ, I understand this is the proper way to do it.  The problem is, I can't get anything to display with <img src=.../> or <jsp:include ..../> when calling a servlet.  Finally I took a look at the log files and found an error.  Google turns up next to nothing to the fix (with the exception of unrecognized naming conventions for including .txt or anything other than .jsp or .html/.htm).  

Ma2003-11-03 14:57:45 StandardWrapperValve[jsp]: Servlet.service() for servlet jsp threw exception
org.apache.jasper.JasperException
      at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:254)
      at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:295)
      at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:241)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:247)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:193)
      at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:256)
      at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
      at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
      at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
      at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
      at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
      at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
      at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
      at org.apache.catalina.core.StandardContext.invoke(StandardContext.java:2416)
      at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:180)
      at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
      at org.apache.catalina.valves.ErrorDispatcherValve.invoke(ErrorDispatcherValve.java:171)
      at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:641)
      at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:172)
      at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:641)
      at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
      at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
      at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:174)
      at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
      at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
      at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
      at org.apache.coyote.tomcat4.CoyoteAdapter.service(CoyoteAdapter.java:223)
      at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:601)
      at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:392)
      at org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java:565)
      at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:619)
      at java.lang.Thread.run(Thread.java:536)
----- Root Cause -----
java.lang.IllegalStateException
      at org.apache.jasper.runtime.ServletResponseWrapperInclude.getOutputStream(ServletResponseWrapperInclude.java:109)
      at StationaryPreview.sendPage(Unknown Source)
      at StationaryPreview.doGet(Unknown Source)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:740)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
      at org.apache.catalina.servlets.InvokerServlet.serveRequest(InvokerServlet.java:466)
      at org.apache.catalina.servlets.InvokerServlet.doGet(InvokerServlet.java:180)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:740)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
      at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:684)
      at org.apache.catalina.core.ApplicationDispatcher.doInclude(ApplicationDispatcher.java:575)
      at org.apache.catalina.core.ApplicationDispatcher.include(ApplicationDispatcher.java:498)
      at org.apache.jasper.runtime.JspRuntimeLibrary.include(JspRuntimeLibrary.java:822)
      at org.apache.jsp.verify_jsp._jspService(verify_jsp.java:117)
      at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:137)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
      at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:210)
      at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:295)
      at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:241)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:247)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:193)
      at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:256)
      at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
      at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
      at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
      at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
      at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
      at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
      at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
      at org.apache.catalina.core.StandardContext.invoke(StandardContext.java:2416)
      at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:180)
      at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
      at org.apache.catalina.valves.ErrorDispatcherValve.invoke(ErrorDispatcherValve.java:171)
      at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:641)
      at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:172)
      at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:641)
      at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
      at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
      at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:174)
      at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
      at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
      at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
      at org.apache.coyote.tomcat4.CoyoteAdapter.service(CoyoteAdapter.java:223)
      at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:601)
      at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:392)
      at org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java:565)
      at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:619)
      at java.lang.Thread.run(Thread.java:536)ybe someone here knows what causes this error.  The entire stack trace is below.
0
 

Author Comment

by:sapientconceptions
ID: 9675142
ok, I went back and played with this a little more.  Made some changes ( i dunno what) but it seems I'm making progress on this...now I need a little more help (I'll increase the points and split them between RRZ and whomever else can help).

Using the <img src="/servlet/TestServlet" width="XXX" height="YYY" /> example that rrz recommended, I get a page with a large x (missing image).  After checking the log (see error below), I see that I'm having a problem getting the servlet class loaded.  I added these few lines to my web.xml file:

  <servlet>
      <servlet-name>TestServlet</servlet-name>
      <servlet-class>com.examples.services.TestServlet</servlet-class>
  </servlet>

  <servlet-mapping>
      <servlet-name>TestServlet</servlet-name>
      <url-pattern>/servlet/TestServlet</url-pattern>
  </servlet-mapping>


2003-11-03 19:18:43 StandardWrapper[:org.apache.catalina.INVOKER.StationaryPreview]: Marking servlet org.apache.catalina.INVOKER.TestServlet as unavailable
2003-11-03 19:18:43 invoker: Cannot allocate servlet instance for path /servlet/TestServlet
javax.servlet.ServletException: Wrapper cannot find servlet class TestServlet or a class it depends on
      at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:891)
      at org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:668)
      at org.apache.catalina.servlets.InvokerServlet.serveRequest(InvokerServlet.java:416)
      at org.apache.catalina.servlets.InvokerServlet.doGet(InvokerServlet.java:180)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:740)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:247)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:193)
      at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:256)
      at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
      at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
      at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
      at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
      at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
      at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
      at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
      at org.apache.catalina.core.StandardContext.invoke(StandardContext.java:2416)
...etc....etc...
----- Root Cause -----
java.lang.ClassNotFoundException: TestServlet
      at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1444)
      at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1289)
      at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:885)
      at org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:668)
      at org.apache.catalina.servlets.InvokerServlet.serveRequest(InvokerServlet.java:416)
      at org.apache.catalina.servlets.InvokerServlet.doGet(InvokerServlet.java:180)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:740)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
0
 

Author Comment

by:sapientconceptions
ID: 9675197
nevermind.  Added my web app name to the call and it worked.
0
 
LVL 27

Expert Comment

by:rrz
ID: 9675642
><url-pattern>/servlet/TestServlet</url-pattern>
If, you are using mapping, then you could just use
<url-pattern>/TestServlet</url-pattern>  
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

If you thought ransomware was bad, think again! Doxware has the potential to be even more damaging.
Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question