2016-02-11 104 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