If you want to avoid resubmission problem in JSP

Some time ago when I faced with this problem for the first time, this was annoying. After googling around (maybe about 30 articles) I didn't find complete example that will work for JSP. There were good examples for other languages. Well after some reading, idea was in my head. Some time ago when I faced with this problem for the first time, this was annoying. After googling around (maybe about 30 articles) I didn't find complete example that will work for JSP. There were good examples for other languages. Well after some reading, idea was in my head. Logically, resubmission occures when refresh button calls page in which you accept parameters from user form and insert them in database. Shortly, if you want to avoid double submission just organize one page which would be only for displaying results. For example on other page you can place accepting parameters and database insert. So when you have display on other page, refresh button will call only display, without accepting parameters for processing. Read below code snippet. Assume that data are sent from form which has 2 fields with names (Name, Field). This is only simple example used for describing resubmission problem.


process.jsp

<%Connection conn=null;Statement st=null;
Class.forName("com.mysql.jdbc.Driver").newInstance();
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/your_database", "user", "pass");
st = conn.createStatement();
if(request.getParameter("SubmitButton")!=null && !request.getParameter("Name").isEmpty() && !request.getParameter("Field").isEmpty()){
st.execute("insert into your_table (user,field) values ('" +
request.getParameter("Name") +"','" +
(String) request.getParameter("Field") + "')");
conn.close();
st.close();
response.sendRedirect("your_page_for_display.jsp");
%>

your_page_for_display.jsp

<%Connection conn=null;Statement st=null;ResultSet rs=null;
Class.forName("com.mysql.jdbc.Driver").newInstance();
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/your_database", "user", "pass");
st = conn.createStatement();
rs = st.executeQuery("select * from your_table");
while(rs.next())
{ out.print( rs.getString(2) +":
");
out.print(rs.getString(3);)
}
st.close(); conn.close(); rs.close(); %>

Thank you for your patience!
Pera Sarajlic