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.
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.
The output of the program is given below:
<!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>
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"); } } }
There are many differences between the forward() method of RequestDispatcher and sendRedirect() method of HttpServletResponse interface. They are given below:
forward() method | sendRedirect() method |
---|---|
The forward() method works at server side. | The sendRedirect() method works at client side. |
It sends the same request and response objects to another servlet. | It always sends a new request. |
It can work within the server only. | It can be used within and outside the server. |
Example: request.getRequestDispacher(“servlet2”).forward(request,response); | Example: response.sendRedirect(“servlet2”); |
MySearcher.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 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); } }
index.html
<!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>
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…