Servlets - SPLessons
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Servlet Redirect

Servlet Redirect

shape Description

Servlet Redirect, HttpServletResponse is an interface that consists of SendRedirect (), the functionality of this method is redirecting the response to other pages such as JSP , HTML . It works at browser side because it uses URL bar and it always sends an new request. It will be used within and outside the server. Following is the syntax to declare SendRedirect(). [java]response.sendRedirect("URL")[/java]

shape Conceptual figure

Servlet Redirect, The figure below demonstrate the Servlet Redirect method in web applications The HTML output of servlet1 (Servlet Program) will be discarded and only the HTML output of servlet2 will be displayed on the browser window as a response.

Difference between forward() and sendRedirect() method

shape Table

forward() sendRedirect()
Performs forward() mode of servlet chaining Performs sendRedirect() mode of communication
The servlet program communicates with destination web resource program directly. The servlet program communicates with destination servlet program by having network round trip with browser window
The servlet program and destination web resource program uses same request and response object, so request is visible and accessible in destination web resource program. The servlet program and destination web resource program will not use same request and response object. So request data is not visible and accessible in destination program. Then sendRedirect sends the new request.
During forward request URL is displayed in the browser window and will not be changed. During sendRedirect operation the URL in the browser window and will be changed.

shape Example

index.html [html] <html> <form action="./welcome"> <input type="submit" value="click here for splessons"> </form> </html> [/html] Here just created the submit button that is "click here for splessons", given URL that is "./welcome", make sure to this URL should be match with web.xml file URL. web.xml [xml] <web-app> <servlet> <servlet-name>DemoSendRedirect</servlet-name> <servlet-class>sendredirect.SimpleSendRedirect</servlet-class> </servlet> <servlet-mapping> <servlet-name>DemoSendRedirect</servlet-name> <url-pattern>/welcome</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>welcome.html</welcome-file> </welcome-file-list> </web-app> [/xml] As discussed previously, that is DemoSendRedirect should be same in both servlet as well in servlet-mapping. SimpleSendRedirect.java [java] 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 SimpleSendRedirect extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); String tutorialName = request.getParameter("name"); response.sendRedirect("http://www.splessons.com"); out.flush(); out.close(); } } [/java] Servlet Redirect, Here the purpose of setContentType("text/html") is that, It basically tells the client what content type it is so that it knows what to do with it. Print formatted representations of objects to a text-output stream. sendRedirect() method of HttpServletResponse interface can be used to redirect response to another resource, it may be servlet, jsp or html file. Output: By compiling the program following output will be displayed. When clicked on search button the required page will be displayed as follows.

Example on sendRedirect for search tutorial in Splessons

shape Example

welcome.html [html] <html> <form action="./welcome"> <h2>Enter your tutorial name<input type="text"name="name/"> <input type="submit"value="search"> </form> </html> [/html] Here created a text box to search the required page and created submit button to give the input to the server. web.xml [xml] <servlet> <servlet-name>DemoSendRedirect</servlet-name> <servlet-class>sendredirect.SimpleSendRedirect</servlet-class> </servlet> <servlet-mapping> <servlet-name>DemoSendRedirect</servlet-name> <url-pattern>/welcome</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>welcome.html</welcome-file> </welcome-file-list> </web-app> [/xml] As discussed servlet name should be same and URL should be match with HTML form. DemoSendRedirect.java [java] package sendredirect; 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 SimpleSendRedirect extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); String tutorialName = request.getParameter("name"); response.sendRedirect("http://www.splessons.com/lesson/"+tutorialName+"+tutorial/"); out.flush(); out.close(); } } [/java] The doGet() method is utilized to send parameters to an URL along with the header information. The sendRedirect() method of HttpServletResponse interface can be used to redirect response to another resource, it may be servlet, jsp or html file. Output: When compiled the program following output will be displayed. When clicked on search button required page will be displayed.

Difference between doGet() and doPost()

  • In doGet() parameters are sent along with header information where as in doPost() parameters are sent in the body.
  • The doGet() will have limitation to send the data where as no limitation for doPost() .
  • In doGet() parameters are not encrypted where as in doPost() parameters are encrypted.
  • The doGet() method is utilized to get some information from the server and the doPost() is utilized to post some information to the server.

Summary

shape Key Points

  • The doGet() method is utilized to send parameters to an URL along with the header information.
  • The doPost() method is utilized to send huge information to an URL.
  • Here code should be called from HTML page.