<div dir="ltr" style="text-align: justify;">
<div dir="ltr" style="text-align: justify;">The <i><b>sendRedirect() </b></i>method of <b><i>HttpServletResponse </i></b>interface can be used to redirect response to another resource, it may be servlet, jsp or html file.It accepts relative as well as absolute URL.</p>
<p>It works at client side because it uses the url bar of the browser to make another request. So, it can work inside and outside the server.</p>
<p>In send Redirect whenever the client makes any request it goes to the container, there the container decides whether the concerned servlet can handle the request or not. If not then the servlet decides that the request can be handle by other servlet or jsp. Then the servlet calls the sendRedirect() method of the response object and sends back the response to the browser along with the status code. Then the browser sees the status code and look for that servlet which can now handle the request. Again the browser makes a new request, but with the name of that servlet which can now handle the request and the result will be displayed to you by the browser. In all this process the client is unaware of the processing.</p>
<p><b><i>The output of the program is given below:</i></b></p>
<pre class="highlight"><;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">; 
<;html>; 
<;head>; 
<;meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">; 
<;title>;Redirecting the page<;/title>; 
<;/head>; 
<;body>; 
 <;form action = "/ServletProject/SendRedirect" method = "post">; 
 <;tr>; 
 <;td>;Enter your name :<;/td>; 
 <;td>;<;input type = "text" name = "username">;<;/td>; 
 <;/tr>;<;br>; 
 <;tr>; 
 <;td>;Enter your password :<;/td>; 
 <;td>;<;input type = "password" name = "password">;<;/td>; 
 <;/tr>;<;br>; 
 <;tr>; 
 <;td>;<;input type = "submit" name = "submit">;<;/td>; 
 <;/tr>; 
 <;/form>; 
<;/body>; 
<;/html>; 
</pre>
<p> ;</p>
<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> ;</p>
<pre class="highlight">import java.io.*; 
import java.io.IOException; 
import javax.servlet.ServletException; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 
 
public class SendRedirect extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet { 
public SendRedirect() { 
super(); 
} 
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws 
ServletException, IOException { 
// TODO Auto-generated method stub 
response.setContentType("text/html"); 
PrintWriter pw = response.getWriter(); 
String name = request.getParameter("username"); 
String password = request.getParameter("password"); 
if(name.equals("dinesh")&;&; password.equals("swwety")) 
{ 
response.sendRedirect("/ServletProject/ValidUser"); 
} 
else 
{ 
pw.println("u r not a valid user"); 
} 
} 
} 
</pre>
<h2><i><b>Difference between forward() and sendRedirect() method</b></i></h2>
<p>There are many differences between the forward() method of RequestDispatcher and sendRedirect() method of HttpServletResponse interface. They are given below:</p>
<p> ;</p>
<table class="alt">
<tbody>
<tr>
<th>forward() method</th>
<th>sendRedirect() method</th>
</tr>
<tr style="background-color: white; border-color: inherit; display: table-row; vertical-align: inherit;">
<td style="border: 1px solid #d4d4d4; font-family: Verdana,Arial,sans-serif; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; line-height: 1.5; margin-left: 20px; padding: 7px 5px; vertical-align: top;">The forward() method works at server side.</td>
<td style="border: 1px solid #d4d4d4; font-family: Verdana,Arial,sans-serif; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; line-height: 1.5; margin-left: 20px; padding: 7px 5px; vertical-align: top;">The sendRedirect() method works at client side.</td>
</tr>
<tr style="background-color: #f6f4f0; border-color: inherit; display: table-row; vertical-align: inherit;">
<td style="border: 1px solid #d4d4d4; font-family: Verdana,Arial,sans-serif; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; line-height: 1.5; margin-left: 20px; padding: 7px 5px; vertical-align: top;">It sends the same request and response objects to another servlet.</td>
<td style="border: 1px solid #d4d4d4; font-family: Verdana,Arial,sans-serif; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; line-height: 1.5; margin-left: 20px; padding: 7px 5px; vertical-align: top;">It always sends a new request.</td>
</tr>
<tr style="background-color: white; border-color: inherit; display: table-row; vertical-align: inherit;">
<td style="border: 1px solid #d4d4d4; font-family: Verdana,Arial,sans-serif; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; line-height: 1.5; margin-left: 20px; padding: 7px 5px; vertical-align: top;">It can work within the server only.</td>
<td style="border: 1px solid #d4d4d4; font-family: Verdana,Arial,sans-serif; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; line-height: 1.5; margin-left: 20px; padding: 7px 5px; vertical-align: top;">It can be used within and outside the server.</td>
</tr>
<tr style="background-color: #f6f4f0; border-color: inherit; display: table-row; vertical-align: inherit;">
<td style="border: 1px solid #d4d4d4; font-family: Verdana,Arial,sans-serif; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; line-height: 1.5; margin-left: 20px; padding: 7px 5px; vertical-align: top;">Example: request.getRequestDispacher(&#8220;servlet2&#8221;).forward(request,response);</td>
<td style="border: 1px solid #d4d4d4; font-family: Verdana,Arial,sans-serif; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; line-height: 1.5; margin-left: 20px; padding: 7px 5px; vertical-align: top;">Example: response.sendRedirect(&#8220;servlet2&#8221;);</td>
</tr>
</tbody>
</table>
<p><b>MySearcher.java</b></p>
<pre class="highlight">import java.io.IOException; 
import javax.servlet.ServletException; 
import javax.servlet.http.HttpServlet; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 
 
public class MySearcher extends HttpServlet { 
 protected void doGet(HttpServletRequest request, HttpServletResponse response) 
 throws ServletException, IOException { 
 
 String name=request.getParameter("name"); 
 response.sendRedirect("https://www.google.co.in/#q="+name); 
 } 
} 
</pre>
<p><i><b>index.html</b></i></p>
<pre class="highlight"><;!DOCTYPE html>; 
<;html>; 
<;head>; 
<;meta charset="ISO-8859-1">; 
<;title>;sendRedirect example<;/title>; 
<;/head>; 
<;body>; 
 
 
<;form action="MySearcher">; 
<;input type="text" name="name">; 
<;input type="submit" value="Google Search">; 
<;/form>; 
 
<;/body>; 
<;/html>; 
</pre>
<p> ;</p>
<div class="separator" style="clear: both; text-align: center;"><img src="https://dineshonjava.com/wp-content/uploads/2013/12/111-2.png" border="0" /></div>
<div class="separator" style="clear: both; text-align: center;"><img src="https://dineshonjava.com/wp-content/uploads/2013/12/111-3.png" border="0" /></div>
<p> ;</p>
</div>
<div style="background-color: pink; border-width: thin; text-align: center;"><b><;<;<a href="https://dineshonjava.com/httpsession-interface/">Previous</a> <;<; || <a href="https://dineshonjava.com/java-servlets-overview/">Index </a>|| >;>;<a href="https://dineshonjava.com/servletconfig-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>
</div>
<div class="wp-post-navigation"> 
									 <div class="wp-post-navigation-pre"> 
									 <a href="https://dineshonjava.com/httpsession-interface/">Previous</a> 
									 </div> 
									 <div class="wp-post-navigation-next"> 
									 <a href="https://dineshonjava.com/servletconfig-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: '262'});
});
</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…