Recipe7.11.Displaying a File from the Server


Recipe 7.11. Displaying a File from the Server

Problem

You need to display the contents of a file on your server's filesystem that isn't part of your web application.

Solution

Use a servlet, similar to the one shown in Example 7-13, to read the file from the filesystem and write the file contents to the HTTP response.

Example 7-13. File viewer servlet
package com.oreilly.strutsckbk.ch07; import java.io.File; import java.io.FileReader; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class FileViewerServlet extends HttpServlet {          protected void doGet(HttpServletRequest request,              HttpServletResponse response)             throws ServletException, IOException {         doPost(request, response);     }     protected void doPost(HttpServletRequest request,              HttpServletResponse response)             throws ServletException, IOException {         String fileName = (String) request.getAttribute("fileName");         fileName = "temp.txt";         FileReader in = null;         PrintWriter out = null;         response.setContentType("text/plain");         File dir = (File) getServletContext( ).getAttribute("javax.                     servlet.context.tempdir");         File f = new File(dir, "test.tmp");         try {             // Get an input stream on the form file             in = new FileReader(f);             // Get an output stream for the response              out = response.getWriter( );                      // Write from the input stream to the output stream             char[] buffer = new char[512];             int chars = 0;             while ((chars = in.read(buffer)) != -1) {                 out.write(buffer, 0, chars);             }         }         finally {             if (out != null) out.close( );             if (in != null) in.close( );              }             } }

Discussion

This servlet reads a file from the filesystem and writes it out to the response; it's written to handle a file uploaded using the Solution from Recipe 7.10. The file is read from the servlet's temp directory; however, the location of the file could be any place accessible to the server, even a database.

The servlet shown in Example 7-13 can be used for any Java web application and not just a Struts application.


Here are the primary steps to render content from a servlet, regardless of where the file is located:

  1. Determine the file to retrieve.

  2. Set the HTTP response content type to the correct MIME type for the file.

  3. Acquire a java.io.Reader on the file.

  4. Acquire a java.io.Writer on the response.

  5. In a buffered fashion, read the data from the Reader and write to the Writer.

  6. Ensure the Reader and Writer are always closed on completion.

See Also

Jason Hunter's classic text, Java Servlet Programming (O'Reilly), is the bible of servlet programming. This text has loads more information on the details and nuances of rendering content in this fashion.

Recipe 6.5 shows a good technique for integrating a servlet like this into a Struts application.



    Jakarta Struts Cookbook
    Jakarta Struts Cookbook
    ISBN: 059600771X
    EAN: 2147483647
    Year: 2005
    Pages: 200

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net