Redirecting a servlet to another html page

I have two html pages - one for login and one that accepts face data. The login page is the first page, and when the database is checked for username and password, the user can enter their data. The SQL code works just fine, it's just a display issue that I have. By the way, I'm using a Tomcat server. Can someone help or determine what I am doing wrong?

This is my java code for logging in and entering details

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-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>

+4
source share
3 answers

You can 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);
+7

HTML

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

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

2.String path= "/redirect";

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

dispatcher.forward(request,response);
0

All Articles