如何在Servlet中使用jsp中的doGet

时间:2011-06-14 19:53:44

标签: jsp servlets web.xml

我正在尝试向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>

任何人都知道为什么不起作用?

2 个答案:

答案 0 :(得分:2)

所有法国人都非常困惑。但至少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

是不必要的

答案 1 :(得分:0)

您的web.xml文件可能不正确。看起来应该是这样的:

<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_2_5.xsd" version="2.5">

<servlet>
<servlet-name>pdfreader</servlet-name>
<servlet-class>com.search.ts.pdfreader</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>pdfreader</servlet-name>
<url-pattern>/pdfreader/*</url-pattern>
</servlet-mapping>

</web-app>