Go-ForIt 记事:eXtreme DragonSlayers 专题报告,第 3 部分: 会话管理、servlet 和维护状态
2009-11-06 00:00:00 来源:WEB开发网通过调用 visits.getNumberOfVisits() 取回新的计数。
如果页面请求计数大于或等于允许的最大页面计数,它将再次更改 greeting。
既然 visit 对象具有它需要保存到自身的全部信息,而且我们需要显示的内容被保存在本地变量中,那么它将再次测试计数。
如果小于最大页面请求计数,则新的 visit 对象被保存到会话中。 否则整个会话都将被破坏( session.inactivate() )。
然后将本地变量打印到屏幕上。
注意:突出显示的代码段是创建、测试和操作会话部分。
SessionTest.java 代码package com.sessiontest;
import javax.servlet.*;
import javax.servlet.http.*;
public class SessionTest extends javax.servlet.http.HttpServlet {
public void doGet(javax.servlet.http.HttpServletRequest request,
javax.servlet.http.HttpServletResponse response)
throws javax.servlet.ServletException, java.io.IOException {
performTask(request, response);
}
public void doPost(javax.servlet.http.HttpServletRequest request,
javax.servlet.http.HttpServletResponse response)
throws javax.servlet.ServletException, java.io.IOException {
performTask(request, response);
}
public void init() {}
public void performTask(javax.servlet.http.HttpServletRequest req,
javax.servlet.http.HttpServletResponse res) {
/* Let's first initiate a few necessary objects.. */
HttpSession session = null;
VisitInformation visits = null;
java.io.PrintWriter out = res.getWriter();
/* Set a few variables */
int numberOfVisits = 0;
int maxNumberOfVisits = 5;
String greetingText = null;
String charS = ""; // This will only be used to make a word plural...
/* Check to see if the session was started already
and if not create one */
session = req.getSession(true);
/* Create a new visits object if one
was not saved in the session */
visits = (VisitInformation) session.getAttribute("visits");
if (visits == null){
visits = new VisitInformation();
}
/* Reset some of the values in the visits object as
needed and grab some variables from it */
if(!session.isNew()){
/* session.isNew() will return true only the FIRST time you access
the session so this block will only happen from the 2nd time on...*/
visits.setGreetingText("What? Still here?");
}
visits.incrementVisitCount();
numberOfVisits = visits.getNumberOfVisits();
if(numberOfVisits>=maxNumberOfVisits){
visits.setGreetingText("You come around too much. Leave me alone. Bye, bye.");
}
greetingText = visits.getGreetingText();
/* Save the final visits object back to the session or kill
the session if we have reached the max increment number */
if(numberOfVisits<maxNumberOfVisits){
session.setAttribute("visits", visits);
}
else{
session.invalidate();
}
/* Print away... */
if (numberOfVisits!=1){
charS = "s";
}
out.println("<h3>" + greetingText +
"</h3>You have requested this servlet <b>" +
numberOfVisits + "</b> time" + charS +".");
}
}
}
VisitInformation.java 代码package com.sessiontest;
public class VisitInformation {
public int numberOfVisits = 0;
public java.lang.String greetingText = "First time, eh?...";
public VisitInformation() {
super();
}
public String getGreetingText() {
return greetingText;
}
public int getNumberOfVisits() {
return numberOfVisits;
}
public void incrementVisitCount() {
setNumberOfVisits(++numberOfVisits);
}
public void setGreetingText(String newGreeting) {
greetingText = newGreeting;
}
public void setNumberOfVisits(int newNumberOfVisits) {
numberOfVisits = newNumberOfVisits;
}
}
- ››Google运作经理Bryan Power给出的GOOGLE求职意见
- ››Google用户体验的十大设计原则
- ››Google Analytics(分析)能为网站带来什么
- ››Google goggles图片搜索 如何优化一个wap网站
- ››Google Docs将增加iPhone和Android编辑功能
- ››Google Android操作系统内核编译图文教程
- ››google map api 与jquery结合使用--控件,监听器...
- ››google map api 与jquery结合使用(2) --标注,浮...
- ››google map api 与jquery结合使用(3) --图标样式...
- ››Google 首页代码分析及简评
- ››Got a packet bigger than ‘max_allowed_packet’...
- ››Google财经更新iPhone和Android版本
赞助商链接