Java Servlet在刷新中取消post请求
我有以下资料:Java Servlet在刷新中取消post请求,java,jsp,servlets,Java,Jsp,Servlets,我有以下资料: JSP页面向Servlet提交请求以添加新客户 Servlet使用了一些动作类 重定向到其他Jsp页面 这是源代码 new\u customer.jsp: <form action="/NewCustomerServlet" method="post"> <input type="test" name="company_name" /> <input type="submit" name="save_button" value="Sav
- JSP页面向Servlet提交请求以添加新客户
- Servlet使用了一些动作类
- 重定向到其他Jsp页面
<form action="/NewCustomerServlet" method="post">
<input type="test" name="company_name" />
<input type="submit" name="save_button" value="Save"/>
</form>
@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>
<%} %>
<%
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>
查看\u customer.jsp:
<%
if(request.getParameter("v").equals("v_cst")) {%>
<jsp:include page="customer/view_customer.jsp"></jsp:include>
<%} %>
<%
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>
客户ID
客户名称
客户网站
客户电话
客户地址
在从页面new_customer.jsp添加新客户并在浏览器中查看页面view_customer.jsp之后,如果我刷新页面(从我看到view_customer.jsp的页面),它将再次将数据提交到servlet并添加新客户,我将看到具有新客户id的相同数据
也许我还应该提到,我在浏览器地址栏中看到的是NewCustomerServlet
的URL,而不是索引页的URL
有人知道我错过了什么来再次取消刷新中的帖子吗
**也许我忘了提到index.jsp页面中也包含了新的_customer.jsp,也许这就是这种行为的原因?您没有重定向到视图,您只是转发到相应的jsp服务器端,因此浏览器不会收到新URL的通知 您必须构建重定向URL并执行以下操作,而不是使用
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);
...
您正在查看view_customer.jsp页面上的数据,只有一个按钮(搜索)。那么它是如何提交new_customer.jsp页面提交按钮的。new_customer.jsp是一个单独的页面,还是包含在任何其他页面中?谢谢您的回答,正如您在index.jsp代码中所注意到的,我包括了view_customer页面可能我忘了提到index.jsp页面中也包括了new_customer.jsp,可能这就是这种行为的原因?是的,只需将new_customer.jsp与index.jsp分开。谢谢您的回答,我需要使用dispatcher,这样我就可以将请求作为属性传递给对象!如果我使用发送重定向,我无法执行@马万·贾伯:没错,这就是为什么你可能需要另一个控制器。事实是,在重定向的情况下,您会丢失“请求上下文”。因此,如果你想重定向,你需要一个mecahnism来转移状态,在我的示例中,它是参数
customerId
。也尝试了这个,但没有成功!刷新浏览器时,浏览器发送具有相同参数(包括分配的操作)的相同前一个请求。我看到的解决方案是,浏览器忘记前一个操作,或者servlet识别请求是否从刷新发送并忽略它。例如,在.net中,您可以设置一个参数来取消回发,从而取消重新提交。谢谢你的回答,但不幸的是它没有起作用niether@Marwan贾伯:会有用的,但可能需要比你想象的更多的改变。sendRedirect
将修改浏览器URL,请查看例如Wikipedia上的Post/Redirect/Get模式以了解更多详细信息: