Java 将servlet重定向到另一个html页面

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27022741/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 03:40:57  来源:igfitidea点击:

Redirecting servlet to another html page

javahtmlxmlservletsmapping

提问by fathertodumptious

I have two html pages - one for login and one that takes in a persons details. The login page is the first page and when the database is checked for the username and password, the user is allowed to enter their details. The SQL code works perfectly, it is just a problem with the mapping I am having. I am using the Tomcat server by the way. Could anybody help or spot what i am doing wrong?

我有两个 html 页面 - 一个用于登录,一个用于接收个人详细信息。登录页面是第一页,当检查数据库的用户名和密码时,允许用户输入他们的详细信息。SQL 代码运行良好,这只是我所拥有的映射的问题。顺便说一下,我正在使用Tomcat服务器。任何人都可以帮助或发现我做错了什么吗?

This is my java code for logging in and entering details

这是我用于登录和输入详细信息的 java 代码

public class Details extends HttpServlet {

private Connection con;

public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {

  res.setContentType("text/html");
  //return writer
  PrintWriter out = res.getWriter();   

  String username = req.getParameter("username");
  String password = request.getParameter("password");

  out.close();

  try {
    login(username, password);
  } catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
  }

  res.sendRedirect("/redirect.html"); 

   String name = request.getParameter("name");
   String address = request.getParameter("address");
   String age = request.getParameter("age");

    out.println("<HTML><HEAD><TITLE>Personnel Details</TITLE></HEAD><BODY>");
    out.println(name + address + age);
    out.println("</BODY></HTML>");
    System.out.println("Finished Processing");
}

out.close();


}

In my web.xml file I have:

在我的 web.xml 文件中,我有:

<web-app>

  <servlet>
    <servlet-name>Details</servlet-name>
    <servlet-class>Details</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>Details</servlet-name>
    <url-pattern>/Details</url-pattern>
  </servlet-mapping>

 <servlet-mapping>
<servlet-name>redirect</servlet-name>
<url-pattern>/redirect</url-pattern>

回答by dev

You may try this :

你可以试试这个:

response.sendRedirect("redirect.html");

or

或者

response.setStatus(response.SC_MOVED_TEMPORARILY);
response.setHeader("Location", "redirect.html");

Alternative way,

替代方式,

ServletContext sc = getServletContext();
sc.getRequestDispatcher("/redirect.html").forward(request, response);

回答by Kusal Thiwanka

Redirect to HTML

重定向到 HTML

RequestDispatcher ds = request.getRequestDispatcher("index.html");
ds.include(request, response);

回答by TanvirChowdhury

you can use

您可以使用

1.response.sendRedirect("redirect.html")or

1.response.sendRedirect("redirect.html")或者

2.String path= "/redirect";

RequestDispatcher dispatcher =servletContext().getRequestDispatcher(path);

dispatcher.forward(request,response);