有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Tomcat找不到我尝试转发的jsp页面

我正在构建一个应用程序,目前正在登录页面上工作。当我启动Tomcat时,它会正常启动,并打开一个显示我的索引的浏览器。登录的jsp页面。然而,在成功登录后,我并没有按照登录servlet中的指示被转发到下一个jsp页面,而是得到了一个404“源服务器没有找到目标资源的当前表示,或者不愿意透露存在一个。”

我已经上网了。xml I看起来不错,我使用注释来映射servlet。我不知道还能尝试什么。即使我故意输入错误的登录详细信息,我也会收到相同的错误,而不是“else”语句中包含的输出

@WebServlet("/Login")
public class Login extends HttpServlet {

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        String uname = request.getParameter("username");
        String pwd = request.getParameter("password");

        if (uname.equals("MyUsername") && pwd.equals("MyPassword")){
            RequestDispatcher dispatcher = request.getRequestDispatcher("/WEB-INF/welcome.jsp");
            dispatcher.forward(request, response);
        }else {
            PrintWriter writer = response.getWriter();
            writer.write("User details not recognised");
        }


    }

}
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
        <welcome-file>welcome.jsp</welcome-file>
    </welcome-file-list>

</web-app>

共 (1) 个答案

  1. # 1 楼答案

    如果转发到/WEB-INF/welcome.jsp,服务器将在上下文根目录WEB-INF中查找文件welcome.jsp,即WEB-INF目录

    因此,您不应该转发到/WEB-INF/welcome.jsp,而应该转发到/welcome.jsp

    无论如何,WEB-INF是保留的,无论重定向到哪里都不起作用