开发学院软件开发Java Go-ForIt 记事:eXtreme DragonSlayers 专题报告,... 阅读

Go-ForIt 记事:eXtreme DragonSlayers 专题报告,第 3 部分: 会话管理、servlet 和维护状态

 2009-11-06 00:00:00 来源:WEB开发网   
核心提示: 通过调用 visits.getNumberOfVisits() 取回新的计数, 如果页面请求计数大于或等于允许的最大页面计数,Go-ForIt 记事:eXtreme DragonSlayers 专题报告,第 3 部分: 会话管理、servlet 和维护状态(8),它将再次更改 greeting,

通过调用 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; 
  } 
} 

上一页  3 4 5 6 7 8 9  下一页

Tags:Go ForIt 记事

编辑录入:爽爽 [复制链接] [打 印]
[]
  • 好
  • 好的评价 如果觉得好,就请您
      0%(0)
  • 差
  • 差的评价 如果觉得差,就请您
      0%(0)
赞助商链接