如何在Servlet的JSP中使用doGet [英] How to use doGet in jsp with Servlet

查看:76
本文介绍了如何在Servlet的JSP中使用doGet的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将某些东西发送到servlet,但我知道了

I'm trying to send some thing to a servlet but i get this

    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调用它

I invoke it from my JSP like this

<a href="/pdfreader/<%=filename/*le nom d'un fichier pdf par exemple (jsp.pdf)*/ %>"><%=bookName %> </a>

我的servlet代码是

and my servlet code is

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)

when i create the servlet and the jsp i dont get any web.xml in web-inf (i use 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>

有人知道为什么这行不通吗?

Anyone know why that don't work?

推荐答案

所有这些法语都非常令人困惑.但是,至少HTTP 404错误显然是不言自明的:这仅意味着请求URL完全错误或资源(servlet)无法启动.

All that French is extremely confusing. But at least a HTTP 404 error is clearly self-explaining: it just means that the request URL is plain wrong or that the resource (servlet) failed to startup.

有几种潜在的问题原因:

There are several potential problem causes:

首先,链接:

<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 !因此,请相应地解决

The leading slash / in the URL makes it relative to the domain root. So when your JSP runs on http://localhost:8080/contextname/vieu/indexS.jsp, then this URL actually points to http://localhost:8080/pdfreader/name.pdf. But you want it to be http://localhost:8080/contextname/pdfreader/name.pdf! So fix it accordingly

<a href="${pageContext.request.contextPath}/pdfreader/<%=filename%>"><%=bookName %></a>


第二,servlet声明:


Second, the servlet declaration:

@WebServlet("/pdfreader")

这是完全错误的.您需要按以下说明对其进行批注:

This is completely wrong. You need to annotate it as follows:

@WebServlet(urlPatterns={"/pdfreader/*"})


第三,web.xml缺少Servlet API版本声明,该声明导致容器回退到兼容性最低的方式,因此新的Servlet 3.0 @WebServlet注释将不再起作用.相应地修复它:


Third, the web.xml is missing the Servlet API version declaration which causes that the container falls back to the least compatibility modus and thus the new Servlet 3.0 @WebServlet annotation won't work anymore. Fix it accordingly:

<?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>

从您的web.xml

删除 <servlet><servlet-mapping>声明. 并不需要(适当的!).

and remove the <servlet> and <servlet-mapping> declarations from your web.xml. Those are not necessary with (a proper!) @WebServlet.

这篇关于如何在Servlet的JSP中使用doGet的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆