Back to: Java Servlets Tutorials
Request Redirection in Servlet
In this article, I am going to discuss Request Redirection in Servlet. Please read our previous article where we discussed Servlet Chaining in Java-based Web Application. At the end of this article, you will understand the following pointers.
- Request Redirection in Servlet: sendRedirect() method
- Difference Between the Forward Mechanism and the Send Redirect Mechanism
- An example demonstrating the sendRedirect method in servlet
- Creating custom google search using sendRedirect
Request Redirection in Servlet: sendRedirect() method
The process of bypassing the request from one web application to another web application is called Request Redirection. In web applications, we are able to achieve Request Redirection by using Send Redirect Mechanism.
To perform Request Redirection, if we use Send Redirect Mechanism no need to use Hyperlinks, not required to set status code and Response Header values to the Response Format, but we need to use the following method:
Syntax: public void sendRedirect(String url)
The sendRedirect() method is used to redirects the response to another resource. It is the method of HttpServletResponse interface. It makes the browser to create a new request to get to the resource. The client can directly see the new URL in the browser. It accepts both the relative and absolute URL so that it can work both inside and outside the server.
Difference Between the Forward Mechanism and the Send Redirect Mechanism
Forward Mechanism | Send Redirect Mechanism |
In web applications, the Forward Request Dispatching mechanism can be used to provide the communication between two resources which must be available at the same server. | In web applications, the Send Redirect mechanism can be used to provide the communication between two resources which may be available at the same server or at two different servers. |
In the case of the Forward mechanism, one request is sufficient to establish communication between two resources. | In the case of the Send Redirect Mechanism, we need requests to establish communication between two web resources. |
It works on the request object. | It works on the response object. |
The user can see the new URL while request dispatch gets the resource in the same request and the URL does not change. | Redirection makes the client creates a new request to get to the resource. |
An example demonstrating the sendRedirect method in servlet
In this example, we are redirecting the request to the website. The sendRedirect method works at the client-side so that we can send or request anywhere within and outside the server.
import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class DemoServlet extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); try { response.sendRedirect("https://dotnettutorials.net/course/java-servlets-tutorials/F"); } finally { out.close(); } } }
Creating custom google search using sendRedirect
In this example, in the “index.html” form we are taking the user input (data you want to search) and storing them in the parameter’s “name”. In the “MyServlet.java” we are redirecting the request to the Google server. Here, we are using the sendRedirect() method to send requests to the google server with the request data.
index.html
<!DOCTYPE html> <html> <head> <meta charset="ISO-8859-1"> <title>sendRedirect example</title> </head> <body> <form action="MyServlet"> <input type="text" name="name"> <input type="submit" value="Google Search"> </form> </body> </html>
MyServlet.java
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 MyServlet 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); } }
Output
In the next article, I am going to discuss the ServletConfig Interface. Here, in this article, I try to explain Request Redirection in Servlet. I hope you enjoy this Request Redirection in Servlet article.