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 :'(