to include another resource as
part of the processing of this page -->
<jsp:include page="anotherPage.jsp" flush="true"/>
清单 34 显示了这些导航方法在 servlet 中的一些例子。
清单 34. servlet 中的编程式导航
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
...
// Redirecting to another resource
response.sendRedirect("anotherResource.jsp");
...
}
// This code snippet shows the use of a RequestDispatcher to forward to another resource
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
...
// Get a RequestDispatcher instance
RequestDispatcher rd = getServletContext().getRequestDispatcher ("anotherResource.jsp");
// Forward the request
rd.forward(request, response);
...
}
// This code snippet shows the use of a RequestDispatcher to include another resource
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
...
// Get a RequestDispatcher instance
RequestDispatcher rd = getServletContext().getRequestDispatcher ("anotherResource.jsp");
// Forward the request
rd.include(request, response);
// Continue processing the request
...
}
走上开放之路: .NET开发人员的J2EE基础(下)(11)
时间:2011-04-09 IBM David Carew
Cookie
在 ASP.NET 中,cookie 的处理使用了 HttpRequest 和 HttpResponse 对象的 Cookies 属性。这个属性表示 HttpCookie 对象的一个集合,集合中的每个成员分别表示一个单独的 cookie。
在 J2EE 中,cookie 使用 Cookie 类来表示,并且可通过 HttpServletRequest 和 HttpServletResponse 接口来访问它们。现有的 cookie 使用 HttpServletRequest 接口来 访问,新的 cookie 使用 HttpServletResponse 接口来创建。
清单 35 演示了如何在 J2EE 应用程序中使用 cookie。你可以在 Java Servlet 或 JSP 页面的 scriptlet 中使用这些代码片断。
清单 35. 在 J2EE Web 应用程序使用 cookie
// Create a cookie using the HttpServletResponse interface
// The first parameter of the constructor is the name of the cookie
// and the second parameter is the cookie''s value
Cookie myCookie = new Cookie("myCookie", "someValue");
// Set the age of the cookie - by default cookies will be stored in memory
// by the browser and will be destroyed when the user closes the browser
// The setMaxAge() methods takes an integer as its parameter which represents
// the age in seconds. In this example we''re specifying an age of 24 hours for the cookie
myCookie.setMaxAge(86400);
// Add cookie to the response
response.addCookie(myCookie);
// Here''s an example of retrieving cookies from the HttpServletRequest interface
// Note that the cookies are returned as an array of Cookie objects
Cookies[] myCookies = request.getCookies();
// The getCookies method will return null if there are no cookies
if (myCookies != null) {
for (int i = 0; i < myCookies.length; i++) {
Cookie ea
|