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

时间:2014-11-19 17:06:34

标签: java html xml servlets mapping

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

这是我登录和输入详细信息的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();


}

在我的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>

3 个答案:

答案 0 :(得分:7)

你可以试试这个:

response.sendRedirect("redirect.html");

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

替代方式,

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

答案 1 :(得分:0)

重定向到HTML

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

答案 2 :(得分:0)

您可以使用

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

2.String path= "/redirect";

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

dispatcher.forward(request,response);