servlet的如何创建供所有用户使用session变量并维护其值

jinsanqi 2003-05-23 10:52:51
比如有个记数器变量,要记录访问量.怎么创建这个使所有用户都能使用的session值,然后怎么维护它呢?

请高手们指点迷津!谢谢!
...全文
34 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
jinsanqi 2003-05-25
  • 打赏
  • 举报
回复
to: pigo()
多谢兄弟帮忙!
小弟是想做一棵MVC的树用把树放到session里,这样就不用每次都去数据库取了.喜欢探讨的话,等我做好了,发给你.
谢谢!
jinsanqi 2003-05-24
  • 打赏
  • 举报
回复
哇,不会吧!高手全修假了???
pigo 2003-05-24
  • 打赏
  • 举报
回复
完整的doGet:

public void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
response.setContentType(CONTENT_TYPE);
PrintWriter out = response.getWriter();
ServletContext application = null;
application = getServletContext();
java.lang.String count;
count = (String)application.getAttribute("count");
if (count !=null){
count=String.valueOf(Integer.parseInt(count)+1);
application.setAttribute("count",count);
}else{
count="1";
application.setAttribute("count",count);
}
out.println("<html>");
out.println("<head><title>appt</title></head>");
out.println("<body bgcolor=\"#ffffff\">");
out.println("已经有:"+count+"人次访问过此页面!!!<br>");
out.println("<p>The servlet has received a GET. This is the reply.</p>");
out.println("</body></html>");
}

cxy_ll 2003-05-24
  • 打赏
  • 举报
回复
用application变量
pigo 2003-05-24
  • 打赏
  • 举报
回复
已经调试通过:

ServletContext application = null;
application = getServletContext();
java.lang.String count;
count = (String)application.getAttribute("count");
if (count !=null){
count=String.valueOf(Integer.parseInt(count)+1);
application.setAttribute("count",count);
}else{
count="1";
application.setAttribute("count",count);
}

81,094

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧