码迷,mamicode.com
首页 > Web开发 > 详细

JSP内置对象

时间:2016-12-21 23:53:53      阅读:266      评论:0      收藏:0      [点我收藏+]

标签:不能   cat   记录   value   car   取数   div   依次   请求   

1.request

服务器端接收参数。

1)给请求的对象添加数据用request.setAttribute(key,value)

2)从请求的对象中取出数据是request.getAttribute(key)

3)接收cookie(一些网站的记录存储在本地,类似淘宝的记住用户登录)

cookie存的是key,value,ck.getName()--->key

<%
//检查Cookie
//获得Cookie集合
Cookie[] cks=request.getCookies();
for(Cookie ck:cks)
{
    out.write(ck.getName()+"="+URLDecoder.decode(ck.getValue())+"<br>");
}
%>

2.response

服务器端发送数据,浏览器接收。

1)发送信息时,不缓存,立即向浏览器发送。

2)发送cookie

//创建cookie
        Cookie ck = new Cookie("cardid",cardid);
        ck.setMaxAge(10 *24 *60*60); //设置过期时间
        
        response.addCookie(ck); //发送
        String username="张三";
        //对中文进行转码
        Cookie ck1 = new Cookie("username",URLEncoder.encode("张三"));
        response.addCookie(ck1);

3.session

会话对象,记住当前用户信息。

1)setAttribute("key",object)

2)getAttribute("key") ,返回的是对象的属性值

跳转页面之后,request不能获取数据,但是session可以得到数据

<%
//判断session
String cardid=session.getAttribute("cardid").toString();
out.write("cardid="+cardid);
%>
<a href="Testlogin.jsp">测试是否已成功登录的页面</a>
<%
Object obj=session.getAttribute("cardid");
if(obj==null)
{
    out.print("你没有登录");
    response.setHeader("refresh", "2;url=loginin.jsp");
}
else
{
    out.print("cardid="+obj.toString());
    //销毁所有session
    //session.invalidate();
    //移除某个属性
    session.removeAttribute("cardid");

}
%>

设置session超时时间  setMaxInactiveInterval(秒数) 默认tomcat的session20分钟,超过20分钟,没有请求发送给服务器,session就会失效
时间是重置的,在未达到请求时间时,依次过来的请求,都会重设时间

4.application

是服务器共享的对象

//获取application属性
int count=Integer.parseInt(application.getAttribute("count").toString());
out.print("<br>网站计时器:"+count++);
application.setAttribute("count", count);
%>

 

JSP内置对象

标签:不能   cat   记录   value   car   取数   div   依次   请求   

原文地址:http://www.cnblogs.com/miracle-0807/p/6209403.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!