Solved

Displaying java Image object on a jsp page

Posted on 2006-11-20
13
275 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
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
 
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&param2=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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

A procedure for exporting installed hotfix details of remote computers using powershell
Use of TCL script on Cisco devices:  - create file and merge it with running configuration to apply configuration changes
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

757 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

22 Experts available now in Live!

Get 1:1 Help Now