This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes JSP and the fly likes Opening pdf file using jsp Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » JSP
Bookmark "Opening pdf file using jsp" Watch "Opening pdf file using jsp" New topic
Author

Opening pdf file using jsp

Ganesh Ravi Kumar
Greenhorn

Joined: Dec 06, 2010
Posts: 17
The problem that im facing is, the below code is working correctly and retrieve the pdf format files correctly and displaying it in the iframe. but it is only working in eclipse internal browser but not in firefox or internet explorer. what will be the reason for this....

Source code:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Browse File</title>
</head>
<body>
<center><h3>File Uploading</h3></center>
<center>
<form name="PDFForm" action="browse.jsp" method="post">
Select PDF : <input type="file" name="txtPDFFile" value="">
<input type="submit" value="View">
</form>
<% String s=request.getParameter("txtPDFFile");
if(s!=null){
%>
<iframe src ="<%= s %>" width="100%" height="700">
<p>Your have to select file to view.</p>
</iframe>
<% } %>
</center>
</body>
</html>
Stefan Evans
Bartender

Joined: Jul 06, 2005
Posts: 1018
I would suggest you view source on the generated html to see what the src link looks like in the different browsers.
Look at that url to determine whether you think it should work or not.

All you seem to be doing is putting the filename straight into the iframe.
If it works in the Eclipse internal browser, then that will be the exception, and not the norm.

The input type="file" control is intended for uploading files to the server. Not opening local files for viewing in the browser.

Ganesh Ravi Kumar
Greenhorn

Joined: Dec 06, 2010
Posts: 17
In Internet Explorer im getting the fully qualified path. Eg: D:\Ganesh\Assign1.pdf. But it only displays the iframe as blank.......it is not displaying the pdf file. But when working with FireFox im getting only the resource name. Eg: Assign1.pdf. thats it. And i have concatenated the resources name with the fully qualified path (Eg: D:\Ganesh\Assign1.pdf) when using firefox and it is returning a alert message like........... "Firefox doesn't know how to open this address, because the protocol (d) isn't associated with any program". And one more thing as you said, input type file only used for browsing.....yes that i know......im passing the path to the iframe with the use of input type submit.
Stefan Evans
Bartender

Joined: Jul 06, 2005
Posts: 1018
And one more thing as you said, input type file only used for browsing


No, I did not say that.
I said that the input type='file' is intended for uploading files to the server.

You seem to be attempting to use it another fashion - choosing a file to display in your browser window/iframe.
This is not a supported operation.

Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

Ganesh Ravi Kumar wrote:im passing the path to the iframe with the use of input type submit.


Let's go back a step. What exactly are you trying to do here? Upload a file from the client to your server? Display in the browser a file which is downloaded from your server? Display in the browser a file which already exists on the client machine? Something else?
Ganesh Ravi Kumar
Greenhorn

Joined: Dec 06, 2010
Posts: 17
exactly........you are right......(Something else?)no more............what i have to do for that..........u have understood clearly about my problem........solution for that???
Ganesh Ravi Kumar
Greenhorn

Joined: Dec 06, 2010
Posts: 17
somebody fix this issue............how to display pdf file using jsp in different browser............
Lester Burnham
Rancher

Joined: Oct 14, 2008
Posts: 1337
Ganesh Ravi Kumar wrote:You have understood clearly about my problem.

Actually, Paul had not - which is why he asked what exactly you're trying to do. Notice how he suggests at least 3 different things you may be trying to do? Unless you tell us which it is, there's not much we can do to help.
Ganesh Ravi Kumar
Greenhorn

Joined: Dec 06, 2010
Posts: 17
im not trying to do three different things.......i just want to display the pdf file in the browser......thats it.......the code that i posted have some logic, im asking why it is not working in mozhilla and ie.......whether i have made any modifications in that code...........
Alex Hurtt
Ranch Hand

Joined: Oct 26, 2010
Posts: 98
If you want to src of the frame to be a file from the local file system you'll need to use the file:/// protocol in front of your src url if you're going to use an absolute URL like that.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61314
    
  66

Do you really want to serve from the file system? You do realize that that means that it will work on your browser an no one else's, right?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

There's certainly some evidence here that Ganesh doesn't realize that the JSP runs on the server and generates HTML which is executed in the browser. That being the case, the scriptlet in the JSP is useless. There's also the possibility that Ganesh doesn't realize that the browser doesn't have direct access to the file system on the server, which ties in to the first possible misunderstanding.

However all we have is an incomplete and incoherent description of the requirements. We don't know whether these PDF files to be displayed in the browser are coming from the client machine or from the server, for a start.

Using a browser to display files from the client machine is impractical and pointless and probably not allowed because of security issues, besides which the browser has no way of identifying files on the client anyway. But that still might be the requirement if it's coming from somebody who doesn't understand the web environment.

On the other hand if the requirement is to display files which come from the server, then what's required is a file download. A JSP is completely the wrong tool to download binary files, what is required is a download servlet.

On the other other hand, there's also the fact that Ganesh has included HTML code which is designed to upload files to the server, which you wouldn't do by accident if you were a beginner. And he mentions another FAQ, which is that IE uploads the whole file path when you do that and other browsers don't. This almost looks like he wants to upload a file path and not to upload the contents of the file, which would be a different misunderstanding. It rather looks like he is trying to upload the path to a file on the client and then generate HTML to display that file in the browser, which would be pretty useless even if it could be done, which it can't. Anyway there's plenty of scope for guessing about what the real requirements are.

So hopefully Ganesh can take this post and take it to whoever the real requirement came from. Perhaps that person could pick out whatever applies to that requirement and clarify things.
Alex Hurtt
Ranch Hand

Joined: Oct 26, 2010
Posts: 98
Well he said he wanted to display a pdf in a browser. What he's doing can work...doesn't matter if it's a jsp generating the HTML or it's a static HTML page. He just has the content of the src attribute wrong. Whether he's trying to display the pdf from the server or really off the local file system...either way he's got the src attribute content wrong. I think he's maybe lacking some foundational understanding about what code gets interpreted/executed where and how the browser resolves resources.
 
GeeCON Prague 2014
 
subject: Opening pdf file using jsp