Creating files under tomcat - java

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

Related

How to get data from XML feed URL in Java

There is a betting exchange website which offer their data in XML from the following link:
http://odds.smarkets.com/oddsfeed.xml
I would like to access this link to retrieve the latest data (in java). Previously I have had to download the (very large) file and add it to my project and get the data from there. What is the best way to achieve this without having to download the file every time I want to access the data?
I plan on storing the returned data into a database.
Thanks
Well this seems to be very tricky question .I would suggest you to create a simple web service application[Client/server architecture] to get the contents from this url. You can use REST to call this url. But what contents you need to read depends on the functionality that you want to achieve.You need to write your custom logic to read the data.Here in you will be acting as client and the url would be your service.
You can refer following link
https://community.atlassian.com/t5/Confluence-questions/Access-page-content-via-URL/qaq-p/163060

File upload with Ajax - not getting complete fileName

It is quite a common question but I can't find an answer to it
I have a simple HTML with an input text box (type=file) and a submit button. On clicking the submit button, I call a js function where I try to get the complete path of the file
var data = $('#fileName').val();
the issue is I am not getting complete file path of the file I am uploading. I know due to security reasons chrome gives me a C:\fakePath\filename and firefox gives me only the fileName. But in case I need a complete path what shall I do?
PS: Further I will make an ajax call and give that file path to the back-end which needs it to read that file using FileReader
You cannot get the complete path! there is no way to do that!! Even though you are on an intranet and you have enough permissions.
A workaround for this is to have a textarea and ask the user to enter the complete path of the file.
In short you can't have the full name of a file once is loaded on server side, you will just have the file name and its content in a raw byte array (among other attributes). This is not a Java thing nor other server side technologies issue, is related to browser implementation (but it looks that IE6 may contain a flaw about this).
Not directly related to your question but caught my attention
PS: Further I will make an ajax call and give that file path to the back-end which needs it to read that file using FileReader
Usually, you can't handle a file upload using ajax because it can lead to security holes. Still, there are some browsers (like Chrome and Firefox) that allows you to send a file using XMLHttpRequest but that isn't allowed on some browsers (like IE8-) so you have to use an iframe in order to make the file ajax uploading work.
In order to avoid handling all these problems, I would advice you to use a third-party js library that handles the ajax file upload. An example is blueimp jQuery file upload that also has Java server side examples (DISCLAIMER: I do not work in this project nor I'm associated with blueimp in any way). Note that using this plugin requires that you have a mid knowledge on HTML/JavaScript/jQuery/Java Server Side so if you're a starter it may take you some time to make it work, but once it does is pretty good.
I dont know which technology you are using.. but you can always get file name once it is uploaded on server (Using php or .net )
your steps to upload should be like below:
1) Upload file to the server (e.z. /uploadedFiles/...filename
2) Create a method which will fetch file name from the uploaded path
3) simply insert file name in to the database (this will give you flexibility to change folder name of uploaded docs in future if required)
Generally filenames are not stored as it is . to avoid name conflict in future. So it is a advisable to always rename your filename by adding minutes & seconds after itsname.
If any doubts do ask.
Hope it helps.
Browsers block the filepath access on javascript for securit reasons.
The behavior makes sense, because the server doesn't have to know where the user stores the file on his computer, it is irrelevant to the upload process.

JSP: save a file on client without storing it on server

In my jsp someone wants to export a query result to csv. Can't use frameworks. At the moment, here is the process:
press button, go to servlet
prepare data for csv
make csv and save to server
back to jsp and let them download the fresh-made file from an anchor tag.
Thing is, I' don't want to create this file on server, as I have to dispose it afterwards, but I still want to give the user the "save as" window. So, is there a way, putting for example an OutputStream object in session, to achieve this result?
Thank you all!
A servlet can generate any type of content. So, when you click the button to run the servlet, simply have the servlet write the file back to the client at that time. You'll need to set the Content-type header to "text/csv" (and making sure that you set encoding properly). You don't need to set the Content-Length header; the browser can deal with that.
When the servlet returns data to the browser, the user will be prompted to save the file or open it with an application.
Yes. You don't need to save the file. Just hold it in the session and send it in-memory to the OutputStream. I would trash it after a given time or after delivery in order to save memory.
Set the content type and the content disposition appropriately. This will mean that the browser interprets the output correctly and prompts your user to save it or launch the appropriate application.
See this SO answer for more details.

Storing HTML file rendered by JSP

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

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.

Categories

Resources