I'm using JFreeChart to generate a dynamic chart depending on the user input. I have a JSP with some textbox and combobox, the user makes the input and submits it, and the Action process it, generating an image of a chart. I need to display this image on the same JSP as before, below the textbox/combobox.
If I use response.setContentType("image/jpeg"); etc... then I get a page with the image alone. I thought of saving the image to a file and then access it with <img >, but I'm not sure that will work (need to save it to WebContent and I may not be able to access it always?).
Is there a way to somehow cache the image and then access it inside the JSP through an <img> or something? Maybe JFreeChart has an easy way to do what I want?
If it matters, I'm also using struts and spring on my webapp.
Thanks in advance.
I've not tried it, but you might look into org.jfree.chart.imagemap and a suitable URL generator from org.jfree.chart.urls. An outline of implementing a PieURLGenerator is illustrated here.
Well, if you generate the image on the server side, you could always just store it in a temp directory using something like a UUID to generate a unique name for it, and concatenating the image file extension on the end of it.
Make sure that the directory the image is generated is accessible on the webserver, and then send the URL path to the image file on the server back to the JSP using ajax (Direct Web Remoting), for display using Javascript.
Just make sure you also have a chron job or service to clear the older files out of the directory now and again.
You should have a servlet that can create the image you want solely from the URL. The URL can then contain an id, which maps back to an object in your program containing raw data in memory. The servlet then generates the image and returns it.
You can then simply set the url of the image in your current web page in Javascript, and it should be loaded.
This is because JSP's are character oriented which do not lend well to binary data so you need to have a servlet do it.
Related
I'm a java developer using Tapestry5. I'm using a jQuery plugin to manage my file uploads via ajax.
I'm looking for the best approach to temporarily saving the attachments until the actual page has been saved. My current work flow consist of a user uploading / removing a file with either the ajax upload box or an ajax remove link positioned next to the file like gmail attachments. I do not want to commit the file to the database until after the file has been uploaded and the save action within the form has been clicked.
I'm currently uploading the files and saving them to the user session until the save action within the form has been clicked. This works perfectly fine providing only one tab is in use, however I'm not sure that is the best approach.
Does anybody have any additional suggestions?
I've had to do something somewhat similar, but, with a slightly different stack (ASP.NET MVC).
The way I solved the problem was by generating a GUID on the page load and stuffing it inside of a hidden input. Your data store could be whatever you want (session, database, disk, etc - just be sure to include the GUID as part of your key). When the user saves the form just be sure to include your hidden input data and then you will have a unique way of identifying the files that were part of that instance.
What you are doing so far seems right, and you are also right that you could run into trouble if the user attempts to use your web app within multiple tabs. What you could do is :
Obtain a unique key before any file is uploaded with an ajax call. (note: as user #Ek0nomik suggests, this could also be included in a hidden input property when generating the page)
Whenever a file is uploaded, make sure to also submit this key.
When the form is saved, the key is submitted, you then know which files should be committed in the database.
I know the how to show th image on jsp file. But never thought how it works? I mean Does image get rendered with html page right at the time when jsp page
is evaluated to html content by webserver and transferred to browser as bytes along with other html or it does not happen this way.
I did discuss with my collegues but they were not sure too. One of them told me that when you request any JSP page from server, jsp page is evaluated
to corresponding html content and images are not rendered at this point of time.So when browser gets this html page and see the tag like below,browser
makes separate call to server to for each image.Is that correct?If yes ,if there are 50 images on jsp page, will 50 request go to server to download the
image. He also mentioned not only images but javascript also included in JSP this way only?
I am not sure when and how the image included in jsp page is requested? Could not get this fact cleared
thru googling too. T
src="getImage.jsp"
The question came in my mind because on change of some value in dropdown, i want to to change the image . I thought i could do it
on client side. But if go by the approach mentioned in the last, looks like image has to be downloaded from server first.
It's very hard to tell what you're asking. If you mean, does an image you include in your JSP page via an img tag (e.g., <img src="/path/to/image.jpg">) somehow get "baked into" your JSP page when it's compiled into a servlet by your JSP container, the answer is no. The browser will request the JSP page, get back HTML et. al., and then request the image.
If you want to change images based on a dropdown I'd suggest using javascript.
First load all the images in javascript objects (on loading of the page)
then in the onchange event of the dropdown change the image.
googling for "preloading images javascript" should provide ample examples
returning an image from a jsp file is also possible
Write code in your jsp that writes the byte stream of an image to the jsp writer, make sure you set the mime type correctly.
Images are downloaded exactly as all other resources do, and get rendered by your browser.
If you ask if 50 instances of the same image in a single page will be downloaded 50 times, the answer depends on the HTTP caching policy headers for the particular image resource - if they allow a resource to be cached, it will get cached by your browser and will be downloaded over the wire only once.
One possible way of storing images in a database is as a stream of bits. This works as a storage mechanism, but I can't figure out how to embed this data into a webpage as an image once I extract it to fulfill a client request.
I'm working with Servlets in Java. Can anyone give me some guidance?
You probably want to write a servlet that reads the BLOB from the database and copies directly into HttpServletRequest.getOutputStream(), remembering to set the content type to the appropriate format (image/png for example).
The database id or key or whatever can be encoded in the path (/image/foo), or passed as a query parameter (/image?id=foo). This path is what you use in your <img> tag.
This is probably best written in actual Java, rather than JSP or similar presentation technologies.
Let me share what I am doing with image files and dynamic HTML generation and why - I believe this is a non-standard approach, feel free to comment or use such a system if it works for you. :)
I have several parameterized html, style sheets and image files that go into the html jarred up and saved as [clientid].jar file on the server side. The UI is applet based. At a lean time, the applet requests the file (and associated logic) from the servlet, after due authentication. The servlet wraps up the entire jar file as byte array, encapsulates its contents (from predefined directory in the unix FS) and the business logic (from the database) in a FileXfer object and sends it out on an ObjectOutputStream. The applet extracts the bytes and saves the jar as a tmp file in tmpdir, with a deleteOnExit flag.
During execution, when the html is required to be displayed, the applet extracts the necessary files from the archive and saves them in the same directory, filtering the html as required by the business logic. Image files (jpeg, png, etc) are not filtered. All such files are deleteOnExit, so there is no footprint once the application exits. Next it opens the html with a browser tab, and everything I need to display is there in the right format. The applet has the logic of file extraction - e.g., do not extract "logo.png" if it was extracted 15 seconds back to display another piece of HTML, etc.
The advantages I see are:
I get an automatic compression of the bytes I need to transfer from the server to the client, speeding up the transfer by about 3x, (jar uses zip-compression)
The client (applet) picks up the load of filtering the html, thereby relieving the server of the same job
No blob storage of image files on the DB (I read somewhere that blobs are not exactly efficient for DB operations)
The html can be edited independently using standard img tags assuming the image file is in $cwd
[clientid].jar file content is not included in the jar containing the applet class, allowing the applet to load and start up faster.
TIA for your comments, - M.S.
I need to show the first slide of a PowerPoint presentation in my jsf app. It must look something like that:
user upload the .ppt file to jsf app
the app take the first slide and converts it to Flash
user sees the converted slide in the separate (not as part of the another page)
I suppose that I couldn't use any external converters because the user of the system will not have them when he uses our system.
Also I'm thinkinig about Flash, not HTML, because the presentation can be dynamic.
Any ideas? Can I do this task or it's impossible and I need to think in another way (maybe restriction for end users - to save presentation as png, but I think that my boss won't like this decision).
Maybe I need to look at .xslt format, maybe it would help?
suppose that I couldn't use any external converters because the user of the system will not have them when he uses our system.
You can truly use external tools since your Java/JSF code runs at the webserver, not at the webclient (webbrowser). All the webbrowser get is just the Java/JSF-generated HTML/CSS/JS code. Open a page in webbrowser, rightclick and View Source and see it yourself. You just install the external tool at the webserver and execute it there.
To upload a file in JSF, you'll need to grab a 3rd party component library since the standard implementation doesn't have an upload component, for example Tomahawk's t:inputFileUpload or the one of whatever component library you're currently already using.
To convert PPT to Flash, execute the appropriate action using external tool in the managed bean action method and store the Flash file somewhere in the local disk file system of the webserver. You can store it in the public webcontent so that it's directly accessible by URL, but those files will be lost whenever you redeploy the webapp. If this shouldn't happen, then store it outside the public webcontent.
I don't have hands on experience with PPT-Flash converters, so I can't recommend a specific one, but Google learns me that there's pretty a lot of choice. There seems to be a Java solution of iSpring.
To display the Flash file, use the HTML <object> element which points to the URL of the Flash file. If the Flash file is stored outside the public webcontent, then you'll need to create a Servlet which gets an InputStream of the file from the local disk file system and writes it to the OutputStream of the response along a correct set of HTTP headers and then let the URL of the <object> element point to that instead.
I have a java applet that creates a JPEG file. I want to pass that file to a Javascript where it can display and print it. The only way I can think of doing this is to save the jpeg to a temporary storage area on the user's computer and then pass the path of the file to the javascript which picks it up and displays it. This raises a two questions:
Where should the applet store the file. If you suggest the temporary internet files folder, then how do I find that path to that folder?
Is there a better way to do this? Can I pass the JPEG directly from java to javascript without first writing out to a disk?
Thank you in advance for your help.
To store file on users's machine your applete should be signed, and user should give necessary permissions to your applet (through special dialog window which is shown automatically).
Read this article about modifying DOM from applet
Another approach is to save your image on the server (pass it from your applet to the server) and then reload page (or use Ajax, but in this case you probably have to make ajax calls every few seconds to check if the image is available on the server).
Can't you just have an applet that displays the picture and prints it?
I don't think it'd be possible to do this in IE before IE8 (and it's wimpy even in IE8), but in other browsers your applet could make the image data available to Javascript (please don't say, "a Javascript"; it's like saying, "a FORTRAN" or "a Java") and then from Javascript you could create an <img> tag with a "data URI". See this reference: http://en.wikipedia.org/wiki/Data_URI_scheme