Site iconJava PDF Blog

How to access external HTML resources in the GlassFish server

An alternate document root (docroot) allows for a web application to serve requests for certain resources from outside its own docroot.

This is a really handy feature when two or more of your applications try to access sources in the same directory: (usually you can use web-inf directory if you are running a single Java EE application), more usage information about this can be found at GlassFish documentation page.

However if you access an HTML file (which is not part of an application and resides outside the application) then the file will be delivered as a single content and neither CSS or image would not come along with that particular HTML file, eventually you would see only the text version of an HTML file.

We encountered the same situation where we needed to access PDF files and converted HTML, CSS, and image elements those reside outside the application scope. So I thought of using file server class in such exceptional cases.

    import java.io.BufferedInputStream;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.IOException;
    import javax.servlet.ServletException;
    import javax.servlet.ServletOutputStream;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;

    @WebServlet(name = "FileServer", urlPatterns ={"/output/*"})
    public class FileServer extends HttpServlet {
        protected void doGet(HttpServletRequest request,HttpServletResponse response)
                throws ServletException, IOException {

            String fileName = request.getRequestURI();
            String justName = fileName.substring(fileName.lastIndexOf("/"),
                    fileName.length());
            int pos = request.getContextPath().length();
            fileName = "../docroot" + fileName.substring(pos, fileName.length());
            BufferedInputStream buf = null;
            ServletOutputStream myOut = null;

            try {
                myOut = response.getOutputStream();
                File myfile = new File(fileName);

                response.setContentLength((int) myfile.length());

                //statement useful in IE browsers
                if (fileName.endsWith(".css")) {
                    response.setContentType("text/css");
                } else if (fileName.endsWith(".js")) {
                    response.setContentType("text/javascript");
                } else {
                    response.setContentType(request.getContentType());
                }

                FileInputStream input = new FileInputStream(myfile);
                buf = new BufferedInputStream(input);
                int readBytes = 0;

                //read from the file; write to the ServletOutputStream
                while ((readBytes = buf.read()) != -1) {
                    myOut.write(readBytes);
                }

            } catch (IOException ioe) {
                throw new ServletException(ioe.getMessage());
            } finally {
                //close the input/output streams
                if (myOut != null) {
                    myOut.close();
                }
                if (buf != null) {
                    buf.close();
                }
            }
        }
    }