[color=green]基本概念:.request session servletContext对象保持的数据范围 区别
1.request :保持的数据 仅在下一个request中得到用 在页面转发时共享数据
2.session 作用域表示在一个用户与服务器会话范围之内 直到客户端关闭或session超时
3.servletContext中对象存取的数据是个全局的,生命周期也是长久的,直到web服务器关闭[/color]
示例:
[color=blue]在web.xml中的配置:[/color]
结果如下:
firfox中显示:
[img]http://dl.iteye.com/upload/attachment/454444/e4461c13-494d-3baf-8669-297c347250a2.png[/img]
控制台中输出:
[img]http://dl.iteye.com/upload/attachment/454446/e724a443-6e0e-3d04-86aa-c906c7a6ef10.png[/img]
1.request :保持的数据 仅在下一个request中得到用 在页面转发时共享数据
2.session 作用域表示在一个用户与服务器会话范围之内 直到客户端关闭或session超时
3.servletContext中对象存取的数据是个全局的,生命周期也是长久的,直到web服务器关闭[/color]
示例:
package com.qingbyqing.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
//servlet中对象作用域 测试: ServletRequest Session ServletContext
public class SessionScopeTest extends HttpServlet {
public void service(HttpServletRequest request, HttpServletResponse response)
throws IOException {
System.out.println("_______________________________");
PrintWriter writer = response.getWriter();
writer.println("<html> <head> </head> <body>");
HttpSession session = request.getSession();
ServletContext servletContext = this.getServletContext();
Integer iRequest = (Integer) request.getAttribute("count");
Integer iSession = (Integer) session.getAttribute("count");
Integer iContext = (Integer) servletContext.getAttribute("count");
if (iRequest == null) {
iRequest = new Integer(1);
request.setAttribute("count", iRequest);
} else {
System.out.println(iRequest.toString());
int intValue = iRequest.intValue();
intValue++;
iRequest = new Integer(intValue);
request.setAttribute("count", iRequest);
}
System.out.println("request: " + (iRequest.intValue()));
writer.println("</br> request: " + (iRequest.intValue()));
if (iSession == null) {
iSession = new Integer(1);
request.getSession().setAttribute("count", iSession);
} else {
int intValue = iSession.intValue();
intValue++;
iSession = new Integer(intValue);
request.getSession().setAttribute("count", iSession);
}
System.out.println("Session: " + (iSession.intValue()));
writer.println("</br> Session: " + (iSession.intValue()));
if (iContext == null) {
iContext = new Integer(1);
this.getServletContext().setAttribute("count", iContext);
} else {
int intValue = iContext.intValue();
intValue++;
iContext = new Integer(intValue);
this.getServletContext().setAttribute("count", iContext);
}
System.out.println("sevletContext: " + (iContext.intValue()));
writer.println("</br> sevletContext: " + (iContext.intValue()));
writer.println("</body></html>");
}
}
[color=blue]在web.xml中的配置:[/color]
<servlet>
<description>this is a session scope test</description>
<display-name>this is a session scope test</display-name>
<servlet-name>SessionScopeTest</servlet-name>
<servlet-class>com.qingbyqing.servlet.SessionScopeTest</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>SessionScopeTest</servlet-name>
<url-pattern>/com.qingbyqing.servlet/SessionScopeTest</url-pattern>
</servlet-mapping>
结果如下:
firfox中显示:
[img]http://dl.iteye.com/upload/attachment/454444/e4461c13-494d-3baf-8669-297c347250a2.png[/img]
控制台中输出:
[img]http://dl.iteye.com/upload/attachment/454446/e724a443-6e0e-3d04-86aa-c906c7a6ef10.png[/img]