How to use doGet in jsp with Servlet

此生再无相见时 提交于 2020-01-06 14:00:53

问题


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.

I invoke it from my JSP like this

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

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) {}
            }
    }

}

when i create the servlet and the jsp i dont get any web.xml in web-inf (i use eclipse)

so i try to create one

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


回答1:


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:


First, the link:

<a href="/pdfreader/<%=filename%>"><%=bookName %></a>

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>

Second, the servlet declaration:

@WebServlet("/pdfreader")

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

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

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>

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




回答2:


Your web.xml file is probably not correct. It should look something like this:

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



来源:https://stackoverflow.com/questions/6349265/how-to-use-doget-in-jsp-with-servlet

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!