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?
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.