Search code examples
javahtmlservletsforwardrequestdispatcher

Why is my code not printing proper result when I am using requestDispatcher.forward function?


I am trying to learn the requestDispatcher.forward function. So I wrote an Html code and then as we click the submit button on the HTML page it goes to a webservlet1. There I use forward function to go to webservlet2.

Please see the code below

web.html

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Login Page</title>
<style type="text/css">
div{
    top:10px;
    left:100px;
    width:250px;
    height:120px;
    border: 2px solid green;
    border-color:green;
    background-color:yellow;
    
    }
</style>
<script type="text/javascript">
function loginValidation(){
    var FirstName=document.LoginForm.fname.value;
    var LastName=document.LoginForm.lname.value;
    if(FirstName=="" || LastName=""){
        alert("Field cannot be empty");
        return false;
    }
}
</script>
</head>
<body>
<div>
<form name="LoginForm" action="welcomeservlet1" onsubmit="return loginValidation()" method="post">
<table>
<tr><td>First Name</td><td><input type="text" name="fname"/></td></tr>
<tr><td>Last Name</td><td><input type="text" name="lname"/></td></tr>
<tr><td colspan="2" align="center"><input type="submit" value="login"/></td></tr>
</table>
</form>
</div>
</body>
</html>

Code for welcomeservlet1

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            // TODO Auto-generated method stub
            PrintWriter out=response.getWriter();
            String fname=request.getParameter("fname");
            String lname=request.getParameter("lname");
            String FullName=fname+" "+lname;
            FullName=FullName.toUpperCase();
            out.println("<h1><font color=green> Welcome "+FullName+"</font></h1>\n");
            request.setAttribute("Fullname",FullName);
            RequestDispatcher requestdispatcher=request.getRequestDispatcher("welcomeservlet2");
            requestdispatcher.forward(request, response);
            out.println("<br>Thank you");
        }
    
    }

Code for WelcomeServlet2

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            // TODO Auto-generated method stub
            String Name=request.getAttribute("Fullname").toString();
            PrintWriter out=response.getWriter();
            out.println("Your full name is "+Name+"\n");
            out.println("<br>The name length is "+Name.length());
        }

The output of the code is

Your full name is Muskan AGARWAL The name length is 12

Why is the code not printing what I am putting in the welcomeservlet1 code? It is not printing "Welcome Muskan Agarwal". Why is it not printing?


Solution

  • As per the servlet specification, forward should be called only if no output has been committed to the client. If you try to forward when the output has already been committed to the client, the IllegalStateException will be thrown.

    Why did you not face IllegalStateException?

    You did not face this exception because the output has not been committed in your code. If you put response.flushBuffer(); before requestdispatcher.forward(request, response);, you will face this exception.

    And finally...why did "Welcome ..." not get printed?

    It is because any uncommitted output was simply thrown away before forward.