• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Servlet to open a pdf

 
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello, I am trying to create a servlet that will open a pdf document based on the passing the MIME header. I have the servlet written that is suppose to do this. I am not sure how to dynamically tell the servlet the path of the file that I want to open.
At the moment the path for the file(s) is set in the action (using Struts 2). Any help would be appreciated.
 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It amounts to this


I think if you want it to come up in the browser you use



Here are a couple URL's that may be helpful

http://www.javaworld.com/javaworld/javatips/jw-javatip94.html

http://www.onjava.com/pub/a/onjava/2003/06/18/dynamic_files.html

Finally, I've found the IText library to be very helpful for working with and generating PDFs from Java
http://www.lowagie.com/iText/

Hope this helps.
[ October 31, 2007: Message edited by: John Melton ]
 
Katie Doody
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you. Here is the code I have:

public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException

{

ServletOutputStream out = response.getOutputStream ();
//---------------------------------------------------------------
// Set the output data's mime type
//---------------------------------------------------------------
response.setContentType( "application/pdf" ); // MIME type for pdf doc
//---------------------------------------------------------------
// create an input stream from fileURL
//---------------------------------------------------------------
String fileURL = "C:/jboss-4.0.4.GA/server/default/bill.pdf";
//------------------------------------------------------------
// Content-disposition header - don't open in browser and
// set the "Save As..." filename.
// *There is reportedly a bug in IE4.0 which ignores this...
//------------------------------------------------------------
response.setHeader("Content-Disposition", "attachment; filename=\"bill.pdf\"");
//-----------------------------------------------------------------
// PROXY_HOST and PROXY_PORT should be your proxy host and port
// that will let you go through the firewall without authentication.
// Otherwise set the system properties and use URLConnection.getInputStream().
//-----------------------------------------------------------------
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
URL url = new URL( "http", CWAAppConstants.PROXY_HOST,
Integer.parseInt(CWAAppConstants.PROXY_PORT), fileURL );
// Use Buffered Stream for reading/writing.
bis = new BufferedInputStream(url.openStream());
bos = new BufferedOutputStream(out);
byte[] buff = new byte[2048];
int bytesRead;
// Simple read/write loop.
while(-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
bos.write(buff, 0, bytesRead);
}
} catch(final MalformedURLException e) {
System.out.println ( "MalformedURLException." );
throw e;
} catch(final IOException e) {
System.out.println ( "IOException." );
throw e;
} finally {
if (bis != null)
bis.close();
if (bos != null)
bos.close();
}
}

}

I am just not sure how to reference the servlet. Does it replace the action. Do I need to add an entry to the web.xml? I am not sure how to use it. I think if I was doing it new it would be easier but I am changing existing code and the jsp calls an action.
 
John Melton
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My assumption would be you'd probably want to go with an actual servlet, but I'm sure you could figure out a way to do it in an action. If you go with the servlet, yes, you'd then have to configure it in the web.xml. A google search can get you the code for that - search on something like "configure a servlet in web.xml java". I've only ever used raw servlets w/ PDFs - never tried it w/ struts. Maybe someone else could offer help if you want to go that route.
Hope this helps.
 
Katie Doody
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think I can add the servlet to the web.xml fairly easily but how does the jsp know to use the servlet? Thanks. Do you do an onclick event or something? Thank you very much by the way.
 
John Melton
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do you mean how do you go to a servlet when you click on the link? If so, you configure a path when you setup a servlet in the web.xml. In the jsp, your link is to that url - the servlet container then takes it from there.

For example, you could have this in your web.xml



and this in your jsp

 
Katie Doody
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Right now this page has a url that pass parameters to an action class to dynamically create a path to the specific pdf it is trying to open. Here is some sample code from the page:

<s:url id="url" action="ShowBillPDFfromPop">
<s aram name="pdfParam"><%=((CWAAccountInfoTO)x.get(0)).getAccountNbr()%>
_<%=((CWAAccountInfoTO)x.get(i)).getBillHistPrintedDate()%>,<%=request.getRealPath("/")%>;Yes</s aram>
</s:url>

I guess I am not getting the big picture of how to pass the path to the servlet depending on the link selected and how the servlet interacts with the jsp page. I mean, i am thinking it would work very similar to an action class but would not have an execute method.
[ October 31, 2007: Message edited by: Ulf Dittmer ]
 
Katie Doody
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am terrible at trying to say what I want to say but say that I call the generic servlet from the action class(servlet) using struts 2. I create a new instance of my generic openPdf servlet and call the doPost method. This was the path I was starting to go down. I then tried to pass the request and the response to the doPost method and it keeps asking my for another ")"? I don't really think this is the problem. Here is my call:

CWAOpenPdf openPDF = new CWAOpenPdf();
openPDF.doPost(HttpServletRequest request,
HttpServletResponse response);

I think since I am using struts 2, it does not like the way that I am passing the request and response.

I also just want to do it the cleanest way I can and I am not sure what that is.

Thanks for working with me. I know I do not explain myself very well.
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Never, ever, instantiate a servlet. The servlet container does that, and your code should stay clear. You also can't call the doGet/doPost method directly - again, the servlet container does that. The reason the compiler complains about the line

is that you left the class designations (HttpServletRequest and ...Response) in there. But removing them -see above- doesn't solve the problem.

I'm not all that familiar with Struts actions, but in general the way to call a servlet from within a servlet is to use a RequestDispatcher and then invoke its forward method.
 
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Katie Doody:
openPDF.doPost(HttpServletRequest request,
HttpServletResponse response);

That's nothing to do with servlets, or PDF, or Struts. It's just basic beginner Java. When you declare a method, you have to declare the types of its parameters. Like what you have there. But when you call that method, you don't need to give the types of the parameters. Just the parameters themselves. Like this:(From the Struts point of view what you are doing there is probably a bad idea, too, but I will let people who know something about Struts comment about that.)
 
Katie Doody
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you. My goal is to create the servlet to open the pdf's so that it is independent and can be reused. The problem is that I need to create the file in the destination path before I do the post. I thought of adding the doPost to my action but also thought this was a bad idea.

I guess I am having problems trying to integrate the struts 2 action and the reusable servlet.

If anyone with struts 2 experience could help that would be great.
 
John Melton
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First of all a method call would look like ....doPost(request,response) -- without the types HttpServletRequest and HttpServletResponse, but actually with a servlet, doPost gets called (or doGet) just by making a request to the servlet - the servlet container takes care of that.
 
Katie Doody
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Alright, so I made my action ServletRequestAware. I then placed the do post in the action and set the form in the jsp to post to this action. It does not seem to be doing the do post. Any ideas?

Action:
public class CWABillA extends ActionSupport implements ServletRequestAware


Struts.xml:
<action name="ShowBillPDF" class="com.mudnebr.cwa.action.CWABillA" >
<result>/tiles/keepScreen.jsp</result>
</action>

web.xml:
<servlet>
<servlet-name>CWABillA</servlet-name>
<servlet-class>com.mudnebr.cwa.action.CWABillA</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>CWABillA</servlet-name>
<url-pattern>/CWABillA.action</url-pattern>
</servlet-mapping>
jsp:
<s:form action="CWABillA.action" method="post" theme="simple">
 
reply
    Bookmark Topic Watch Topic
  • New Topic