Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Displaying java Image object on a jsp page

Posted on 2006-11-20
13
Medium Priority
?
290 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 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
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 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

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

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

As much as Microsoft wants to kill off PST file support, just as they tried to do with public folders, there are still times when it is useful or downright necessary to export Exchange mailboxes to PST files. Thankfully, it is still possible to e…
This month, Experts Exchange sat down with resident SQL expert, Jim Horn, for an in-depth look into the makings of a successful career in SQL.
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

636 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