wood burning stoves
The moose likes JSF and the fly likes Servlet to Backing bean Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » JSF
Bookmark "Servlet to Backing bean" Watch "Servlet to Backing bean" New topic

Servlet to Backing bean

Rohit Boorugu

Joined: Oct 06, 2009
Posts: 14
Hello everyone, I have a servlet in which I send out a PDF document to the client. Along with the PDF the client has a save button(purpose is to save the PDF to database). When the save button is clicked a backing bean is called. Is there a way i could get the PDF document to the backing bean and save it to the database.
Christian Nicoll
Ranch Hand

Joined: Mar 09, 2008
Posts: 106
Hi Rohit,

I'm quite new to Faces, but I think that there are at least two different approaches to get the document in your backing bean.

1) When you create the pdf-document then put the pdf in a folder off your web-appl which is directly accessible (not under WEB-INF). The link to the pdf-document which you show to the user contains the path to your file. I think you can use the <h: outputLink>-Tag with the binding-attribute to bind the url of your pdf-document to your backing bean. In the backing bean you 'll be then able to access the pdf over the url and to store it in your database. When you use this approach then you 've to take care that your pdf-files will be deleted after a while...

2) You can save your pdf as an attribute in the request-scope of your servlet. In the backing-bean you should be able to access the request-scope over the following code:

A problem in this approach can be the performance because you need to store the whole document in your request scope.

Hope it helps.

My SCBCD-Notes - My Hello World Webservice
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 17423

WHATEVER you do, DON'T save files inside your webapp directory. It's a great way to get into trouble. Aside from the fact that a WAR isn't supposed to be writable under the J(2)EE specs, it's a great way to end up losing critical data. Sorry, Christian. I've got major scars because someone did that.

Rohit, I'm not quite sure how you expect this to work. Once you've generated a PDF and sent it to the client, the only way to get that PDF in a database back on the server is either to generate another copy and write it to the database or have the client upload the downloaded copy and have the webapp write the uploaded file to the database.

Backing beans themselves are no problem. They're just ordinary beans of whatever scope you declared them in the faces-config file. The only magic that JSF adds is in its ability to automatically construct and interconnect them for you declararatively.

So if you define a "pdfBean" in session scope in faces-config, you can then use the normal J2EE session bean access methods to use it in a servlet. If you're planning on calling one of the "pdfBean"'s action processors, that's OK, too, as long as the action processor is straight Java and doesn't attempt to access JSF objects like the FacesContext. A FacesContext only exists when JSF is controlling the request/response, so in the case of a servlet or non-JSF JSP, any attempt to get the FacesContext will just return null.

An IDE is no substitute for an Intelligent Developer.
Rohit Boorugu

Joined: Oct 06, 2009
Posts: 14
thank you guys.
I agree. Here's the link: http://aspose.com/file-tools
subject: Servlet to Backing bean
It's not a secret anymore!