Storing HTML file rendered by JSP - java

I need to create a report using Servlet and jsp. My problem is this, there is a page which list the previously generated report. One can click on any of this report from list and view that report again. For this I have to keep the static html file Rendered by the jsp on the server.
How can i obtain this. I don't want to render the jsp file twice as this is time consuming.
Thanks in advance.

You could cache the reports by adding a Filter. The filter can check if an already rendered version is available and return the contents of that file. If no file is present it can write the resulting response to the cache.

Basically, you should write the body of your HTTP response object to a file on your webserver and write some kind of service/servlet to retrieve these files.
I guess the easiest way to do this, is by writing a ServletFilter that intercepts the responses of your JSPs.
The current Oracle Java EE 6 tutorial is rather brief about Filters, but you can read all about them in the previous version : http://docs.oracle.com/javaee/5/tutorial/doc/bnagb.html

Related

Can you change the text content of a JSP file by code (on the fly)

Lets say I have a jsp page in my webapp. The code inside it displays some info.
Can I change the code (text) inside this jsp using a Rest Controller. I want to dynamically replace its code by opening the file and changing its content.
I want to change file content via http request:
"/api/change-jsp?newcontent=sometext&file=example.jsp"
--> The corresponding rest controller now does his job.
Will that work or not ?
Ps: 'newcontent=sometext' contains jsp code (EL)
It won't work — jsps are compiled at runtime by the servlet container when the app starts. The JSP compiler will not expect the jsp to change.
But why change it in the first place? Just make the jsp ask for the information it needs.

Creating files under tomcat

I want to display a file from DataBase (stored as Blob).so for that i want to copy it under tomcat Server after that call the method that shows the file .
So is there a possibility to create a temporary folder in tomcat .
Any help will be appreciated
Many thanks
What you want is a Java Servlet which:
Retrieves the data blob from the database
Streams the data back to the browser as though a file were being returned
Here's an example from over on Java Ranch that I think summarizes the idea pretty well: http://www.coderanch.com/t/291337/JSP/java/Display-database-BLOB-jsp
Just remember that the servlet is tied to a URL, it receives a set of parameters via the query parameters on the end of the URL, and then it will use those to go get the data and return it. Make sure that the MIME type on the returned data is correct because that is the browser's clue what to do with the data streamed to it. That is, to the browser, the response is just a big bunch of data and the MIME type it sees with it helps it determine what action it should take. Should it be saved to a file, displayed, etc.
You can see some discussion of that portion of things in this Stack Overflow question: Help getting image from Servlet to JSP page
There already exists a temporary folder in tomcat.
tomcat/temp
Regards

How to store a copy of complete web page at server side as soon as it is rendered on client browser?

Requirement is to keep a copy of complete web page at server side same as it is rendered on client browser as past records.These records are revisited.
We are trying to store the html of rendered web page. The html is then rendered using resources like javascript, css and image present at server side. These resources keep on changing. Therefore old records are no longer rendered perfectly.
Is there any other way to solve above? We are also thinking converting it into pdf using IText or apache FOP api but they does not consider javascript effect on page while conversion. Is there any APIs available in java to achieve this?
Till now, no approach working perfectly. Please suggest.
Edit:
In summary,requirement is to create a exact copy of rendered web page at server side to store user activities on that page.
wkhtmltopdf should do this quite nicely for you. It will take a URL, and return a pdf.
code.google.com/p/wkhtmltopdf
Example:
wkhtmltopdf http://www.google.com google.pdf
Depending on just how sophisticated your javascript is, and depending on how faithfully you want to capture what the client saw, you may be undertaking an impossible task.
At a high level, you have the following options:
Keep a copy of everything you send to the client
Get the client to return back exactly whatever it has rendered
Build your system in such a way that you can actually fetch all historical versions of the constituent resources if/when you need to reproduce a browser's view.
You can do #1 using JSP filters etc, but it doesn't address issues like the javascript fetching dynamic html content during rendering on the client.
Getting the client to return what they are seeing (#2) is tricky, and bandwidth intensive.
So I would opt for #3. In order to turn a website that renders dynamic content versioned, you have to do several things. First, all datasources need to versioned too. So any queries would need to specify the version. "Version" can be a timestamp or some generation counter that you maintain. If you are taking this approach, you would also need to ensure that any javascript you feed to the client does not fetch external resources directly. Rather, it should ask for any resources from your system. Your system would in turn fetch the external content (or reuse from a cache).
The answer would depend on the server technology being used to write the HTML. Are you using Java/JSPs or Servlets or some sort of an HTTPResponse object to push the HTML/data to the browser?
If only the CSS/JS/HTML are changing, why don't you just take snapshots of your client-side codebase and store them as website versions?
If other data is involved (like XML/JSON) take a snapshot of those and version that as well. Then the snapshot of the client codebase as mentioned above with the contemporary snapshot of the data should together give you the exact rendering of your website as at that point of time.
A very resource-consuming requirement but...
You haven't written what application server you are using and what framework. If you're generating responces in your own code, you can just store it while generating.
Another possibility is to write a filter, that would wrap servlet's OutputStream and log everything that was written to it, you must just assure your filter is on the top of the hierarchy.
Another, very powerfull, easiest to manage and generic solution, however possibly the most resource-consuming: write transparent proxy server staying between user and application server, that would redirect each call to app server and return exact response, additionally saving each request and response.
If you're storing the html page, why not the references to the js, css, and images too?
I don't know what your implementation is now, but you should create a filesystem with all of the html pages and resources, and create references to the locations in a db. You should be backing up the resources in the filesystem every time you change them!
I use this implementation for an image archive. When a client passes us the url of an image we want to be able to go back and check out exactly what the image was at that time they sent it (since it's a url it can change at any time). I have a script that will download the image as soon as we receive the url, store it in the filesystem, and then store the path to the file in the db along with other various details. This is similar to what you need, just a couple more rows in your table for the js, css, images paths.

Extracting JSP page content

I am working on a Mail API module where i have to develop a generic functionality of sending Mails with respect to various functionality in the Appliication.
There is one proposed functionality where the module will expose a method which along with some required parameters will take nane of JSP template.
It expects that this will extract the content of the JSP which will be a well formated mail template and send mail.
Is there any way in JAVA where i can extract the content (HTML) from this JSP page so that i can use that HTML content in to the Mail.
Thanks in advance
You have two paths to go, with the first one being a little shorter:
use new URL("http://site.com/url/to/page.jsp").openConnection(), get the InputStream and read the contents - this will be as if your server sends a request to itself and gets the result
use a Filter and a HttpServletResponseWrapper, and return a custom Writer / OutputStream. Each time something is written to the writer / stream, delegate it to the original object, and also write it somewhere where you can read it from later. This explanation is not sufficient, because this is less likely what you need, but if you are willing to take this path, tell me.
That's, however, not the way this is usually done. You'd better use some templating technology like Freemaker or Velocity for your email templates.
It sounds like you're trying to use JSPs as a templating engine for your email, which is something it wasn't intended to do. There are other technologies out there better suited for what you want, like Velocity and Freemarker.
However, if you're dead-set on using JSP, you have two options :
1) You can use the method described by Bozho to, essentially, connect to your own site and have it generate the content for you
2) You can write the JSP, compile it at compile time, and include the generated servlet file in your email generator and mock the inputs to the Servlet API that the generated JSP servlet will be expecting to extract content from your compiled JSP.

asynchronous file upload with java servlet

Here's I want to do, I want to upload a file that will be processed by a servlet. I would use Apache Commons - File Upload to handle the file to be uploaded.
I've seen the gmail-like AJAX file upload, where there would be a hidden iframe that would later be populated with a javascript to stop showing the upload image or displaying a message that the upload is succesful. However, this uses PHP, where the php file to handle the file upload would include the javascript inside the iframe.
My question is, how would I do this in Java using servlets, without resorting to JSP and imitating the above implementation on PHP. I don't even know if this is possible, so please guide me on a good implementation (without external libraries except for commons fileupload).
Note: I am aware that there are libraries out there that could do this easily, but I first want to know how this happens, how this is possible, and to dirty my hands and learn this.
Edit: Just to add, I would use the streaming API of Apache-Commons FileUpload
It is exactly the same.
The client makes an HTTP request to the server (by submitting a form).
The server responds with some HTML (which links to or embeds some JavaScript).
Switching from PHP to Java is just a drop in replacement. You don't need to change any of the JavaScript. The user guide tells you how to set it up.
http://oreilly.com/pub/a/javascript/2002/02/08/iframe.html is the best idea to file-upload. i done file upload using hidden iframe. Please consult with attached link.

Categories

Resources