*//post method*
protected void doPost (HttpServletRequest req, HttpServletResponse res)
throws ServletException,IOException {
String username = req.getParameter("username");
String password = req.getParameter("password");
System.out.println( " Form data recieved .. Now Verifying ");
if ( loginVerificator ( username, password ) ) {
Cookie xO = new Cookie ( "username", username );
Cookie xT = new Cookie ( "password", password );
res.addCookie( xO );
res.addCookie( xT );
res.setContentType( "text/html" );
PrintWriter out = res.getWriter( );
out.println("<meta http-equiv=\"refresh\" content=\"2\";url=\"home\">");
}
The post method retrieves the form parameters and then creates cookies from them and adds those cookies to the response. Then it redirects the page with meta tag.
I want to know why this page is reloading instead of redirecting. I am not able to find where I am making a mistake.
out.println("<meta http-equiv=\"refresh\" content=\"2\";url=\"home\">");writes a
metatag in the response with the following value:<meta http-equiv="refresh" content="2";url="home">which is incorrect, as the url is not wrapped by the quotes for the content attribute value. Instead the tag should have been generated as:<meta http-equiv="refresh" content="2;url=home">which requires the corresponding line in the servlet to be:out.println("<meta http-equiv=\"refresh\" content=\"2;url=home\">");Note that the
meta refreshconcept has been deprecated by W3C. If you intend to redirect the user to a new page, it is always preferable to use a HTTP 302 response, which is easily doable in Servlets using theHttpServletResponse.sendRedirect(location)method.