suda

Suda is the Senior Java EE Developer at IDR Solutions, and specialises in XFA, Fonts, True-Type Fonts, application servers and conversions.
He is a keen science-fiction fan in his spare time.

How to access external HTML resources in the GlassFish server

1 min read

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();
                }
            }
        }
    }

This post is part of our “GlassFish Articles Index” series. In these articles, we aim to explore Glassfish in different ways, from useful tutorials, to JaveOne and general.

If you’re a first-time reader, or simply want to be notified when we post new articles and updates, you can keep up to date by social media (Twitter, Facebook and Google+) or the  Blog RSS.

suda

Suda is the Senior Java EE Developer at IDR Solutions, and specialises in XFA, Fonts, True-Type Fonts, application servers and conversions.
He is a keen science-fiction fan in his spare time.

Java 10’s Experimental JIT Compiler explained in 5 minutes

This month’s theme is Java 10 to tie in with the release of the new Java version. Despite being only 6 months since Java...
Georgia Ingham
1 min read

Leave a Reply

Your email address will not be published. Required fields are marked *