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;
}
}
- ››Godaddy域名解析使用DNSPOD方法
- ››GOV.CN域名解析修改
- ››Google搜索引擎的奥秘
- ››Google测试搜索结果页面右侧内容更丰富的信息栏
- ››Google Dart精粹:应用构建,快照和隔离体
- ››google的代码审查
- ››google analytics清晰追踪爬虫的爬行信息
- ››Google+中文用户在两千万Google+大军中是少数派
- ››Google AdWords最昂贵点击成本的20种关键词分类
- ››Google运作经理Bryan Power给出的GOOGLE求职意见
- ››Google用户体验的十大设计原则
- ››Google Analytics(分析)能为网站带来什么
更多精彩
赞助商链接