suda Senior Java EE Develope specialises in Pdf forms , Fonts, application servers and Image manipulation, meditates in 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 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("/"),
            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")) {
                } else if (fileName.endsWith(".js")) {
                } else {

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

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

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

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.

Are you a Developer working with PDF files?

Our developers guide contains a large number of technical posts to help you understand the PDF file Format.

Do you need to solve any of these problems?

Display PDF documents in a Web app
Use PDF Forms in a web browser
Convert PDF Documents to an image
Work with PDF Documents in Java
suda Senior Java EE Develope specialises in Pdf forms , Fonts, application servers and Image manipulation, meditates in spare time.

Leave a Reply

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

IDRsolutions Ltd 2022. All rights reserved.