所有的法语都非常令人困惑。但是,至少HTTP 404错误显然是不言自明的:这仅意味着请求URL完全错误或资源(servlet)无法启动。
有几种潜在的问题原因:
首先,链接:
<a href="/pdfreader/<%=filename%>"><%=bookName %></a>
/
URL中的斜杠使它相对于域根。因此,当您的JSP在http:// localhost:8080 / contextname / vieu / indexS.jsp上运行时,此URL实际上指向http:// localhost:8080 / pdfreader / name.pdf。但您希望它是http:// localhost:8080 / contextname / pdfreader / name.pdf!所以相应地修复它
<a href="${pageContext.request.contextPath}/pdfreader/<%=filename%>"><%=bookName %></a>
二,servlet声明:
@WebServlet("/pdfreader")
这是完全错误的。您需要对其进行如下注释:
@WebServlet(urlPatterns={"/pdfreader/*"})
第三,web.xml
缺少Servlet API版本声明,该声明导致容器回退到兼容性最低的方式,因此新的Servlet 3.0@WebServlet
注释将不再起作用。相应地修复它:
<?xml version="1.0" encoding="UTF-8"?>
<web-app
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<!-- Config here -->
</web-app>
并 了<servlet>
与<servlet- mapping>
您的声明web.xml
。这些对于(适当的!)来说是不必要的@WebServlet
。