Re: Serving files using tomcat
I use this code and it works in my app. Their are small differences between how we copy the data to the response output. I don't know for sure, but this may account for why the fragment I posted works. The difference is small, I think it would be worth giving it a try. AS- - Original Message - From: "Steve Vanspall" <[EMAIL PROTECTED]> To: "Tomcat Users List" Sent: Wednesday, May 04, 2005 11:47 AM Subject: Re: Serving files using tomcat Unfortunately that is what I do OutputStream dos = null; FileInputStream fis = null; try { fis = new FileInputStream(rf.getPdf()); response.setContentType("application/pdf"); response.setContentLength((int) rf.getPdf().length()); //response.setHeader(response.) dos = response.getOutputStream(); int read = -1; byte[] bytes = new byte[10]; while((read = fis.read(bytes)) != -1) dos.write(bytes, 0, read); dos.flush(); return mapping.findForward("PDF"); } catch (Exception e) { // TODO Auto-generated catch block if(e instanceof SocketException) return mapping.findForward("reload"); throw new IOException(e.toString()); } finally { if(dos != null) dos.close(); if(fis != null) fis.close(); } Acrobat now loads but the PDF doesn't appear. Probably worth mentioning that I use struts, so I forward to a blank page with the content type set to application/pdf, maybe that is the problem, but not sure what else to do with the return. When I do the same thing with a dynamic image and forward to a page with a jpg content type, the image appears without a problem. Steve - Original Message - From: "Anhony" <[EMAIL PROTECTED]> To: "Tomcat Users List" Sent: Thursday, May 05, 2005 1:02 AM Subject: Re: Serving files using tomcat Greetings, Take a look at the code fragment below. It should serve as a good starting point. I hope this helps. AS- private void processPDFRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, Exception { int bytesCopied = 0; FileInputStream fin = null; OutputStream out = null; String fileAddress = "The fully qualified path to your PDF file"; if( fileAddress == null ) return; int ext = fileAddress.lastIndexOf( '.' ); if( ext != -1 ) { ext = fileAddress.substring( ext+1, fileAddress.length() ).toLowerCase(); if( ext == "pdf" ) response.setContentType("application/pdf"); else "Do whatever you think best to do" } else "Do whatever you think best to do" try { out = response.getOutputStream(); fin = new FileInputStream( fileAddress ); bytesCopied = StreamCopier.copy( fin, out ); } finally { if( fin != null ) fin.close(); if( out != null ) { out.flush(); out.close(); } } } - Original Message - From: "Steve Vanspall" <[EMAIL PROTECTED]> To: "Tomcat User List" Sent: Wednesday, May 04, 2005 9:29 AM Subject: Serving files using tomcat Hi, I have been looking around and haven't found a solution that works basically I have a PDF that gets created dynamically. Now to save memory I have the PDF written to a file rather than a ByteArray. The only way I can be sure that I wont encounter errors creating the file is to use File.createTempFile. The creation goes of ok. And I have checked the file itself and the PDF looks great. How do i now serve this to the user who has requested it. If I try to write it to the response (using the same method I use to creare dynamic image, this works), it just shows up a blank screen. The problem also is, even if it did show the PDF, acrobat, to my understand will read only chunks of the stream and will go pack to get more. Thisis a problem because there is nothing to go back for. So the point, If I can just redirect the browser to a file in the tomcat temp directory (can I do that, will the use have access to that directory), then how do I translate the location of the temp directory to a url that is accesible outside. If not then what other suggestions can people give me. Thanks in advance Steve - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Serving files using tomcat
Greetings, Take a look at the code fragment below. It should serve as a good starting point. I hope this helps. AS- private void processPDFRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, Exception { int bytesCopied = 0; FileInputStream fin = null; OutputStream out = null; String fileAddress = "The fully qualified path to your PDF file"; if( fileAddress == null ) return; int ext = fileAddress.lastIndexOf( '.' ); if( ext != -1 ) { ext = fileAddress.substring( ext+1, fileAddress.length() ).toLowerCase(); if( ext == "pdf" ) response.setContentType("application/pdf"); else "Do whatever you think best to do" } else "Do whatever you think best to do" try { out = response.getOutputStream(); fin = new FileInputStream( fileAddress ); bytesCopied = StreamCopier.copy( fin, out ); } finally { if( fin != null ) fin.close(); if( out != null ) { out.flush(); out.close(); } } } - Original Message - From: "Steve Vanspall" <[EMAIL PROTECTED]> To: "Tomcat User List" Sent: Wednesday, May 04, 2005 9:29 AM Subject: Serving files using tomcat Hi, I have been looking around and haven't found a solution that works basically I have a PDF that gets created dynamically. Now to save memory I have the PDF written to a file rather than a ByteArray. The only way I can be sure that I wont encounter errors creating the file is to use File.createTempFile. The creation goes of ok. And I have checked the file itself and the PDF looks great. How do i now serve this to the user who has requested it. If I try to write it to the response (using the same method I use to creare dynamic image, this works), it just shows up a blank screen. The problem also is, even if it did show the PDF, acrobat, to my understand will read only chunks of the stream and will go pack to get more. Thisis a problem because there is nothing to go back for. So the point, If I can just redirect the browser to a file in the tomcat temp directory (can I do that, will the use have access to that directory), then how do I translate the location of the temp directory to a url that is accesible outside. If not then what other suggestions can people give me. Thanks in advance Steve - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Session lost when switching from https to http in Tomcat 5.
Greetings, Your help is greatly appreciated, I have hade a devil of a time with this. I am glad to know this was not caused by an error in my code. Again, thanks very much for your help. Best Regards, Anthony- - Original Message - From: "Bob Feretich" <[EMAIL PROTECTED]> To: <[EMAIL PROTECTED]>; Sent: Sunday, May 01, 2005 4:48 AM Subject: Re: Session lost when switching from https to http in Tomcat 5. Tomcat (starting with Tomcat 4) stores the JSESSIONID cookie as a "secure" cookie that is tagged for port 443 (or 8443) when the session begins under HTTPS. Browsers are not allowed to send secure cookies under plain HTTP, so your session is lost. For Tomcat 4 or 5 you must start your session under HTTP, then switch to HTTPS to maintain a session across both. Tomcat 3 had a config.xml option to always store JSESSIONID as non-secure. It's a long story. See the mailing list archive for the rants. In the its current state, Tomcat's implementation does not agree with published "Best Practices" and the *proposed* "State Management" standard, but the decision was made to err on the side of security. I have modified Tomcat 4 to permit sessions that span HTTP and HTTPS. The changes are not difficult, but you must implement your own mechanism to prevent session hijacking. Non-secure JSESSIONID cookies create a security hole. The committees are supposed address the security vs. state management issue in the next Servlet Spec. Regards, Bob Feretich I have a servlet/JSP application in which users establish their servlet session using https but conduct the rest of their interactions using http. The session appears not to be preserved between https and http, ie. after switching from back to http the request.getSession(false) call returns null. Can anyone shed light on this for me? Is this expected? Is there a workaround/configuration/setting in Tomcat 5 I might have missed? Thanks Anthony - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Session lost when switching from https to http in Tomcat 5.
I am using Tomcat 5.0.28 Users log into my application from https://xxx.com/login.jsp. When submitted, I check for a valid userID/Password, create a session with getSession(), and then save the userID/Password in a session variable. The validated user is then returned to my main tools page. If the user then selects a link to a non secure page, http://..., I get a return of null when performing getSession( false) when trying to check that the user is valid. Thanks for the help. Anthony - Original Message - From: "Anto Paul" <[EMAIL PROTECTED]> To: "Tomcat Users List" Sent: Thursday, April 28, 2005 10:26 AM Subject: Re: Session lost when switching from https to http in Tomcat 5. On 4/28/05, Anhony <[EMAIL PROTECTED]> wrote: I have a servlet/JSP application in which users establish their servlet session using https but conduct the rest of their interactions using http. The session appears not to be preserved between https and http, ie. after switching from back to http the request.getSession(false) call returns null. Can anyone shed light on this for me? Is this expected? Is there a workaround/configuration/setting in Tomcat 5 I might have missed? Thanks Anthony - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] Please specify version of Tomcat and URLs which you used which caused problems. It is working fine for me on Tomcat 4.1.12,4.1.30,4.1.31. -- rgds Anto Paul - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: How to use servlet filters without modifying webapp
Greetings, Try adding a block to your web.xml. Your JSP container locates your filters thru these sections in the web.xml. I included a small sample block below. processingFilter servletFilters.ProcessingFilter I hope this helps. Anthony- - Original Message - From: "joelsherriff" <[EMAIL PROTECTED]> To: "Tomcat Users List" Sent: Thursday, April 28, 2005 9:36 AM Subject: How to use servlet filters without modifying webapp Hello, I'm experimenting with applying a servlet filter to an existing webapp and I'm getting a ClassCastException upon startup. Can I do this without modifying the webapp source and adding my filter in there? If so, what else could be causing this? I'm not sure where it looks for the filter .class file but I put it in the webapp's WEB-INF/classes directory - I guess it finds it since I'm getting this error. The filter really does nothing, I'm just trying to get A filter in place before making it more complicated. - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Session lost when switching from https to http in Tomcat 5.
I have a servlet/JSP application in which users establish their servlet session using https but conduct the rest of their interactions using http. The session appears not to be preserved between https and http, ie. after switching from back to http the request.getSession(false) call returns null. Can anyone shed light on this for me? Is this expected? Is there a workaround/configuration/setting in Tomcat 5 I might have missed? Thanks Anthony - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]