我有以下内容:
- 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
的 URL,而不是索引页面的 URL。
任何人都知道我在这里错过了什么以在刷新时再次取消帖子?
** 也许我忘了提到 new_customer.jsp 也包含在 index.jsp 页面中,也许这可能是这种行为的原因?
最佳答案
您不会重定向到 View ,您只是转发到相应的 JSP 服务器端,因此浏览器不会收到新 URL 的通知。
您必须构建重定向 URL 并执行 HttpServletResponse.sendRedirect
,而不是使用 RequestDispatcher
.这样,您的 servlet 会将 HTTP 重定向发送回客户端,客户端将调用相应的 URL 并相应地更改地址栏。
您可能不想重定向到 JSP 本身,而是重定向到另一个 Controller :
在 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);
...
关于java - Servlet 在刷新时取消发布请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21204189/