首页 > 解决方案 > 如何从运行在 Apache Tomcat 服务器上的 Java servlet 重定向到本地 HTML 文件?

问题描述

我创建了一个简单的 Java servlet,它从 HTML 表单中获取登录凭据。我检查用户名和密码,然后相应地重定向到本地 HTML 页面。使用命令 response.sendRedirect 时出现“404:找不到资源”错误。我知道错误在哪里。我不确定相对 URL 是怎样的。我已将这两个 HTML 文件放在 sendRedirect 参数中提供的路径中。

protected void doGet(HttpServletRequest request, HttpServletResponse 
response) throws ServletException, IOException 
{   
     String username = request.getParameter("username");
     String password = request.getParameter("password");

     System.out.println(username);
     System.out.println(password);

     if((username.equals("xxx"))&&(password.equals("yyy")))
     {
        response.sendRedirect("http://localhost:8080/MyServer/WebContent/correct.html");
     }
     else
     {
        response.sendRedirect("http://localhost:8080/MyServer/MyServer/WebContent/wrong.html");      }
}

标签: javahtmlapachetomcatservlets

解决方案


推荐阅读