Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JSP to Display PDF ?

 
Muks Sam
Ranch Hand
Posts: 47
Java MyEclipse IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Friends, I have following code and PDf is not getting displayed, Can somebody help me on priority pls.

============================================================================
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML>
<HEAD>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1" %>
<%@ page import="java.io.*"%>
<%@ page import="java.net.*"%>
<%@ page import="java.util.*"%>
<%@ page import="java.sql.Date"%>
<%@ page import="org.apache.struts.util.MessageResources"%>
<%@ page import="java.text.SimpleDateFormat"%>
<%@ page import="com.dcx.catl.epc.util.*"%>
<%@ page import="javax.servlet.http.HttpSession"%>

<META name="GENERATOR" content="IBM WebSphere Studio">

<TITLE>ShowPdf</TITLE>
</HEAD>
<BODY>
<table class="groupborder" cellspacing="0" cellpadding="0" width="840" border="0">
<tr>
<td>
<table cellspacing="1" cellpadding="0" width="900" border="1">
<tr>
<td class="tableheading">
<%
System.out.println("Starting...." + request.getParameter("pdf").trim());
if(request.getParameter("pdf")!="")
{
String strGraphicsFileName = "";
String strFileName="";

InputStream in = null;

strGraphicsFileName = request.getParameter("pdf").trim();

System.out.println("strGraphicsFileName : " + strGraphicsFileName);

String strURL = "";
if (EpcConstants.getWHERE_AM_I().equalsIgnoreCase("T"))
{
strURL = "ftp://x:x@clcss020.x.com/Output/tsb/html/en_us/dto/pbd2/08/00/22/";
}
if (EpcConstants.getWHERE_AM_I().equalsIgnoreCase("P"))
strURL = "ftp://x:x@clcss020.x.com/Output/tsb/html/en_us/dto/pbd2/08/00/22/";

System.out.println("strGraphicsFullFileName ::: strURL \n" + strURL+strGraphicsFileName);
try
{
URL url = new URL(strURL+strGraphicsFileName);

URLConnection urlc = url.openConnection();
in = urlc.getInputStream();

strFileName = "/EPC Web/EPCWeb/Web Content/jsp/images/" + strGraphicsFileName;

System.out.println("strFileName : " + strFileName);

System.out.println("\n\n\n----------------------------------------");

File parent =null;
File outputFileOld = new File(parent, strFileName);

System.out.println("out File exists " + outputFileOld.exists());

if (outputFileOld.exists())
outputFileOld.delete();
System.out.println("After deleting old file");

File outputFile = new File(parent, strFileName);
FileOutputStream outFile = new FileOutputStream(outputFile);

System.out.println("out File" );
response.setContentType( "application/pdf" );
response.setContentLength((int)outputFile.length());

response.setHeader("Content-Disposition","inline; filename=\""+ strGraphicsFileName +"\"");
//response.setHeader("Cache-Control", "no-cache");

byte[] buf = new byte[8192];

//FileInputStream inStream = new FileInputStream(outputFile);

int c=0;
//while ((c = in.read()) != -1)
outFile.write(c);

while ((c = in.read(buf, 0, buf.length)) > 0)
{
//System.out.println("size:"+c);
outFile.write(buf, 0, c);
}

//inStream.close();
outFile.flush();
outFile.close();

System.out.println("\n\n\n----------------------------------------");
System.out.println("File Found:");
}
catch(IOException e)
{
e.printStackTrace();
System.out.println("File Not Found: "+e);
%>
<center><font color="red" family="verdana" size="2"> <p style="background-color:white">File Not Available !!</p></font></center>
<%
}
catch(Exception me)
{
me.printStackTrace();
System.out.println("Exception in pdf locating "+me);
}
}
else
{
%>
<table border=0 width=600 align=center><tr><td class=columnheader>File not Found. Please check back later.</td><tr></table>
<%
}
%>
</td>
</tr>
</table>
</td>
</tr>
</table>
</BODY>
</HTML>
============================================================================

thanks

Muks
 
Paul Clapham
Sheriff
Posts: 21137
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, if all that Java code were in a servlet then it might have a chance of working. But as it is you're writing out HTML, not PDF. Look at the first two lines of what you're writing out:Even if you strip away everything except the scriptlets, I expect there will still be some whitespace that you let leak into the response. And that will mess up the PDF you send. Use a servlet instead.
 
Muks Sam
Ranch Hand
Posts: 47
Java MyEclipse IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for reply.
I can't use servlet since it is a Struts framework. pls. suggest.
Thanks
Muks
 
Paul Clapham
Sheriff
Posts: 21137
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know Struts. But I would be extremely surprised if it didn't allow you to write servlets.

After one minute of my Google search for "struts servlets" I found this:

http://www.javareference.com/jrexamples/viewexample.jsp?id=103

Maybe it's relevant to your problem.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64973
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You cannot do this in a JSP for exactly the reasons Paul mentioned. JSPs are solely inteneded to render character response data. Anything else, be it PDF, images or any other binary stream, must be emitted with a servlet.

Originally posted by mukul samak:

I can't use servlet since it is a Struts framework.


Hogwash.
 
Stefan Evans
Bartender
Posts: 1752
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are doing it in struts, then do it from your Struts action.
Its the equivalent of a servlet anyway.
Just return null from your action to indicate to struts that you have already sent the response.
And why are you writing it to a FileOutputStream? That would write the file out onto the server. Get the response output stream to send it back to the browser.

( a little bit of struts specific code, but not much)


Cheers,
evnafets
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic