Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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>

like image 713
fathertodumptious Avatar asked Nov 19 '14 17:11

fathertodumptious


People also ask

Can we call HTML page from servlet?

It can either redirect to a page or directly write any content from the servlet itself. So, in short, yes, you can directly generate any content from servlet without creating any jsp or html. Just get the writer by calling response. getWriter() and write your HTML content to it.

What is the difference between sendRedirect () and forward ()?

The main important difference between the forward() and sendRedirect() method is that in case of forward(), redirect happens at server end and not visible to client, but in case of sendRedirect(), redirection happens at client end and it's visible to client.

Why do we use sendRedirect () method?

sendRedirect() method redirects the response to another resource, inside or outside the server. It makes the client/browser to create a new request to get to the resource. It sends a temporary redirect response to the client using the specified redirect location URL.


2 Answers

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);
like image 61
dev Avatar answered Sep 22 '22 12:09

dev


Redirect to HTML

RequestDispatcher ds = request.getRequestDispatcher("index.html");
ds.include(request, response);
like image 26
Kusal Thiwanka Avatar answered Sep 20 '22 12:09

Kusal Thiwanka