In this tutorial you will learn about the Servlet SendRedirect and its application with practical example.
SendRedirect in Servlet
The word send redirect saying everything that this method is used to redirect the response to another resources such as jsp, servlet, html file. This method can accept relative URL as well as absolute URL. This is the method of HttpServletResponse interface.
When to use sendRedirect() method:
- For the redirection of client request to another Website.
- For the redirection of errors to another resources such as servlet , JSP or Html file.
- For sending any existing HTML form on the server to client.
Difference Between forward() and sendRedirect() method
These two methods are commonly used to send the request to another resources but there are some difference between both method. Which are given below.
- forward() method execute at server side but sendRedirect() method execute at client side.
- forward() method is used to sends the same request and response to another resources but sendRedirect() method sends always new request.
- forward() method can transfer the resources only within server but sendRedirect() method can transfer the resources within and outside the server.
- forward() method consume only one call but sendRedirect() consume Two request and response calls.
- We can not see forwarded message but we can see redirected address.
- The forward() method is faster than sendRedirect().
Syntax:-
1 |
public void sendRedirect(String URL)throws IOException; |
Example:-
1 |
response.sendRedirect("http://www.w3adda.com"); |
Let’s understand it by an simple example. The required files are.
welcome.html :-
Using this file we will send the request to servlet.
SearchDemo.java :-
This is the servlet file where we have used sendRedirect() method. Check below code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class SearchDemo extends HttpServlet { /** * Processes requests for both HTTP <code>GET</code> and <code>POST</code> * methods. * * @param request servlet request * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); } // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code."> /** * Handles the HTTP <code>GET</code> method. * * @param request servlet request * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); String name=request.getParameter("searchName"); response.sendRedirect("https://www.google.co.in/#q="+name); } /** * Handles the HTTP <code>POST</code> method. * * @param request servlet request * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); } /** * Returns a short description of the servlet. * * @return a String containing servlet description */ @Override public String getServletInfo() { return "Short description"; }// </editor-fold> } |
Output :-
Check below Images for output of this sample code.
This is the output of welcome.html file, it will send request to servlet on click of Search button.
Below image is the output of sendRedirect() method of Servlet