Response redirection

One disadvantage of forwarding a request as described in the previous section is that requests can only be forwarded to other servlets or JSPs in the same context. If we need to direct the user to a page on a different context (deployed in another WAR file in the same server or deployed in a different server) we need to use the HttpServletResponse.sendRedirect() method.

To illustrate response redirection, let's develop a simple web application that asks the user to select their favorite search engine, then directs the user to his/her search engine of choice. The HTML page for this application would look like this:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html> 
    <head> 
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
        <title>Response Redirection Demo</title> 
    </head> 
    <body> 
        <form method="post" action="responseredirectionservlet"> 
            <p>Please indicate your favorite search engine.</p> 
            <table> 
                <tr>            
<td><input type="radio" name="searchEngine"

value="http://www.google.com">Google</td>

</tr>

<tr>

<td><input type="radio" name="searchEngine"

value="http://www.bing.com">Bing</td>

</tr>

<tr>

<td><input type="radio" name="searchEngine"

value="http://www.yahoo.com">Yahoo!</td>

</tr>
<tr> <td><input type="submit" value="Submit" /></td> </tr> </table> </form> </body> </html>

The HTML form in the previous markup code contains three radio buttons, the value for each of them is the URL for the search engine corresponding to the user's selection. Notice how the value for the name attribute of each radio button is the same, namely "searchEngine". The servlet will obtain the value of the selected radio button by calling the request.getParameter() method and passing the string "searchEngine" as a parameter, as is demonstrated in the following code:

package net.ensode.javaee8book.responseredirection; 
 
import java.io.IOException; 
import java.io.PrintWriter; 
import javax.servlet.annotation.WebServlet; 
 
import javax.servlet.http.HttpServlet; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 
 
@WebServlet(urlPatterns = {"/responseredirectionservlet"}) 
public class ResponseRedirectionServlet extends HttpServlet { 
 
    @Override 
    protected void doPost(HttpServletRequest request,  
HttpServletResponse response) throws IOException { String url = request.getParameter("searchEngine"); if (url != null) { response.sendRedirect(url); } else { PrintWriter printWriter = response.getWriter(); printWriter.println("No search engine was selected."); } } }

By calling request.getParameter("searchEngine"), the preceding code assigns the URL of the selected search engine to the url variable. Then (after checking for null, in case the user clicked on the submit button without selecting a search engine), it directs the user to the selected search engine by calling response.sendRedirect() and passing the url variable as a parameter.

The web.xml file for this application should be fairly straightforward and is not shown (it is part of this book's code download).

After packaging the code and deploying it, we can see it in action by typing a URL similar to the following in the browser: http://localhost:8080/responseredirection/.

After clicking the Submit button, the user is directed to their favorite search engine.

It should be noted that redirecting the response as illustrated previously creates a new HTTP request to the page we are redirecting to, therefore any request parameters and attributes are lost:

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.217.254.118