• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Write an Image From Servlet

 
Steve Dyke
Ranch Hand
Posts: 1619
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64715
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you serving an image, or forwarding to a JSP? You can't have it both ways.
 
Steve Dyke
Ranch Hand
Posts: 1619
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I found in my local root directory.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64715
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your answer seems to be to a question much different than the one I asked.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1619
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64715
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Steve Dyke
Ranch Hand
Posts: 1619
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1619
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64715
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic