Servlet在刷新时取消发布请求

时间:2014-01-18 12:37:00

标签: java jsp servlets

我有以下内容:

  • JSP页面向Servlet提交请求以添加新客户
  • Servlet使用一些Action类
  • 重定向到其他一些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的网址,而不是索引网页的网址。

任何人都知道我在这里错过了刷新后再次取消帖子吗?

**也许我忘了提及new_customer.jsp也包含在index.jsp页面中,也许这可能是这种行为的原因?

1 个答案:

答案 0 :(得分:0)

您没有重定向到视图,只是转发到相应的JSP服务器端,因此浏览器不会收到新URL的通知。

您必须构建重定向网址并执行HttpServletResponse.sendRedirect,而不是使用RequestDispatcher。这样你的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);
    ...