我需要为我的应用程序中的所有PDF文件设置内容配置头响应。因此,我认为最好的方法是使用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
}}
我把我的应用web.xml配置成这样:
<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>我需要获得的PDF是在{TOMCAT_HOME}/webapp/resources/ files /*. PDFs中隐藏的静态文件,问题是当我在浏览器中指向该Url (localhost:8080/resources/files/myPDF.pdf)时,servlet不会打印任何东西,它不会调用servlet。但是当我从浏览器中点击一个PDF,它位于docbase文件夹{TOMCAT_HOME}/webapps/FRONTEND_BANCO_CO/res/myPDF.pdf -> localhost:8080/res/myPDF.pdf中,它确实有效,它会打印我在doGet()方法中拥有的消息。即使我点击了一个不存在但最后有.pdf的url,它也会超出servlet并工作。
我试着将资源文件夹从{TOMCAT_HOME}/webapp/ resources /移到TOMCAT_HOME,它可以工作。因此,我得出的结论是,它与server.xml上下文标记上的path属性有关:
<Context path="" reloadable="true" docBase="FRONTEND_BANCO_CO"></Context>我不能更改path属性,所以这不是一个选项。我也不能将资源文件夹留在docbase文件夹中。有什么建议吗?请帮帮忙,我一整天都在忙这件事
发布于 2020-06-26 01:39:44
是的,网络应用是独立的应用程序。一般来说,一个应用程序实际上不知道另一个应用程序。Tomcat中webapps下的每个文件夹都是一个不同的应用程序。
你有三个选择:
/resources/files/myPDF.pdf.
resources成为一个完整的webapp (至少有一个WEB-INF目录),并链接到webapp下的PDF文件,我认为这是不可能的。
最简单的选择是1。您没有提到安全性(这意味着任何人都可以在您的服务器上访问/resources ),但最简单的方法是在您的resources目录下创建一个WEB-INF/web.xml文件。基本上是空的:
<?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>现在,这应该允许您访问http:://localhost:8080/resources/files/myPDF.pdf,因为resources目录现在是JEE应用程序。
如果您需要修改默认的HttpResponse,那么您也需要resources webapp应用程序中的一些代码。
https://stackoverflow.com/questions/62586371
复制相似问题