RSS
热门关键字:  下载  cms  模版  开源  dedecms
当前位置 :| 主页 > 站长学院 > JSP教程 >

Tips for Server-Side Redirection

来源:JR 作者:未知 时间:2006-07-25 Tag: 点击:
Use these two server-side redirection techniques to control which servlet/JSP pages or URLs your users see.

Author: Budi Kurniawan
Date: March 20, 2002
From: www.fawcette.com

Redirection is an important technique in Web programming. With redirection, you forward control to another servlet/JSP page or redirect the Web browser (the user) to a new URL. However, it is also common to redirect the user to the same page. For example, to check if the browser's cookie support is enabled, you send a cookie to the browser and then redirect the browser to the same page.

Redirection can happen either on the server side or the client (browser) side. Redirection on the server side happens because of the server-side code on the servlet/JSP page. Redirection on the client side can be accomplished by sending JavaScript code or metadata in the HTML page sent to the browser. For now, let's take a look at the two server-side redirection techniques. 

In servlet/JSP programming, server-side redirection can be achieved using either the forward method of the javax.servlet.RequestDispatcher interface, or the sendRedirect method of the javax.servlet.http.HttpServletResponse interface.

Using the RequestDispatcher Interface's Forward Method
To use the forward method of the RequestDispatcher interface, the first thing to do is obtain a RequestDispatcher object. The servlet technology provides three ways of doing so:
  1. By using the getRequestDispatcher method of the javax.servlet.ServletContext interface, passing a String containing the path to the other resource. The path is relative to the root of the ServletContext. 

  2. By using the getRequestDispatcher method of the javax.servlet.ServletRequest interface, passing a String containing the path to the other resource. The path is relative to the current HTTP request. 

  3. By using the getNamedDispatcher method of the javax.servlet.ServletContext interface, passing a String containing the name of the other resource. 

Once you get a RequestDispatcher object, using the forward method is easy. The signature of the forward method is:

  1. public void forward(javax.servlet.ServletRequest request, 
  2. javax.servlet.ServletResponse response)
  3. throws javax.servlet.ServletException, java.io.IOException

Note, however, that you can call the forward method only if no output has been flushed out to the client. If the current page buffer is not empty, you must first clear the buffer before invoking the forward method. Otherwise, an IllegalStateException will be thrown. The forward method can also be used to forward the request to a static content. 

Beginners to servlet/JSP programming often encounter confusion when trying to obtain a RequestDispatcher object because there is a huge difference between the getRequestDispatcher method of the ServletContext interface and that belonging to the ServletRequest interface. Read on for some tips on avoiding this confusion.

When using the forward method of the RequestDispatcher object to pass control from a servlet called ABCServlet to another servlet named XYZServlet, the easiest is to place the class files of both ABCServlet and XYZServlet in the same directory. This way, ABCServlet can be invoked from the URL http://domain/VirtualDir/servlet/ABCServlet and XYZServlet can be called from the URL http://domain/VirtualDir/servlet/XYZServlet. Using the forward method is then straightforward. You can use the getRequestDispatcher from the ServletRequest interface passing the name of the second servlet. In ABCServlet, you can write this code:

  1.   RequestDispatcher rd = 
  2. request.getRequestDispatcher("SecondServlet");
  3.   rd.forward(request, response);

You don't need to include the / character before XYZServlet. This way is easiest because you don't need to worry about the paths of both servlets at all.

The harder way would be trying to pass this String to the getRequestDispatcher of ServletRequest:

  1. "/servlet/XYZServlet"


If you have to invoke the forward method of a RequestDispatcher object obtained from the getRequestDispatcher of the ServletContext, you need to pass "/VirtualDir/servlet/XYZServlet" as the path argument, such as:

  1.   RequestDispatcher rd =
  2.     getServletContext().getRequestDispatcher(
  3.        "/servlet/XYZServlet");
  4.   rd.forward(request, response);
  5. To use the getNamedDispatcher method, your code would become:
  6.   RequestDispatcher rd =
  7.     getServletContext().getNamedDispatcher(
  8.        "XYZServlet");
  9.   rd.forward(request, response);


When you use the getNamedDispatcher method, you must register the second servlet in your deployment descriptor. Here is an example:

  1. <web-app>
  2.   <servlet>
  3.     <servlet-name>ABCServlet</servlet-name>
  4.     <servlet-class>ABCServlet</servlet-class>
  5.   </servlet>
  6.   <servlet>
  7.     <servlet-name>XYZServlet</servlet-name>
  8.     <servlet-class>XYZServlet</servlet-class>
  9.   </servlet>
  10. </web-app>

If you change the included servlet, you need to restart the Web container for the change to take effect. This is because the included servlet is never invoked directly. Once the included servlet is loaded, its time stamp is never compared again.

If you are forwarding control from a JSP page, you can also use the <jsp:forward> action element, which terminates the execution of the current JSP page and passes control to another resource. Its syntax is: 

  1. <jsp:forward page="relativeURL"/>

For example, <jsp:forward page="OtherPage.jsp"/> is translated into this code in the resulting servlet after the JSP page is parsed:

  1. pageContext.forward("OtherPage.jsp");


Using the HttpServletResponse Interface's sendRedirect Method

The sendRedirect method is much easier to use than the forward method. Its signature is:

  1. public void sendRedirect(java.lang.String location)
  2.    throws java.iio.IOException


This method sends a command to the browser to force the browser to request the URL specified in location. This method can accept absolute or relative URLs. If the argument to this method is a relative URL, the Web container converts it into an absolute URL before sending it to the client. If the location is relative without a leading '/', the Web container interprets it as relative to the current request URI.

For example, you can use this code to redirect the user to www.brainysoftware.com:

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


Which Should You Use?
To produce the most efficient code, you should understand the difference between the two redirection techniques. The forward method works inside the Web container. The sendRedirect method requires a round trip to the client. So the forward method is faster than sendRedirect. However, using the forward method restricts you to redirect only to a resource in the same Web application. The sendRedirect method, on the other hand, allows you to redirect to any URL. Conclusion: Use the forward method if it can solve your problem. Resort to the sendRedirect method only if you can't use the forward method.

About the Author
Budi Kurniawan is an independent consultant in Sydney, Australia, and the author of Java Scalability with Servlet, JSP and EJB, to be published by New Riders in April 2002. He is also one of the developers at BrainySoftware. You can reach him at budi@brainysoftware.com. 
最新评论共有 0 位网友发表了评论
发表评论
评论内容:不能超过250字,需审核,请自觉遵守互联网相关政策法规。
用户名: 密码:
匿名?
注册
热点关注
相关文章