Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Produce image dynamicly by servlet

 
Niu Xiuyuan
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wanna write a counter by servlet,but I don't know how to do.Who can help me?
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here are the basics:

Basically, anything you do to the Graphics context gets output to the client...
You'll want to write text to the graphics context, obviously!
Dave.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13071
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You may find it necessary to set the contentLength in addition to the content type. If so you have to write to a ByteArrayOutputStream first so you can get the size - then output the resulting byte array.
Bill
 
Tracey Currier
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is a really good example for what I have been trying to do the last couple of days. But I am still not getting the image drawn in the applet. I checked the byte array, it is loaded in. My utility class says I don't have a jpeg but I will disregard this because I know I have a byte[]. I convert to an int[] and use:
Image temp_image = createImage(new MemoryImageSource(200, 200,int_arr, 0, 200));
g.drawImage(temp_image,0,0,this);
but still doesn't draw, what am I missing?
Thanks
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13071
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I understand what you are saying, I think the problem is on the Applet end. How are you treating the data stream from the servlet?
If you address the servlet directly from a browser do you get an image?
Bill
 
Tracey Currier
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException{
ServletOutputStream out = res.getOutputStream();
ByteArrayOutputStream bout = new ByteArrayOutputStream();
res.setContentType("image/jpg");
try{

BufferedImage buffImg = new BufferedImage(200,200, BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = buffImg.createGraphics();
//anything you do to the Graphics Context gets written to the BufferedImage
g2.setColor(Color.red);
Rectangle s = new Rectangle(100,100);
g2.fill(s);
g2.dispose();
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(buffImg);
param.setQuality(1.0f,true);
encoder.encode(buffImg,param);


int sizebuf = bout.size();
res.setContentLength(sizebuf);
bout.writeTo(out);
bout.flush();
bout.close();

out.flush();
out.close();
 
Tracey Currier
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just realized that the fill method is fillRect - I will change and try again.
 
Tracey Currier
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
no, fill is ok, it's fill(shape)
I am thinking that I need to add a ColorModel
to the my MemoryImageSource constructor in the
AppletCode???
 
Tracey Currier
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is how I receive the stream in the applet:
InputStream in = con.getInputStream();
ByteArrayOutputStream bstream = new ByteArrayOutputStream();
int c=0;int i =0;
while((c = in.read()) != -1){
bstream.write(c);
}

byt_arr = bstream.toByteArray();
 
Tracey Currier
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, I can't call it up in my browser, I didn't
even realize you could do this with an image, I
thought I had to send it to an applet. Neat (especially if I can get it to work).
 
Tracey Currier
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm using linux op on server and IPlanet web server, I am trying to resolve the awt to X11 graphics environment problem
???
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13071
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you are making things overly complex. Instead of reading a byte array and trying to interpret it, read the stream as if it was coming from a file using the built in image grabber in the Applet class -
getImage( URL url )
where the URL addresses your servlet and includes the parameter to identify the image.
Bill
 
Tracey Currier
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what would the parameter be?
I tried "image/gif" or "gif"
(changed from jpeg)
and "http"
Thanks!
 
Tracey Currier
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried the class name of the servlet if this
parameter is "name" like "name of the servlet" when you are mapping the servlet, no go yet
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13071
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I mean like this where the parameter tells the ImgServ which image to serve up.
urlstring = "http:/yourhost/servlet/ImgServ?Image=NameOfImage" ;
In other words what you would have in an <IMG tag in a plain HTML page.
ImgServ does a request.getParameter("Image"); and has to set the content type and content size.
Bill
 
Prashanth menon
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Tracey Currier:
No, I can't call it up in my browser, I didn't
even realize you could do this with an image, I
thought I had to send it to an applet. Neat (especially if I can get it to work).

Hi I had checked that code. and I could call it up through the browser.
http://localhost/servlet/SampleImgServlet
may be you should check the content type.
you have set it as "image/jpg". I think it should be "image/jpeg". In the former case you may get the file save as dialog.
Prashanth
 
Prashanth menon
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Since the Contenttype for the response has already been set as image, how is it possible to send an HTML. I think we must possibly write image to a filestream and the give the path as attribute for SRC of IMG tag.
Prashanth
 
Tracey Currier
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it turned out to be a problem with the graphics environment, I was working in a "headless"
environment so could not use the awt classes.
java 1.4 has classes that enable some of the
awt classes in a headless environment. we got
the example to work by installing a graphics environment on the server !!!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic