Search code examples
javapdftomcatservletscontent-disposition

Servlet not working with some paths in Tomcat 8


I needed to set the content-disposition header response for all PDF files in my app. So I thought the best way to do so was using a servlet:

public class PdfServlet extends HttpServlet {

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    System.out.println("---> Servlet working");
    //Some code here
}

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    System.out.println("---> Servlet working");
    //Some code here
}

}

And I configured my app web.xml like this:

<servlet>
    <servlet-name>pdfServlet</servlet-name>
    <servlet-class>net.universia.pymes.servlets.PdfServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>pdfServlet</servlet-name>
    <url-pattern>*.pdf</url-pattern>
</servlet-mapping>

The PDFs that I need to get are static files ubicated in {TOMCAT_HOME}/webapps/resources/files/*.pdf, the problem is that when I point that Url (localhost:8080/resources/files/myPDF.pdf) in the browser, the servlet does not print anything, it does not call the servlet. But when I hit from the browser a PDF that is in the docbase folder, {TOMCAT_HOME}/webapps/FRONTEND_BANCO_CO/res/myPDF.pdf --> localhost:8080/res/myPDF.pdf, it does work, it prints the message that I have in doGet() method. Even if I hit an url that does not exist but has .pdf at the end it excecute the servlet and works.

I tried moving the resources folder from {TOMCAT_HOME}/webapps/resources/ to {TOMCAT_HOME}/webapps/FRONTEND_BANCO_CO/resources/ and it works. So I concluded that it has something to do with the path attribute at my context tag in server.xml:

<Context path="" reloadable="true" docBase="FRONTEND_BANCO_CO"></Context>

I can't change the path attribute so that's not an option. I can't leave the resources folder inside the docbase folder either. Any suggestion? Please help, I've been stuck with this all day long :'(


Solution

  • Yes, webapps are independent applications. In general one application really doesn't know about another one. Every folder under webapps in Tomcat is a different application.

    You have three choices:

    1. Make the folder resources a complete webapp (with a WEB-INF directory at the least) and link to /resources/files/myPDF.pdf.
    2. Move the PDF's under your webapp which I believe you're saying isn't possible.
    3. Get the file source path of your webapp and create a file server servlet that can sent these files.

    The simplest option is 1. You don't mention anything about security (meaning that anyone could access /resources on your server) but the simplest would be to create a WEB-INF/web.xml file under your resources directory. It's basically empty:

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
             version="3.1">
    </web-app>
    

    This should now allow you to hit http:://localhost:8080/resources/files/myPDF.pdf as the resources directory is now a JEE web application.

    If you need to modify the default HttpResponse then you'll need some code in the resources webapp too.