提交后从servlet刷新JSP
我有一个具有以下形式的 JSP (AddDocument.jsp):
<form action=" ${pageContext.request.contextPath}/project?to=submitAdd&idProject=${param.idProject}" method="post" name="form">
<div>Name:</div>
<input type="text" name="name" /><br /> <br />
<input type="submit" value="Add document" />
</form>
提交时,它会调用执行以下操作的 servlet (ProjectManager):
private void addDocumento(HttpServletRequest request, HttpServletResponse response) {
HttpSession session=request.getSession();
// elaborating...
session.setAttribute("addingResult", "Document added");
// or error if an exception is caught
redirect(request.getContextPath()+"/project?to=add&idProject="+idProject,request,response);
}
即在设置会话属性后重定向到同一页面,该属性将由 JSP 读取并显示
我的问题是,通过这种方式,浏览器认为它将进入一个新页面,并且其历史记录会增加(例如,如果我从 X.jsp 转到 AddingDocument.jsp,则插入 10 次表单中的名称无效,并被重定向到同一页面并出现错误,我将不得不返回 10 次才能到达 X.jsp)。
我希望能够仅返回 1 次才能到达 X .jsp,所以我需要刷新 JSP 而不是重定向,但对请求和响应具有相同的效果(如果我手动刷新页面,则不会出现新的提交)。 我该怎么做?
I have a JSP (AddDocument.jsp) with the following form:
<form action=" ${pageContext.request.contextPath}/project?to=submitAdd&idProject=${param.idProject}" method="post" name="form">
<div>Name:</div>
<input type="text" name="name" /><br /> <br />
<input type="submit" value="Add document" />
</form>
When submitted it calls a servlet (ProjectManager) which does the following:
private void addDocumento(HttpServletRequest request, HttpServletResponse response) {
HttpSession session=request.getSession();
// elaborating...
session.setAttribute("addingResult", "Document added");
// or error if an exception is caught
redirect(request.getContextPath()+"/project?to=add&idProject="+idProject,request,response);
}
That is redirecting to the same page after setting a session's attribute, which will be read by the JSP and shown to the user.
My problem is that this way the browser thinks it is going to a new page and its history increases (for instance, if I go to AddingDocument.jsp from X.jsp, insert 10 times an invalid name in the form and get redirected to the same page with an error, I will have to go back 10 times to arrive at X.jsp).
I want to be able to go back only 1 time to arrive at X.jsp, so I need some refreshing of my JSP instead of redirect, but with the same effect on request and response (that is no new submit if I manually refresh the page).
How can I do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要使用 AJAX 而不是普通表单提交。除了不影响浏览器历史记录之外,它还应该提供当今大多数用户所期望的“更好”的用户体验。
You'll want to use AJAX instead of a plain form submit. In addition to not contributing to the browser history, it should provide a "better" user experience that most users have come to expect today.