?
Solved

Displaying java Image object on a jsp page

Posted on 2006-11-20
13
Medium Priority
?
291 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
  • 5
  • 3
  • 2
  • +1
11 Comments
 
LVL 10

Accepted Solution

by:
jaggernat earned 672 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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 664 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 28

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 28

Assisted Solution

by:rrz
rrz earned 664 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

Strategic internal linking is often considered an SEO power technique, especially for content marketing. Do you need to hire an SEO agency to optimize you internal linking? No, this article will help you understand the basics of internal linking and…
In the below post we have mentioned the best hosting type for startups. Also, check out some of the superlative web hosting companies that are proposing affordable web hosting solutions to host your startup website.
Integration Management Part 2
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…

840 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