fathertodumptious
fathertodumptious

Reputation: 53

Redirecting servlet to another html page

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?

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>

Upvotes: 5

Views: 60789

Answers (3)

Kusal Thiwanka
Kusal Thiwanka

Reputation: 331

Redirect to HTML

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

Upvotes: 2

dev
dev

Reputation: 1355

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

Upvotes: 9

TanvirChowdhury
TanvirChowdhury

Reputation: 2445

you can use

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

2.String path= "/redirect";

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

dispatcher.forward(request,response);

Upvotes: 1

Related Questions