Every project that we do, there are always some common requirement, that project must need to implement. So one of the common requirement i want to discuss here is...
"duplicate form request submission"
In any project duplicate form request must be intercept and must be aborted, to avoid duplicate data or inconsistency.
In J2EE environment, we can achieve this functionality by using following resources-
Using Filter Class
Using Controller Class(Servlet)
Lets start creating.....
Presentation Tier Component (JSPs)
success.jsp
<%--
Document : success.jsp
Author : Gaurav Pathak
--%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
Student Inserted Successfully!
Inserted Student: ${stdId} successfully...
error.jsp
<%--
Document : error.jsp
Author : Gaurav Pathak
--%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
Error!
Due to security restriction your session has been expired.please re-login.... below are the possible reasons
Browser back button pressed
Duplicate form request
you have pressed F5
Controller Component (Filter)
DuplicateRequestInterceptor.class
/***
* @author:Gaurav Pathak
*/
import java.io.IOException;
import java.util.Calendar;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class DuplicateRequestInterceptor implements Filter {
@Override
public void destroy() {
// no-op
}
@Override
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
System.out.println("in doFilter of DuplicateRequestInterceptor");
HttpServletRequest httpRequest = null;
HttpSession session = null;
String method = null;
String token = null;
String cToken = null;
httpRequest = (HttpServletRequest) request;
session = httpRequest.getSession(false);
// new user
if (session == null) {
session = httpRequest.getSession();
} else {
System.out.println("session is not null... existing user");
// existing user
method = httpRequest.getMethod();
if (method.equalsIgnoreCase("post")) {
cToken = request.getParameter("ctoken");
token = (String) session.getAttribute("stoken");
if (token.equals(cToken) == false) {
session.invalidate();
((HttpServletResponse) response)
.sendRedirect("error.jsp");
return;
}
}
}
token = session.getId() + Calendar.getInstance().getTimeInMillis();
session.setAttribute("stoken", token);
chain.doFilter(request, response);
}
@Override
public void init(FilterConfig config) throws ServletException {
// no-op
}
}
StudentInsertServlet.class
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
*
* @author Gaurav Pathak
*/
public class StudentInsertServlet extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
String id = null;
RequestDispatcher rd = null;
id = request.getParameter("id");
request.setAttribute("stdId", id);
rd = request.getRequestDispatcher("success.jsp");
rd.forward(request, response);
} finally {
out.close();
}
}
//
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
@Override
public String getServletInfo() {
return "Short description";
}
}
Entries in Deployment Descriptor(web.xml)
web.xml
DuplicateRequestInterceptor
DuplicateRequestInterceptor.java
DuplicateRequestInterceptor
/*
With this configuration all requests will be intercepted by the DuplicateRequestInterceptor filter.
You have to include below code in all jsp page that are the part of your navigation, to stop duplicate form submission.