Two Laptop Bag
The moose likes Servlets and the fly likes How to write the content from document file to jsp page Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "How to write the content from document file to jsp page" Watch "How to write the content from document file to jsp page" New topic

How to write the content from document file to jsp page

mahudees waran

Joined: Dec 17, 2006
Posts: 28
hai all,
I have a ms word file in server side. when client requests for that file the content of the file should be displayed in jsp as the same in document file.Because it should only be allowed to read and should be restricted from saving.

I tried by saving the word file as html but the image are not displayed.Also I tried by writing the bytes of the file to response output stream, it's ok for text but image not displayed properly.

In both the case I used text/html as MIME type.

how it should be done ?
Any idea's ?
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1710
setContentType() method sets the content type of the data that is being sent. Iit does not fire any magical translator that would translate the stuffs to the desired format.

response.setContentType("application/msword"); will allow the content of word format to be displayed under word container.

mahudees waran

Joined: Dec 17, 2006
Posts: 28
Yes you are right, that's why I haven't tried application/msword since it opens with word the file could be copied which is not allowed as per the requirement.

My idea is to open it in jsp so it can be prevented from copying by disabling the properties of the window in which it is opened.

Any suggestions?
[ October 17, 2008: Message edited by: mahudees waran ]
Paul Clapham

Joined: Oct 14, 2005
Posts: 19864

If you are going to send a Word document to the client, you cannot prevent the user from saving a copy of it. There are no magic tricks like "open in JSP". So you'll have to review your requirements to correspond with reality.
Peter Johnson

Joined: May 14, 2008
Posts: 5852

should be restricted from saving

Good luck with that. Even if you manage to convert the Word doc into a decent HTML file, once it gets displayed on my browser all I have to do is File | Save Page As.. and I can save the file. And even if you manage to turn off that browser capability, I have several other ways of saving the contents. Now, all of this "saving" is to my PC, not to the server. Similarly, if Word displays a document embedded within the browser, and the user uses the File | Save menu, the document is saved only to the user's PC - the document on the server is not updated.

I bring this up because your post was not clear as to what "saving" you were attempting to prevent - saving to the server or saving to the client. As a general rule, saving to the server is not possible (unless you go to the effort to make that possible, usually by providing an upload capability), and preventing saving to the client is not possible.
[ October 17, 2008: Message edited by: Peter Johnson ]

JBoss In Action
mahudees waran

Joined: Dec 17, 2006
Posts: 28
To be clear the requirement says that the file content should be viewed by the user but the user should not be allowed to save a copy of the content.

I tried by disabling the menubar, right click event's in the browser window in which it is opened.In that part it was ok I was not able to save the content.

The problem is the content displayed is not same as the word file the images in the word file are not displayed porperly and also with some text alignment issues.

Is that will be correct way to convert the word document to image and displaying the image?

I tried and,the problem with this was the image is too large to display the document size is nearly 260kb but image ranges upto 3000kb so I'm unable to show the image.The image is not displayed while the 10kb size image is displayed properly.
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63544

Seems you have two problems:

1) How to prevent the user from saving the file. Answer: you can't. Give it up. It doesn't matter what your requirements say -- they can't be met. If the user can see the info, it can be copied.

2) Images not appearing. This one can be solved. I suggest you stop talking about (1) and concentrate on solving (2).

[Asking smart questions] [About Bear] [Books by Bear]
subject: How to write the content from document file to jsp page
It's not a secret anymore!