Solved

Displaying java Image object on a jsp page

Posted on 2006-11-20
13
285 Views
Last Modified: 2008-02-01
The application I am working on needs to have capability of displaying dynamically generated images.

So an action class generates an image as a java Image object. How can I get it to be displayed in the browser window?

Thanks,
Larisa.
0
Comment
Question by:lorlova05
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
  • 2
  • +1
13 Comments
 
LVL 10

Accepted Solution

by:
jaggernat earned 168 total points
ID: 17984295
create a servlet and put your code which generates image in the servlet.
from action class call the servlet, the servlet will generate the image and show on the screen.
0
 
LVL 12

Expert Comment

by:enachemc
ID: 17985933
jaggernat is almost right:

you might consider creating the image in the action that displays the actual page containing the image. Store this image in the session, and diplay the image as having the src attribute pointing to the servlet used to display images. In this servlet you can take the image from the session and stream it to the output stream (not writer). Make sure you also send the appropiate content type header here.
0
 

Author Comment

by:lorlova05
ID: 17986448
jaggernat,

That's almost exactly what I am trying to do. The question is how to make src attribute be processed correctly that is with using servlet. Besides, there might be more than one image on the page; their number depends on the number of images in an afp file they are extracted from. But if src attribute is processed correctly that shouldn't be a problem.

Could you please be more specific on what such src attribute can look like and what I should do to direct it to calling a servlet?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 12

Expert Comment

by:enachemc
ID: 17986487
you can have the src attribute just like any URL: http://server:port/mapping/servlet?param1=1¶m2=2

You can use the parameters to distinguish between your images as you see fit.
0
 

Author Comment

by:lorlova05
ID: 17987214
enachemc, (btw, sorry for mixing nicknames in my previous post :-)

The thing is that the part "http://server:port/..." before "servlet?param1=val1..." is a base url. I need to have src="servlet?param1=val1..." and that base url to be appended while processing the link. That's what is not working . . . Do you think I should add something like

    <servlet-mapping>
        <servlet-name>myservlet</servlet-name>
        <url-pattern>*.doi</url-pattern>
    </servlet-mapping>

    <servlet>
        <servlet-name>myservlet</servlet-name>
        <servlet-class>com....DisplayImageAction</servlet-class>
                .
                .
                .
                .
        <load-on-startup>1</load-on-startup>
    </servlet>

to web.xml file to make src="dynamic/displayimage.doi?id=1" work? Or maybe I am missing something else? I don't have too much experience in setting up this kind of things . . .

Thank you.
0
 
LVL 12

Assisted Solution

by:enachemc
enachemc earned 166 total points
ID: 17987373
of course you should add your servlet mappings (otherwise it will not be seen as a servlet). It does not have to be loaded at startup - this would mean it will be executed at startup - and there is no point in doing so.

Of course you can use relative URLs. As I told you, this would work like any other URL on your site. The URL pattern should be a static name, like

  <servlet-mapping>
    <servlet-name>ImageDisplay</servlet-name>
    <url-pattern>/images/ImageDisplay</url-pattern>
  </servlet-mapping>


  <servlet>
    <servlet-name>ImageDisplay</servlet-name>
    <servlet-class>com......ImageServlet</servlet-class>
     <init-param>
      <param-name>param1</param-name>
      <param-value>value1 used for testing</param-value>
    </init-param>
  </servlet>

The name, class and url pattern can be anything you want.
0
 

Author Comment

by:lorlova05
ID: 17989394
So now I have
<table>
<tr><td>Image:</td><td><img src="/dynamic/ShowImage?id=0" /></td></tr>
....
<tr><td>Image:</td><td><img src="/dynamic/ShowImage?id=8" /></td></tr>
</table>

om my jsp page and

      <servlet>
    <servlet-name>ShowImage</servlet-name>
    <servlet-class>com......ImageServlet</servlet-class>
     <init-param>
      <param-name>param1</param-name>
      <param-value>value1</param-value>
    </init-param>
  </servlet>

  <servlet-mapping>
    <servlet-name>ShowImage</servlet-name>
    <url-pattern>/dynamic/ShowImage</url-pattern>
  </servlet-mapping>

in web.xml.

What method in ImageServlet will be processing src="/dynamic/ShowImage?id=0" ? Do I need to use anything else like forms?

Thanks.
0
 

Author Comment

by:lorlova05
ID: 17989782
So I figured I need to override doGet() method. I did. But this method doesn't get called while loading the jsp page. . .  Any ideas? . . .
0
 
LVL 27

Expert Comment

by:rrz
ID: 17990195
>But this method doesn't get called while loading the jsp page. . .  Any ideas? . . .      
><img src="/dynamic/ShowImage?id=0" />
Maybe try   instead  
<img src="/yourContextName/dynamic/ShowImage?id=0" />
0
 
LVL 27

Assisted Solution

by:rrz
rrz earned 166 total points
ID: 17990219
or on a JSP
<img src="<%=application.getContextPath()%>/dynamic/ShowImage?id=0" />
0
 

Author Comment

by:lorlova05
ID: 17990273
Thanks a lot, rrz@871311. I forgot about the context path . . . :-(
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
squirrelPlay java challenge 40 160
nested if has else if 13 100
TreeSet comparator example 7 109
runtime exception and error differences 4 94
When using a search centre, I'm going to show you how to configure Sharepoint's search to only return results from the current site collection. Very useful when using Office 365 with multiple site collections.
Check out this step-by-step guide for asking an anonymous question on Experts Exchange.
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

739 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