Solved

Stream custom image from servlet via jsp call

Posted on 2003-11-02
10
743 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Jquery - parsing the results returned from webmethod 1 108
sortaSum challenge java 17 171
How to Make JSP Pages Responsive ? details are below 3 585
maven j2ee examles 2 49
This article lists the top 5 free OST to PST Converter Tools. These tools save a lot of time for users when they want to convert OST to PST after their exchange server is no longer available or some other critical issue with exchange server or impor…
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

910 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now