Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/351.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Servlet RequestDispatcher未转发_Java_Eclipse_Jsp_Tomcat_Servlets - Fatal编程技术网

Java Servlet RequestDispatcher未转发

Java Servlet RequestDispatcher未转发,java,eclipse,jsp,tomcat,servlets,Java,Eclipse,Jsp,Tomcat,Servlets,我正在学习Javaservlet和JSP 我有以下代码: HelloServlet.jsp public class HelloServlet extends HttpServlet { private static final long serialVersionUID=1; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws S

我正在学习Javaservlet和JSP

我有以下代码:

HelloServlet.jsp

public class HelloServlet extends HttpServlet {
    private static final long serialVersionUID=1;

    protected void doGet(HttpServletRequest request,
       HttpServletResponse response)
       throws ServletException, IOException {

        response.setContentType("text/html");
        response.setCharacterEncoding("utf-8);

        RequestDispatcher aDispatcher = request.getRequestDispatcher("file.jsp");
        aDispatcher.forward(request,response);
   }
}
<!DOCTYPE html>
<%@ page language="java" contentType="text/html; charset=UTF-8" 
   pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<html>
    <head>
        <title>First JSP</title>
    </head>
    <body>
        Hello!!
    </body>
</html>
type - Status report
message - Projectname/file.jsp
description - The requested resource is not available.
file.jsp

public class HelloServlet extends HttpServlet {
    private static final long serialVersionUID=1;

    protected void doGet(HttpServletRequest request,
       HttpServletResponse response)
       throws ServletException, IOException {

        response.setContentType("text/html");
        response.setCharacterEncoding("utf-8);

        RequestDispatcher aDispatcher = request.getRequestDispatcher("file.jsp");
        aDispatcher.forward(request,response);
   }
}
<!DOCTYPE html>
<%@ page language="java" contentType="text/html; charset=UTF-8" 
   pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<html>
    <head>
        <title>First JSP</title>
    </head>
    <body>
        Hello!!
    </body>
</html>
type - Status report
message - Projectname/file.jsp
description - The requested resource is not available.
在Tomcat上运行该文件时,出现以下错误:
HTTP状态404-/Projectname/file.jsp

public class HelloServlet extends HttpServlet {
    private static final long serialVersionUID=1;

    protected void doGet(HttpServletRequest request,
       HttpServletResponse response)
       throws ServletException, IOException {

        response.setContentType("text/html");
        response.setCharacterEncoding("utf-8);

        RequestDispatcher aDispatcher = request.getRequestDispatcher("file.jsp");
        aDispatcher.forward(request,response);
   }
}
<!DOCTYPE html>
<%@ page language="java" contentType="text/html; charset=UTF-8" 
   pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<html>
    <head>
        <title>First JSP</title>
    </head>
    <body>
        Hello!!
    </body>
</html>
type - Status report
message - Projectname/file.jsp
description - The requested resource is not available.

我做错了什么?因为我自己找不到解决方案

请尝试使用前缀斜杠,如下所示

RequestDispatcher aDispatcher = request.getRequestDispatcher("/file.jsp");
如果jsp文件直接位于webapp文件夹下

或尝试

RequestDispatcher aDispatcher = request.getRequestDispatcher("/WEB-INF/file.jsp");
如果jsp文件位于WEB-INF文件夹下

项目结构:

WebContent
       |
       |__file.jsp
       |
       |__WEB-INF
              |
              |__file.jsp
              |__web.xml

阅读

如果您不想直接访问此JSP文件,则将其放入不能公开访问的
WEB-INF
文件夹中,这对于受限资源来说是更安全的方式


放置在WEB-INF下的JSP文件不能通过点击URL直接访问,在这种情况下,它只能由应用程序访问。

文件
file.JSP
位于哪里?在
WEB-INF
文件夹中(它是Eclipse中的正常位置)然后您需要通过
/WEB-INF/file.jsp
访问它。我已经将它放在WebContent文件夹下,现在它可以工作了。现在这是一个愚蠢的问题:(是打字错误
/urlpattern
还是web.xml中定义的准确url模式。是
HelloServlet.jsp
还是
HelloServlet.java
我把它放在
WebContent
文件夹下,现在它工作了。现在这是一个愚蠢的问题:(