5.6 Changing the Page According to How the User Got ThereThe Referer header designates the location of the page users were on when they clicked a link to get to the current page. If users simply type the address of a page, the browser sends no Referer at all and request.getHeader("Referer") returns null . This header enables you to customize the page depending on how the user reached it. For example, you could use this header to do the following:
Listing 5.5 shows a servlet that uses the Referer header to customize the image it displays. If the address of the referring page contains the string "JRun," the servlet displays the logo of Macromedia JRun. If the address contains the string "Resin," the servlet displays the logo of Caucho Resin. Otherwise, the servlet displays the logo of Apache Tomcat. The servlet also displays the address of the referring page. Listing 5.6 shows the HTML pages used to link to the servlet. We created three identical pages named JRun-Referer.html , Resin-Referer.html , and Tomcat-Referer.html ; the servlet uses the name of the referring page, not form data, to distinguish among the three. Recall that HTML pages are placed in the top-level directory of your Web application (or an arbitrary subdirectory thereof), whereas servlet code is placed in a subdirectory of WEB-INF/classes that matches the package name. So, for example, with Tomcat and the default Web application, the HTML pages are placed in install_dir /webapps/ROOT/request-headers/ and accessed with URLs of the form http://hostname/request-headers/Xxx-Referer.html . Figures 5-6 through 5-9 show some representative results. Listing 5.5 CustomizeImage.javapackage coreservlets; import java.io.*; import javax.servlet.*; import javax.servlet.http.*; /** Servlet that displays referer-specific image. */ public class CustomizeImage extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); String referer = request.getHeader("Referer"); if (referer == null) { referer = "<I>none</I>"; } String title = "Referring page: " + referer; String imageName; if (contains(referer, "JRun")) { imageName = "jrun-powered.gif"; } else if (contains(referer, "Resin")) { imageName = "resin-powered.gif"; } else { imageName = "tomcat-powered.gif"; } String imagePath = "../request-headers/images/" + imageName; String docType = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 " + "Transitional//EN\">\n"; out.println(docType + "<HTML>\n" + "<HEAD><TITLE>" + title + "</TITLE></HEAD>\n" + "<BODY BGCOLOR=\"#FDF5E6\">\n" + "<CENTER><H2>" + title + "</H2>\n" + "<IMG SRC=\"" + imagePath + "\">\n" + "</CENTER></BODY></HTML>"); } private boolean contains(String mainString, String subString) { return(mainString.indexOf(subString) != -1); } } Listing 5.6 JRun-Referer.html (identical to Tomcat-Referer.html and Resin-Referer.html) <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML><HEAD><TITLE>Referer Test</TITLE></HEAD> <BODY BGCOLOR="#FDF5E6"> <H1 ALIGN="CENTER">Referer Test</H1> Click <A HREF="/servlet/coreservlets.CustomizeImage">here</A> to visit the servlet. </BODY></HTML> Figure 5-6. The CustomizeImage servlet when the address of the referring page contains the string "JRun."
Figure 5-9. The CustomizeImage servlet when the Referer header is missing. When using the Referer header, always handle the case in which the result of getHeader is null .
Figure 5-7. The CustomizeImage servlet when the address of the referring page contains the string "Resin."
Figure 5-8. The CustomizeImage servlet when the address of the referring page contains neither "JRun" nor "Resin."
|