2014.3.10

本文介绍Servlet中的重定向与转发技术,并演示如何利用计数器记录页面访问次数。对比了response.sendRedirect()与request.getRequestDispatcher().forward()的区别,前者用于重定向且不带参数,后者用于转发并可携带参数。此外,还讲解了如何使用Application和Session对象来实现计数器功能。

response.sendRedirect();重定向 不需要传参的时候

request.getRequestDispatcher("url").forward(req,resp);传参的时候使用

计数器的使用 counter

Integer counter=(integer) application.getAttribute("counter");

if(counter==null){//如果页面第一次被访问则为空

counter=1;

}

else

{counter++;}

//将变量放入Application域,在Serlect中他是ServletContext

Appliction.setAttribute("counter",counter);

String realpath=application.getRealpath("/");此方法可以获得绝对路径

<body>

当前页面被访问的次数

</body>

PS:jsp中的Application对象存放数据是所有访问该服务器所共享的,而session对象是一对一的,不同浏览器会重新计算,一次会话包含对个请求


session代码

    <% Integer counter=(Integer) session.getAttribute("counter");
if(counter==null){
counter=1;
}
else{counter++;}


session.setAttribute("counter",counter);
String id=session.getId();
%>

三个对象

<%request.setAttribute(“counter”,10);

session.setAttrebute("counter",100);

application.setAttribute("counter",200);%>

request:<%=(Integer)request.getAttrebute("counter")%>

session:<%=(Integer)session.getAttrebute("counter")%>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值