我正在尝试向Servlet发送一些东西,但是我得到了
Etat HTTP 404 - /pdfreader/Services%20Web%20avec%20J2EE%20et%20.NET.pdf -------------------------------------------------------------------------------- type Rapport d''état message /pdfreader/Services%20Web%20avec%20J2EE%20et%20.NET.pdf description La ressource demandée (/pdfreader/Services%20Web%20avec%20J2EE%20et%20.NET.pdf) n'est pas disponible.
我像这样从我的JSP调用它
<a href="/pdfreader/<%=filename/*le nom d'un fichier pdf par exemple (jsp.pdf)*/ %>"><%=bookName %> </a>
而我的servlet代码是
package com.search.ts; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.net.URLDecoder; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Servlet implementation class pdfreader */ @WebServlet("/pdfreader") public class pdfreader extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public pdfreader() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub String filename = URLDecoder.decode(request.getPathInfo(), "UTF-8"); //filename= request.getParameter("err"); //String filename =(String) request.getAttribute("linkbook"); File file = new File("F:/fichiers/", filename); response.setContentType(getServletContext().getMimeType(file.getName())); response.setContentLength((int) file.length()); response.setHeader("Content-Disposition", "inline; filename=\"" + file.getName() + "\""); BufferedInputStream input = null; BufferedOutputStream output = null; try { input = new BufferedInputStream(new FileInputStream(file)); output = new BufferedOutputStream(response.getOutputStream()); byte[] buffer = new byte[8192]; int length; while ((length = input.read(buffer)) > 0) { output.write(buffer, 0, length); } } finally { if (output != null) try { output.close(); } catch (IOException ignore) {} if (input != null) try { input.close(); } catch (IOException ignore) {} } } }
当我创建servlet和jsp时,我在web-inf中没有得到任何web.xml(我使用eclipse)
所以我尝试创建一个
<?xml version="1.0" encoding="UTF-8"?> <web-app> <welcome-file-list> <welcome-file>/vieu/indexS.jsp</welcome-file> </welcome-file-list> <servlet> <javaee:description></javaee:description> <javaee:display-name>pdfreader</javaee:display-name> <servlet-name>pdfreader</servlet-name> <servlet-class>pdfreader</servlet-class> <jsp-file>/vieu/indexS.jsp</jsp-file> </servlet> <servlet-mapping> <servlet-name>com.search.ts.pdfreader</servlet-name> <url-pattern>/pdfreader/*</url-pattern> </servlet-mapping> </web-app>
有人知道为什么那行不通吗?
所有的法语都非常令人困惑。但是,至少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注释将不再起作用。相应地修复它:
web.xml
@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。
<servlet>
<servlet- mapping>