<div dir="ltr" style="text-align: justify;">
<div dir="ltr" style="text-align: justify;">In this tutorial you will learn how to use <b><i>forward()</i></b> method of <b><i>RequestDispatcher </i></b>in Servlet<br />
<i><b>forward() </b></i>method of <i><b>RequestDispatcher </b></i>forwards the request made by the client by the the resource (any of them servlet , jsp, html, etc.) on the server without including the content of the requested resource.<i><b>syntax :</b></i></p>
<pre class="highlight">void forward(request, response) throws ServletException, IOException 
</pre>
<h2><i><b>RequestDispacher </b></i></h2>
<p>The <i><b>RequestDispacher </b></i>interface provides the facility of dispatching the request to another resource it may be html, servlet or jsp.This interface can also be used to include the content of another resource also. It is one of the way of servlet collaboration.</p>
<p>There are two methods defined in the <i><b>RequestDispatcher </b></i>interface.</p>
<h2><b>Methods of <i>RequestDispatcher </i>interface</b></h2>
<p>The <i><b>RequestDispatcher </b></i>interface provides two methods. They are:</p>
<ol style="text-align: left;">
<li><i><b>public void forward(ServletRequest request,ServletResponse response)throws ServletException,java.io.IOException:</b></i>Forwards a request from a servlet to another resource (servlet, JSP file, or HTML file) on the server.
<div style="background-color: #f2f9fc; border: 1px solid #c9e6f2; border-radius: 3px; padding: 16px; line-height: 1.45;"><span style="color: red; font-size: x-large; text-align: center;"><b>Popular Tutorials</b></span></p>
<ul style="text-align: left;">
<li><b><a href="https://dineshonjava.com/spring-tutorial/"><em><strong>Spring Tutorial</strong> </em></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-web-mvc-framework-chapter-38/"><strong><em>Spring MVC Web Tutorial </em></strong></a></b></li>
<li><b><a href="https://dineshonjava.com/introduction-to-spring-boot-a-spring-boot-complete-guide/"><strong>Spring Boot Tutorial</strong> </a></b></li>
<li><b><a href="https://dineshonjava.com/spring-security-take-baby-step-to-secure/"><em>Spring Security Tutorial</em></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-aop-tutorial-with-example-aspect-advice-pointcut-joinpoint/"><em>Spring AOP Tutorial</em></a></b></li>
<li><b><a href="https://dineshonjava.com/using-spring-jdbc-framework-chapter-32/"><em>Spring JDBC Tutorial</em></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-hateoas-hypermedia-driven-restful-web-service/"><em><strong>Spring HATEOAS </strong></em></a></b></li>
<li><b><a href="https://dineshonjava.com/microservices-with-spring-boot/"><em><strong>Microservices with Spring Boot</strong></em></a></b></li>
<li><b><a href="https://dineshonjava.com/jax-rs-web-service-tutorial/"><strong><em>REST Webservice</em> </strong></a></b></li>
<li><b><a href="https://dineshonjava.com/core-java-baby-step-to-be-best-java-ian/"><em><strong>Core Java </strong></em></a></b></li>
<li><b><a href="https://dineshonjava.com/hibernate-3-on-baby-steps/"><em><strong>Hibernate Tutorial</strong></em></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-batch-process-with-example/"><strong><em>Spring Batch</em> </strong></a></b></li>
</ul>
</div>
<p>HttpSession interface</p>
<div class="separator" style="clear: both; text-align: center;"><img src="https://dineshonjava.com/wp-content/uploads/2013/12/forward.jpg" border="0" /></div>
<p>As you see in the above figure, response of second servlet is sent to the client. Response of the first servlet is not displayed to the user.</li>
<li><i><b>public void include(ServletRequest request,ServletResponse response)throws ServletException,java.io.IOException:</b></i>Includes the content of a resource (servlet, JSP page, or HTML file) in the response.
<div class="separator" style="clear: both; text-align: center;"><img src="https://dineshonjava.com/wp-content/uploads/2013/12/RequestDispatcherInclude.jpg" border="0" /></div>
<p>As you can see in the above figure, response of second servlet is included in the response of the first servlet that is being sent to the client.</li>
</ol>
<h2><b>Getting the object of <i>RequestDispatcher</i></b></h2>
<p>The <i><b>getRequestDispatcher() </b></i>method of <i><b>ServletRequest </b></i>interface returns the object of <b><i>RequestDispatcher</i></b>.</p>
<p><i><b>Syntax of getRequestDispatcher method:</b></i></p>
<pre class="highlight">public RequestDispatcher getRequestDispatcher(String resource); 
</pre>
<h3><b>Example of using getRequestDispatcher method</b></h3>
<pre class="highlight">RequestDispatcher rd=request.getRequestDispatcher("/welcomeServlet"); 
 //welcomeServlet is the url-pattern of the second servlet 
 rd.forward(request, response);//method may be include or forward 
</pre>
<h3><b>Example of RequestDispatcher interface</b></h3>
<p>In this example, we are validating the password entered by the user. If password is servlet, it will forward the request to the <i><b>WelcomeServlet</b></i>, otherwise will show an error message: sorry username or password error!. In this program, we are checking for hard coded information. But you can check it to the database also that we will see in the development chapter.</p>
<p><i><b>1. :login.html file</b></i><i><b> </b></i>for getting input from the user.</p>
<pre class="highlight"><;!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">; 
<;html>; 
<;head>; 
<;meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">; 
<;title>;Login Page<;/title>; 
<;/head>; 
<;body>; 
<;form method="get" action="loginServlet">; 
<;p>;Enter your info in the following text boxes<;/p>; 
Enter your name <;input type="text" name="text1"/>;<;br>; 
Enter your password <;input type="password" name="text2"/>;<;br>; 
<;input type="submit" value="submit"/>; 
<;/form>; 
<;/body>; 
<;/html>; 
</pre>
<p><b>2. LoginServlet.java file:</b> a servlet class for processing the response. If password is servlet, it will forward the request to the welcome servlet.</p>
<pre class="highlight">import java.io.*; 
import javax.servlet.*; 
import javax.servlet.http.*; 
 
 
public class LoginSevlet extends HttpServlet { 
 
public void doPost(HttpServletRequest request, HttpServletResponse response) 
 throws ServletException, IOException { 
 
 response.setContentType("text/html"); 
 PrintWriter out = response.getWriter(); 
 
 String userName=request.getParameter("userName"); 
 String userPass=request.getParameter("userPass"); 
 
 if(userPass.equals("sweety")){ 
 RequestDispatcher rd=request.getRequestDispatcher("welcomeServlet"); 
 rd.forward(request, response); 
 } 
 else{ 
 out.print("Sorry User Name or Password Error!"); 
 RequestDispatcher rd=request.getRequestDispatcher("/login.html"); 
 rd.include(request, response); 
 
 } 
 } 
 
} 
</pre>
<p><b>3. WelcomeServlet.java file: </b>a servlet class for displaying the welcome message.</p>
<pre class="highlight">import java.io.*; 
import javax.servlet.*; 
import javax.servlet.http.*; 
 
public class WelcomeServlet extends HttpServlet { 
 
 public void doPost(HttpServletRequest request, HttpServletResponse response) 
 throws ServletException, IOException { 
 
 response.setContentType("text/html"); 
 PrintWriter out = response.getWriter(); 
 
 String userName=request.getParameter("userName"); 
 out.print("Welcome "+userName); 
 } 
 
} 
</pre>
<p><b><i>4. web.xml file:</i> a deployment descriptor file that contains the information about the servlet.</b></p>
<pre class="highlight"><;?xml version="1.0" encoding="UTF-8"?>; 
<;web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">; 
<;display-name>;servletRequestDispatcher<;/display-name>; 
<;servlet>; 
 <;servlet-name>;loginServlet<;/servlet-name>; 
 <;servlet-class>;LoginServlet<;/servlet-class>; 
 <;/servlet>; 
 <;servlet>; 
 <;servlet-name>;welcomeServlet<;/servlet-name>; 
 <;servlet-class>;WelcomeServlet<;/servlet-class>; 
 <;/servlet>; 
 
 
 <;servlet-mapping>; 
 <;servlet-name>;loginServlet<;/servlet-name>; 
 <;url-pattern>;/loginServlet<;/url-pattern>; 
 <;/servlet-mapping>; 
 <;servlet-mapping>; 
 <;servlet-name>;welcomeServlet<;/servlet-name>; 
 <;url-pattern>;/welcomeServlet<;/url-pattern>; 
 <;/servlet-mapping>; 
 
 <;welcome-file-list>; 
 <;welcome-file>;login.html<;/welcome-file>; 
 <;/welcome-file-list>; 
 
<;/web-app>; 
</pre>
<div class="separator" style="clear: both; text-align: center;"><img src="https://dineshonjava.com/wp-content/uploads/2013/12/requestdispatcher.jpg" border="0" /></div>
<p>Now let us start tomcat server using C:Program Files (x86)Apache Software FoundationTomcat 7.0binstartup.bat (on windows) or /bin/startup.sh (on Linux/Solaris etc.) and finally type <i><b>http://localhost:8080/login.html</b></i> in browser&#8217;s address box. If everything goes fine, you would get following result:</p>
<div class="separator" style="clear: both; text-align: center;"><img src="https://dineshonjava.com/wp-content/uploads/2013/12/111-4.png" border="0" /></div>
<p><b>With wrong password it request dispatched to again login form.</b></p>
<div class="separator" style="clear: both; text-align: center;"><img src="https://dineshonjava.com/wp-content/uploads/2013/12/111-5.png" border="0" /></div>
<p><b>With right user name and password.</b></p>
<div class="separator" style="clear: both; text-align: center;"><img src="https://dineshonjava.com/wp-content/uploads/2013/12/111-6.png" border="0" /></div>
</div>
<div style="background-color: pink; border-width: thin; text-align: center;"><b><;<;<a href="https://dineshonjava.com/httpresponse-servlet/">Previous</a> <;<; || <a href="https://dineshonjava.com/java-servlets-overview/">Index </a>|| >;>;<a href="https://dineshonjava.com/httpsession-interface/" target="_blank" rel="noopener">Next</a> >;>;</b></div>
<p> ;</p>
<div style="background-color: #f2f9fc; border: 1px solid #c9e6f2; border-radius: 3px; padding: 16px; line-height: 1.45;"><b>Servlet Related Posts</b></p>
<ol style="text-align: left;">
<li><b><a href="https://dineshonjava.com/java-servlets-overview/">Java Servlets Overview</a></b></li>
<li><b><a href="https://dineshonjava.com/servlet-life-cycle/">Servlet Life Cycle</a></b></li>
<li><b><a href="https://dineshonjava.com/hello-world-example-in-servlet-interface/">Servlet Example</a></b></li>
<li><b><a href="https://dineshonjava.com/difference-between-servletconfig-and-cervletcontext-in-java-servlet/ "> Difference between ServletConfig and ServletContext<br />
</a></b></li>
<li><b><a href="https://dineshonjava.com/difference-between-genericservlet-and/"> Difference between GenericServlet and HttpServlet<br />
</a></b></li>
<li><b><a href="https://dineshonjava.com/what-is-web-application-servlet/">What is web application?</a></b></li>
<li><b><a href="https://dineshonjava.com/advantages-of-servlets-over-cgi/">Advantages of Servlets over CGI</a></b></li>
<li><b><a href="https://dineshonjava.com/difference-between-genericservlet-and/">GenericServlet Example</a></b></li>
<li><b><a href="https://dineshonjava.com/requestdispatcher-interface/">RequestDispatcher Example</a></b></li>
<li><b><a href="https://dineshonjava.com/servletconfig-interface/">ServletConfig</a></b></li>
<li><b><a href="https://dineshonjava.com/servletcontext-interface/">ServletContext </a></b></li>
<li><b><a href="https://dineshonjava.com/servlet-filters/">Servlet Filter Example</a></b></li>
<li><b><a href="https://dineshonjava.com/servlets-database-access/">Database Access Example using Sevlet</a></b></li>
<li><b><a href="https://dineshonjava.com/servlet-upload-file-example/">File Uploading Example using Servlet</a></b></li>
</ol>
</div>
<p> ;</p>
</div>
<div class="wp-post-navigation"> 
									 <div class="wp-post-navigation-pre"> 
									 <a href="https://dineshonjava.com/httpresponse-servlet/">Previous</a> 
									 </div> 
									 <div class="wp-post-navigation-next"> 
									 <a href="https://dineshonjava.com/httpsession-interface/">Next</a> 
									 </div> 
									</div>
<script type="text/javascript">
jQuery(document).ready(function($) {
 $.post('https://dineshonjava.com/wp-admin/admin-ajax.php', {action: 'mts_view_count', id: '264'});
});
</script>
Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…
Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…
Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…
Technology has emerged a lot in the last decade, and now we have artificial intelligence;…
Managing a database is becoming increasingly complex now due to the vast amount of data…
Overview In this article, we will explore Spring Scheduler how we could use it by…