aspose file tools*
The moose likes Servlets and the fly likes Write an Image From Servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Write an Image From Servlet" Watch "Write an Image From Servlet" New topic
Author

Write an Image From Servlet

Steve Dyke
Ranch Hand

Joined: Nov 16, 2004
Posts: 1466
    
    1
I am trying to use the following to write an image that I can use in my JSP. I do not get any errors but I am not getting a file either, at least not on I can find.

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61433
    
  67

Are you serving an image, or forwarding to a JSP? You can't have it both ways.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Steve Dyke
Ranch Hand

Joined: Nov 16, 2004
Posts: 1466
    
    1
I found in my local root directory.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61433
    
  67

Your answer seems to be to a question much different than the one I asked.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

I'd also recommend a couple of things:

- Don't write to your machine's root directory: pick a more reasonable location.
- Restructure the code to keep similar things near each other; it makes understanding and following the code much easier.
- Don't use the default IDE-supplied names for variables: "arg0" and "arg1" are meaningless.
- Refactor so that methods do only one thing (in general) and do it completely: this makes testing and comprehension much easier.
Steve Dyke
Ranch Hand

Joined: Nov 16, 2004
Posts: 1466
    
    1
Bear Bibeault wrote:Your answer seems to be to a question much different than the one I asked.


I posted this answer be for I saw your response. I have changed the file path to the directory where the app lives.

I don't understand your question I guess. Should I use a class to write the image instead of the servlet?

Also when I open the JSP in preview from IDE the image dispalys ok(unc path). But when ran through application server the path is changed to url path and fails.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61433
    
  67

No my question is whether you want to serve an HTML page (from a JSP) or an image. You can't do both from the same servlet.

krishna Karthikk
Ranch Hand

Joined: Mar 16, 2010
Posts: 92
You cant do two things from one servlet. I mean the servlet must serve the data (text) and image. This is not possible.

I used to follow this method, I dont know how far it is appreciable, but it works fine for me. If this is wrong please anyone tell me that I am wrong.


Save all the images in your application, in some directory. If the user is uploading the images to the server those images must be saved in the application itself. So you have to work on this.

Then while sending the response use an image tag which points to the save path.

Have a nice day


There is no rule that we all should know everything. Lets learn few things of everything here.
Steve Dyke
Ranch Hand

Joined: Nov 16, 2004
Posts: 1466
    
    1
Bear Bibeault wrote:No my question is whether you want to serve an HTML page (from a JSP) or an image. You can't do both from the same servlet.



My desire is to create a JFree chart on the fly then instead of pushing it out to a pdf file as I normally do I want to display it on a web page of my app.

One more related question. Is there any way to do this with an interactive chart like Excel functionality?
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

krishna Karthikk wrote:Save all the images in your application, in some directory. If the user is uploading the images to the server those images must be saved in the application itself.

I recommend against saving non-application files in the application. Besides that it might not work (for example, if you're deploying a war file), you also run the risk of those files being deleted during a re-deploy. Better to save them outside the application.
Steve Dyke
Ranch Hand

Joined: Nov 16, 2004
Posts: 1466
    
    1
David Newton wrote:
krishna Karthikk wrote:Save all the images in your application, in some directory. If the user is uploading the images to the server those images must be saved in the application itself.

I recommend against saving non-application files in the application. Besides that it might not work (for example, if you're deploying a war file), you also run the risk of those files being deleted during a re-deploy. Better to save them outside the application.


I have changed where the app save the Image files to a shared http folder. I have in working except, when the page initially displays I type in a scope parameter click link and DIV tag is loaded with JSP displaying the image, good so far. Now if I change the scope and click the link the image does not change and will not change until I stop the app and re-start it. I was under the impression that myDivTag.load() refreshed the JSP including the link used in the img tag.

Here is my js that does all the work:



Here is the servlet that creates the image:



Here is the servlet that loads the DIV tag JSP:



Here is the JSP that displays the image:

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61433
    
  67

The best practice way to approach this is not to worry about the image at all in the request that generates the JSP, and to have the image tag reference a servlet that streams the image. No files necessary.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Write an Image From Servlet