Tuesday, 29 September 2015

SendRedirect in servlet

SendRedirect in servlet

The sendRedirect() method of HttpServletResponse 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.
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.

Difference between forward() and sendRedirect() method

There are many differences between the forward() method of RequestDispatcher and sendRedirect() method of HttpServletResponse interface. They are given below:
forward() methodsendRedirect() 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");

Syntax of sendRedirect() method

  1. public void sendRedirect(String URL)throws IOException;  

Example of sendRedirect() method

  1. response.sendRedirect("http://www.javatpoint.com");  

Full example of sendRedirect method in servlet

In this example, we are redirecting the request to the google server. Notice that sendRedirect method works at client side, that is why we can our request to anywhere. We can send our request within and outside the server.
DemoServlet.java
  1. import java.io.*;  
  2. import javax.servlet.*;  
  3. import javax.servlet.http.*;  
  4.   
  5. public class DemoServlet extends HttpServlet{  
  6. public void doGet(HttpServletRequest req,HttpServletResponse res)  
  7. throws ServletException,IOException  
  8. {  
  9. res.setContentType("text/html");  
  10. PrintWriter pw=res.getWriter();  
  11.   
  12. response.sendRedirect("http://www.google.com");  
  13.   
  14. pw.close();  
  15. }}  

Creating custom google search using sendRedirect

In this example, we are using sendRedirect method to send request to google server with the request data.
index.html
  1. <!DOCTYPE html>  
  2. <html>  
  3. <head>  
  4. <meta charset="ISO-8859-1">  
  5. <title>sendRedirect example</title>  
  6. </head>  
  7. <body>  
  8.   
  9.   
  10. <form action="MySearcher">  
  11. <input type="text" name="name">  
  12. <input type="submit" value="Google Search">  
  13. </form>  
  14.   
  15. </body>  
  16. </html>  
MySearcher.java
  1. import java.io.IOException;  
  2. import javax.servlet.ServletException;  
  3. import javax.servlet.http.HttpServlet;  
  4. import javax.servlet.http.HttpServletRequest;  
  5. import javax.servlet.http.HttpServletResponse;  
  6.   
  7. public class MySearcher extends HttpServlet {  
  8.     protected void doGet(HttpServletRequest request, HttpServletResponse response)  
  9.             throws ServletException, IOException {  
  10.   
  11.         String name=request.getParameter("name");  
  12.         response.sendRedirect("https://www.google.co.in/#q="+name);  
  13.     }  
  14. }  

Output

sendredirect in servlet example 1
sendredirect in servlet example 2

No comments:

Post a Comment