Solved

Displaying java Image object on a jsp page

Posted on 2006-11-20
13
277 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¶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
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 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

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

What does UTC stand for?  “Coordinated Universal Time” – Think of this as the true time on Planet Earth that never changes with the exception of minor leap seconds here and there to account for the changes in the planet's rotation.   What does th…
Cloud-based technologies and services will continue to grow in popularity in 2017 thanks to the simple, scalable and cost-effective solutions they deliver. Here are three areas where cloud adoption is poised to really take off.
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 …
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

912 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

17 Experts available now in Live!

Get 1:1 Help Now