how to implement this page by jsp?

H

homecurr

Hi, could anyone tell me how this page
(http://finance.yahoo.com/q?s=msft) is implemented? I mean how to load
the data dynamicalLy from the server and put in the right location in
the page. Can I do it with Servlet or jsp? How can I put in jsp a
picture generated by the server at run time?

Thanks,

John from CA
 
C

Christian Hauser

Hi, could anyone tell me how this page
(http://finance.yahoo.com/q?s=msft) is implemented? I mean how to load
the data dynamicalLy from the server and put in the right location in
the page. Can I do it with Servlet or jsp? How can I put in jsp a
picture generated by the server at run time?

Hi,

I don't know how this page is implemented (and I don't even care). This
page only shows the latest stock info which is nothing more than dynamic
data. Of course you can do that with Servlet/JSP technology. That's what
Servlets are meant for.

How to create a picture at runtime? All I can see is a created PNG File
showing a chart. That's no problem to create dynamically if you use a
library that creates charts. Of course you could create such a library
yourself, but then you have to understand the PNG or GIF or whatever
format you want to use quite well.

If you know about Servlets and JSPs you can create such a page yourself.
The problem might be to get the quotes...

Regards,
Christian
 
S

Stanimir Stamenkov

/Christian Hauser/:
How to create a picture at runtime? All I can see is a created PNG File
showing a chart. That's no problem to create dynamically if you use a
library that creates charts. Of course you could create such a library
yourself, but then you have to understand the PNG or GIF or whatever
format you want to use quite well.

The OP may use the ImageIO (javax.imageio) classes introduced in
Java 1.4. As for drawing the charts themselves one may use the
standard graphics libraries (java.awt, Java2D):

BufferedImage img = ... // the chart image
Graphics2D g = img.createGraphics();
g.draw...
OutputStream out = ... // the servlet output stream
ImageIO.write(img, "png", out);

There's an issue with using the graphics drawing methods on servers
without graphics environment which is explained here:

"Headless Support"
<http://java.sun.com/j2se/1.4.2/docs/guide/awt/AWTChanges.html#headless>

More solutions:

"Loading images without X server"
<http://www.geocities.com/marcoschmidt.geo/java-image-faq.html#x>
 
H

homecurr

I think there is no image file generated at the server side. What
happened was that:
1) A Image java class was generated. With all graphics in it.
2) A stream was generated.
3) The stream was sent to the client and rendered.

In the book "Core servlet and jsp", there is a chapter about how to
generated a gif image with servlet. The idea is to set the content
type to "image/gif". I think it works only if you display the image
but nothing else. If an image is just a part of a page, it won't work.

Could anyone help?

Thanks,

John from CA
 
S

Stanimir Stamenkov

/[email protected]/:
I think there is no image file generated at the server side. What
happened was that:

If you have had paid more attention to my example you would find no
file (image file on the file system) is created, just what you
describe below:
1) A Image java class was generated. With all graphics in it.

int width = 120, height = 160;
BufferedImage img =
new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
Graphics2D g = img.createGraphics();
g.setColor(Color.WHITE);
g.fillRect(0, 0, width, height);
g.setColor(Color.BLACK);
g.drawRect(0, 0, width, height);
2) A stream was generated.
3) The stream was sent to the client and rendered.

OutputStream out = response.getOutputStream();
ImageIO.write(img, "png", out); /* see the API for the options */
g.dispose(); /* free up resources */

The ImageIO.write() method generates the image stream and sends it
to the specified output stream.
In the book "Core servlet and jsp", there is a chapter about how to
generated a gif image with servlet. The idea is to set the content
type to "image/gif". I think it works only if you display the image
but nothing else. If an image is just a part of a page, it won't work.

Yeah, you should set the corresponding response headers before
sending the data, but that's a separate issue:

resonse.setContentType("image/png"); /* if you generate PNG
* stream as I've used
* in my example */
Could anyone help?

Basically you should create a separate servlet to serve the image
charts. So you could get a "chart.jsp" which serves the HTML:

<html>
....
<img src="chart_image?whateverparam=neededvalue" ...>
....
</html>

and servlet mapped to "chart_image" URI which determines what
exactly to render from the supplied request params (in the URL query
part), for example.

Hope this helps,
 
R

Roedy Green

I think it works only if you display the image
but nothing else. If an image is just a part of a page, it won't work.

but what you can do is send them a page with a REFERENCE to a gif with
some unique name. Then the browser will automatically immediately fire
off a get of that GIF which your http server can handle, or you can
field it, so that you can delete the image contents as soon as it is
sent.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top