当前位置: 首页 > 图文教程 > 网络编程 > JSP > Tips for Server-Side Redirection

JSP
JSP中有关时间和日期类的使用
JSP中引用JavaBean组件
如何在JSP中添加自己的Tag
使用JSP/Servlet上载文件
关于Java Servlet的Filter 技术
JSP数据库连接方式总结
不需ODBC可由IP地址与端口号建立与SQLSERVER的连接
J2ME学习札记(三)
java 设计模式之Observer
在JSP中如何从数据流中取得图片数据并按随意位置显示
三级级联下拉菜单实现
网页中在图层中部分显示图片(窗口形式)
MySQL5.0中文问题及JDBC数据库连接和JSP汉字编码问题解决方法总结
XSLT合并模板简述
JSP入门学习笔记
Structs中基本配置入门
利用jConfig获取xml文件中的配置信息
关于Applet做数字签名,授予访问本地资源(原创)
在JSP开发中使用jdom解析临时存放数据的XML文件
关于Hhtml嵌入打成jar包的Applet方法

JSP 中的 Tips for Server-Side Redirection


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-04   浏览: 38 ::
收藏到网摘: n/a

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 [email protected]