When I’m calling RequestDispatcher, instead of processing request, JSP page itself is being rendered as output…JSP content is present after “Account Created” line.
//Servlet block
if(i==1){
PrintWriter pw = response.getWriter();
pw.println("Account Created!!");
RequestDispatcher rd = request.getRequestDispatcher("Login.jsp");
rd.include(request, response);
System.out.println("Record Updated!!!");
}
//Output rendered on browser:
Account Created!!
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<link rel="icon" type="image/ico" href="favicon.ico"></link>
<link rel="shortcut icon" href="favicon.ico"></link>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Login Page</title>
</head>
<body>
<form action="./Authentication" method="post">
<p>Enter username<input type="text" name="Uname"></p>
<p>Enter password<input type="password" name="Pword"></p>
<input type="submit" value="Login">
</form>
</body>
</html>
You should not be getting the writer in the servlet and you should not be manually writing a string to it and you should be using
RequestDispatcher#forward()instead ofinclude(). You’re otherwise preventing JSP from setting the propertext/htmlcontent type and hence everything is interpreted as plain text by the webbrowser.Rewrite that servlet block as follows to let JSP do its job properly:
See also: