In Java web development, a typical scenario is the user fills in details on a form, and then submits the form to a Java servlet on the server for processing, and then the servlet redirects the user to the result page.

In this article, you will learn how to forward request from a Java servlet to a destination page which can be JSP or HTML.

First, in the servlet’s doGet() / doPost() method, you need to get a reference of RequestDispatcher from the request, passing the destination page. For example:

String destination = "result.jsp";
RequestDispatcher requestDispatcher = request.getRequestDispatcher(destination);
To send data from the servlet to the JSP page, set attributes for the request object in the form of name-value. For example:

String name = "John";
request.setAttribute("name", name);

Integer numberOfItems = 1000;
request.setAttribute("itemCount", numberOfItems);

List<String> fruits = Arrays.asList("Apple", "Banana", "Lemon", "Papaya");
request.setAttribute("itemCount", numberOfItems);
Then call the forward() method on the RequestDispatcher() object. For example:

requestDispatcher.forward(request, response);
This method should be called at last in a code block, because afterward the request has been forwarded.

And in the destination JSP page, you can use JSP Expression Language (EL) to read the values of the attributes stored in the request. For example:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"%>

<html>
<body>
....
	<p>Name: ${name} </p>
	<p>Number of items: ${itemCount} </p>
...
</body>
</html>
To read a collection, you can use the JSTL’s forEach tag. For example:

<c:forEach var="aFruit" items="${fruits}">
	
	<p> ${aFruit} </p>
	
</c>
 



And here’s the code example of a Java servlet which is supposed to handle login request - it redirects the user to the home page if login successful:

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/login")
public class LoginServlet extends HttpServlet {


	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		// check login...

		String fullName = "John John";

		request.setAttribute("name", fullName);

		RequestDispatcher requestDispatcher = request.getRequestDispatcher("home.jsp");

		requestDispatcher.forward(request, response);

	}

}
Note that with this kind of forward from Servlet to JSP, the URL on the browser remains unchanged - it is the URL of the servlet - even you specify the JSP page.

And if you commit the response before calling forward(), an IllegalStateException is thrown. Consider the following code snippet:

String destination = "result.jsp";

response.getWriter().println("Wait, one more thing...");
response.getWriter().close();

RequestDispatcher dispatcher = request.getRequestDispatcher(destination);
dispatcher.forward(request, response);
Calling forward() in this case causes the server throws the following exception:

java.lang.IllegalStateException: Cannot forward after response has been committed
Because the response is already committed before, so the request cannot be forward. So you should pay attention to this case.

 

Related Java Servlet Tutorials:


About the Author:

is certified Java programmer (SCJP and SCWCD). He started programming with Java in the time of Java 1.4 and has been falling in love with Java since then. Make friend with him on Facebook and watch his Java videos you YouTube.



Add comment

   


Comments 

#2Sam2020-05-15 17:36
test-1 comment is not short
Quote
#1mostafa hegazy2020-04-21 16:41
the image is not appear when i call servlet or from the browser but it appeares in jsp
but i dos't appear when you call websit/
Quote