servlet中request session servletContext 测试示例

[color=green]基本概念:.request session servletContext对象保持的数据范围 区别
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]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值