2016-05-15 71 views
0

大家好!HttpSession Java EE

所以我有一個httpsession的小問題,我不知道如何解決它。這是問題。

我有一個類的人有一個set<Product>,我把這個類的人設置爲 這樣的request.getSession。

String email = getRequest()。getParameter(「email」); String credential = getRequest()。getParameter(「credential」);

validEmail(email); 
    validCredential(credential); 

    if(existError()){ 
     setValues(email, credential);; 
     forward(login); 
     return; 
    } 


    PersonService ps = serviceFactory.getService(PersonService.class); 

    boolean success = ps.validatePasswordAndEmail(email, credential); 

    if(success){ 

    Person person = ps.getPersonByEmail(email);      

    getSession().setAttribute("person", person); here you can see the object person into the session  

    forward("/packed.jsp"); 

    }else{ 

     addError("Email or Credential is not valid!"); 
     setValues(email, credential); 
     forward(login); 
     return; 
    } 

之後,如果用戶把自己的正確的信息,他們將來到另一個叫packed.jsp 文件,他們將被記錄。

我的問題是當我編輯登錄到系統的產品的人。讓我告訴我在這裏的代碼

稱爲packed.jsp頁面具有這樣誰登錄點擊 和人,然後他來到這個servlet下面

public class EditProductAction extends Action { 

private String editProduct = "/edit_product.jsp"; 
private String packed = "/packed.jsp"; 

@Override 
public void process() throws Exception {   


    Integer id = Integer.parseInt(getRequest().getParameter("id")); 
    String brand = getRequest().getParameter("brand"); 
    String model = getRequest().getParameter("model"); 
    String name = getRequest().getParameter("name"); 
    String quantity = getRequest().getParameter("quantity"); 
    String color = getRequest().getParameter("color"); 
    String info = getRequest().getParameter("info"); 


    ProductService ps = serviceFactory.getService(ProductService.class); 


    Product product = ps.getProductByID(id); 

    if(name == null){  
     getRequest().setAttribute("product",product); 
     forward(editProduct); 
     return; 
    } 

    product.setBrand(brand); 
    product.setModel(model); 
    product.setName(name); 
    product.setQuantity(Integer.parseInt(quantity)); 
    product.setColor(color); 
    product.setInfo(info); 

    ps.update(product); 


    getResponse().sendRedirect(getRequest().getContextPath()+packed); 


} 

}

,你可以一個BUTTOM看到一切工作正常,但當我回到packed.jsp 它不顯示產品modyfied,但在數據庫中工作正常,我的意思是產品已被modyfied。這裏packed.jsp

<div class="mainDiv"> 
<table border="1px" width="80%"> 
    <tr> 
     <th>BRAND</th> 
     <th>MODEL</th> 
     <th>NAME</th> 
     <th>QUANTITY</th> 
     <th>COLOR</th> 
     <th>INFO</th> 
     <th>EDIT</th> 
     <th>DELETE</th> 
    </tr> 
    <c:choose> 
     <c:when test="${empty person.products}"> 
      <tr> 
       <td colspan="8" align="center"><span>You don't have any product yet!</span></td> 
      </tr> 
     </c:when> 
     <c:otherwise> 
      <c:forEach var="p" items="${person.products}"> 

       <c:url var="urlDel" value="servlet"> 
        <c:param name="id" value="${p.id}" /> 
       </c:url> 

       <c:url var="urlEdit" value="EditProduct.action"> 
        <c:param name="id" value="${p.id}" /> 
       </c:url> 

       <tr> 
        <td>${p.brand}</td> 
        <td>${p.model}</td> 
        <td>${p.name}</td> 
        <td>${p.quantity}</td> 
        <td>${p.color}</td> 
        <td>${p.info}</td> 
        <td><a href="${urlEdit}"><img src="<%=request.getContextPath()%>/images/edit.png"></a></td> 
        <td><a href="${urlDel}"><img src="<%=request.getContextPath()%>/images/delete.png"></a></td> 
       </tr> 
      </c:forEach> 
     </c:otherwise> 
    </c:choose> 
</table> 

我想改變的產品,然後當我回到packed.jsp我想看看modyfied產品。非常感謝大家。

回答

0

我找到了一個解決方案,用這些對象創建另一個類,然後把它放到會話中。有用。