2016-02-11 182 views
0

嗨我有這個網絡應用程序應該計算在每個輸入中編碼的卡的總和。它是成功的,但數據不會持久。這是我的代碼。使用會話無法保存數據

控制器:

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 

    String BusName = ""; 
    String PlateNumber = ""; 
    String DriverAssigned = ""; 

    try{    
     BusName = request.getParameter("busName"); 
     PlateNumber = request.getParameter("plateNo"); 
     DriverAssigned = request.getParameter("driverAssigned"); 

     String firstString = PlateNumber.substring(0, 2); 
     String secondString = PlateNumber.substring(3, 5); 

     if (firstString.matches(".*[A-Z].*") && secondString.matches(".*\\d.*")){ 

     CalculatorBean beanCalc = BeanFactory.getInstance(BusName, PlateNumber, DriverAssigned); 

     HttpSession session = request.getSession(); 
     session.setAttribute("deJesusBean", beanCalc); 

     RequestDispatcher dispatcher = request.getRequestDispatcher("displayresult.jsp"); 

     dispatcher.forward(request, response); 

我的邏輯模型:

public void compute(){ 
    char x = plateNo.charAt(5); 

     if (x == '1' || x == '2'){ 
      dayOfCoding = "Monday"; 
      mondayCoding = mondayCoding + 1; 
     } 
     else if (x == '3' || x == '4'){ 
      dayOfCoding = "Tuesday"; 
      tuesdayCoding = tuesdayCoding + 1; 
     } 
     else if (x == '5' || x == '6'){ 
      dayOfCoding = "Wednesday"; 
      wednesdayCoding = wednesdayCoding + 1; 
     } 
     else if (x == '7' || x == '8'){ 
      dayOfCoding = "Thursday"; 
      thursdayCoding = thursdayCoding + 1; 
     } 
     else if (x == '9' || x == '0'){ 
      dayOfCoding = "Friday"; 
      fridayCoding = fridayCoding + 1; 
     } 
    } 

,我又把它使用 ${deJesusBean.mondayCoding}

+0

你是什麼意思「它是成功的,但數據不會持續。」? – Vishrant

+0

它需要保存的值,所以如果setAttribute會overwite?我可以使用什麼? – Dex

回答

-1

顯示您需要刪除或無效的會話試試下面的代碼

HttpSession session = request.getSession(); 
if(session.getAttribute("deJesusBean") != null){ 
    session.removeAttribute("deJesusBean"); 
    session.setAttribute("deJesusBean", beanCalc); 
} 
+0

如果你會使會話無效,用戶將能夠繼續,它將被註銷.. :) – Vishrant

+0

在這種情況下只需刪除並創建 –

+0

如何刪除會話?你上面寫的代碼意味着你要從會話中刪除一些屬性並重新設置它,即使會話對象中有一個屬性,並且你正在用'session.setAttribute(「deJesusBean」,beanCalc)設置新值;'然後上一個價值將被覆蓋。 – Vishrant