tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Rino Kadijk <r...@kadijk.net>
Subject Re: jsp and servlet to present zip file for download
Date Sat, 19 Apr 2008 11:11:26 GMT
Once you called:

RequestDispatcher dispatcher =
getServletContext().getRequestDispatcher("/servlet/ZipServlet");

You are not allowed to modify the (headers of the) response anymore. So 
after that call, you are not allowed to modify the outputstream.


So this call is illegal after dispatching.

response.setContentType("application/zip");
        response.setHeader("Content-disposition", "attachment;
filename=selectedCases.zip");


Hope this helpes.

Rino.

Kimberly Begley schreef:
> Hi,
> I'm stumped and was hoping someone might be able to point me in the right
> direction.
> I have an html page form that contains a series of checkboxes and radio
> buttons the form directs to a jsp page that processes the user's selections
> to determine which files to zip up for the user to download. I have written
> a servlet to do the zipping up of subdirectories according to the user's
> selections however I think that I am not calling it correctly. When the user
> hits the submit button - the resulting page is blank and the local_host log
> says this:
>
>  org.apache.catalina.core.StandardWrapperValve invoke
> SEVERE: Servlet.service() for servlet jsp threw exception
> java.lang.IllegalStateException: getOutputStream() has already been called
> for this response
>
>
> The excerpt from the jsp file where the zip servlet is called is pasted
> below along with the zipservlet - any suggestions?
> Thanks!
>
> from jsp:
> request.setAttribute("tcases",cases);
>     request.setAttribute("path",testdir);
>     RequestDispatcher dispatcher =
> getServletContext().getRequestDispatcher("/servlet/ZipServlet");
>     dispatcher.include(request,response);
>
> zipservlet.java:
> public class ZipServlet extends HttpServlet {
>
>     /**
>      * Processes requests for both HTTP <code>GET</code> and
> <code>POST</code> methods.
>      * @param request servlet request
>      * @param response servlet response
>      */
>     protected void processRequest(HttpServletRequest request,
> HttpServletResponse response)
>             throws ServletException, IOException {
>
>         response.setContentType("application/zip");
>         response.setHeader("Content-disposition", "attachment;
> filename=selectedCases.zip");
>
>         try {
>
>             ZipOutputStream zipOutputStream = new
> ZipOutputStream(response.getOutputStream());
>         Vector<String> cases = findCases(request);
>         String testpath = findPath(request);
>
>         for(Iterator it = cases.iterator();it.hasNext();) {
>                 File tcase = new File(testpath + it.next());
>
>                 addFilesToZip(tcase, File.separator, zipOutputStream);
>
>         }
>             zipOutputStream.flush();
>             zipOutputStream.close();
>         } catch (Exception e) {
>         } finally {
>         }
>     }
>     protected Vector<String> findCases(HttpServletRequest request)
>        throws IOException
>     {
>
>     Vector<String> cases = (Vector<String>)request.getAttribute("tcases");
>
>         return cases;
>     }
>     protected String findPath(HttpServletRequest request)
>        throws IOException
>     {
>
>     String path = request.getParameter("path");
>
>         return path;
>     }
>
>     private void addFilesToZip(File path, String pathInZip, ZipOutputStream
> zipOutputStream) {
>
>         if (path.exists()) {
>             File[] files = path.listFiles();
>             for (int i = 0; i < files.length; i++) {
>                 if (files[i].isDirectory()) {
>                     addFilesToZip(files[i], pathInZip + files[i].getName() +
> File.separator, zipOutputStream);
>                 } else {
>                     try {
>                         FileInputStream in = new FileInputStream(files[i]);
>
>
>                         zipOutputStream.putNextEntry(new ZipEntry(pathInZip
> + files[i].getName()));
>
>                         byte buf[] = new byte[2048];
>                         int len;
>                         while ((len = in.read(buf)) > 0) {
>                             zipOutputStream.write(buf, 0, len);
>                         }
>                         zipOutputStream.closeEntry();
>                         in.close();
>
>                     } catch (IOException e) {
>                     }
>                 }
>             }
>         }
>     }
>
>
>     /**
>      * Handles the HTTP <code>GET</code> method.
>      * @param request servlet request
>      * @param response servlet response
>      */
>     protected void doGet(HttpServletRequest request, HttpServletResponse
> response)
>             throws ServletException, IOException {
>         processRequest(request, response);
>     }
>
>     /**
>      * Handles the HTTP <code>POST</code> method.
>      * @param request servlet request
>      * @param response servlet response
>      */
>     protected void doPost(HttpServletRequest request, HttpServletResponse
> response)
>             throws ServletException, IOException {
>         processRequest(request, response);
>     }
>
>     /**
>      * Returns a short description of the servlet.
>      */
>     public String getServletInfo() {
>         return "Short description";
>     }
>
> }
>
>   

---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Mime
View raw message