2014-01-18 21 views
1

POST請求我有以下幾點:Servlet的取消刷新

  • JSP頁面提交請求到Servlet來添加新客戶
  • 的Servlet使用了一些動作類
  • 重定向到一些其他的Jsp頁面

這裏是源代碼。

new_customer.jsp:

<form action="/NewCustomerServlet" method="post"> 

    <input type="test" name="company_name" /> 
    <input type="submit" name="save_button" value="Save"/> 

</form> 

NewCustomerServlet:

@Override 
protected void doPost(HttpServletRequest request, response) throws ServletException, IOException { 
if(request.getParameter("save_button") != null){ 
    Customer customer; 
    try { 
     customer = action.createCustomer(request, response); 
     request.setAttribute(Const.CUSTOMER, customer); 
     RequestDispatcher dispatcher = request.getRequestDispatcher("index.jsp?v=v_cst"); 
     dispatcher.forward(request, response); 
     return; 
    } catch (InstantiationException | IllegalAccessException 
        | ClassNotFoundException | SQLException e) { 
     e.printStackTrace(); 

     request.setAttribute(Const.ERR_MSG_ATTR_NAME, "Failed to insert new customer: " + 
          e.getMessage()); 
     RequestDispatcher dispatcher = request.getRequestDispatcher("CRM/index.jsp?v=n_cst"); 
     dispatcher.forward(request, response); 
     return; 
    }  
} 

的index.jsp

<% 
    if(request.getParameter("v").equals("v_cst")) {%> 
     <jsp:include page="customer/view_customer.jsp"></jsp:include> 
<%} %> 

view_customer.jsp

<% 
    Customer customer = (Customer)request.getAttribute(Const.CUSTOMER); 

    String customerId = ""; 
    String name = ""; 
    String phone = ""; 
    String website = ""; 
    String address = ""; 

    if(customer != null){ 

     customerId = customer.getCustomerId(); 
     name = customer.getName(); 
     phone = customer.getPhone(); 
     website = customer.getWebsite(); 
     address = customer.getAddress(); 
    } 
%> 
<table> 
    <tr> 
     <td> 
      <label>Customer ID</label> 
     </td> 
     <td> 
      <input type="text" name="customer_id" value="<%=customerId %>" /> 
     </td> 
     <td> 
      <input type="button" value="Search" onclick="javascript:searchCustomer"/> 
     </td> 
     <td> 
      <label name="search_customer_err_msg" value="" style="color: red;"></label> 
     </td> 
    </tr> 

    <tr> 
     <td> 
      <label>Customer Name</label> 
     </td> 
     <td> 
      <input type="text" name="customer_name" value="<%= name%>"/> 
     </td> 
    </tr> 
    <tr> 
     <td> 
      <label>Customer website</label> 
     </td> 
     <td> 
      <input type="text" name="customer_website" value="<%= website%>" /> 
     </td> 
    </tr> 

    <tr> 
     <td> 
      <label>Customer phone</label> 
     </td> 
     <td> 
      <input type="text" name="customer_phone" value="<%= phone%>"/> 
     </td> 
    </tr> 
    <tr> 
     <td> 
      <label>Customer Address</label> 
     </td> 
     <td> 
      <input type="text" name="customer_address" value="<%= address%>"/> 
     </td> 
    </tr> 
</table> 

從頁面new_customer.jsp增加一個新客戶,並查看頁面view_customer.jsp在瀏覽器中,如果我刷新頁面(從那裏我看到view_customer頁面後.jsp),它會再次將數據提交給servlet並添加一個新客戶,我將看到具有新客戶ID的相同數據。

也許我還應該提及,我在瀏覽器地址欄中看到NewCustomerServlet的URL,而不是索引頁。

任何人都知道我在這裏錯過了什麼,以便在刷新時再次取消帖子?

**也許我忘了提及new_customer.jsp也包含在index.jsp頁面中,也許這可能是此行爲的原因?

+0

您正在查看view_customer.jsp頁面上的數據,並且只有一個按鈕(搜索)。然後,它如何提交new_customer.jsp頁面提交按鈕。那麼new_customer.jsp是單獨的頁面還是包含在其他頁面中? –

+0

感謝您的回答,因爲您可以在索引中注意到。jsp代碼我包括view_customer頁面 –

+0

也許我忘了提及new_customer.jsp也包含在index.jsp頁面,也許這可能是這種行爲的原因? –

回答

0

你不重定向到視圖,你只是轉發到相應的JSP服務器端,所以瀏覽器不會得到一個新的URL的通知。

而不是使用RequestDispatcher您必須建立重定向URL並執行HttpServletResponse.sendRedirect。這樣您的servlet就會將HTTP重定向發送回客戶端,客戶端將調用相應的URL並相應地更改地址欄。

而且你可能不希望重定向到JSP本身,而是到另一個控制器:

NewCustomerServlet

@Override 
protected void doPost(HttpServletRequest request, response) 
     throws ServletException, IOException { 
    ... 
    customer = action.createCustomer(request, response); 
    response.sendRedirect("ShowCustomer?customerId=" + customer.getCustomerId()); 
    ... 

而且ShowCustomerServlet可能是這樣的:

@Override 
protected void doGet(HttpServletRequest request, response) 
     throws ServletException, IOException { 

    // get customer by HTTP Request Parameter 'customerId' 
    customer = action.getCustomer(request, response); 

    // set attribute and forward to JSP 
    request.setAttribute(Const.CUSTOMER, customer); 
    RequestDispatcher dispatcher = request.getRequestDispatcher("showCustomer.jsp"); 
    dispatcher.forward(request, response); 
    ... 
+0

感謝您的答案,我需要使用調度程序,所以我可以傳遞一個對象的請求作爲屬性!如果我使用發送重定向,我無法做到! –

+0

@ Marwan Jaber:的確,這就是爲什麼你可能需要另一個控制器。事實是,在重定向的情況下,你會放棄'請求上下文'。所以如果你想重定向,你需要一個mecahnism來轉移狀態,在我的例子中它是參數'customerId'。 – home

+0

試過這也,但沒有工作!當您刷新瀏覽器時,瀏覽器會發送相同的先前請求,其中包含相同的參數(包括分配的操作),我將該解決方案看作瀏覽器忘記上一個操作,或者servlet識別請求是否從刷新發送並忽略它。在.net中,例如,您可以設置取消回發的參數,並取消重新提交。謝謝你的回答,但不幸的是它沒有工作niether –