I have an index.jsp page where I have a form where the user enters some data that gets stored in the database using a Controller servlet.
I want to disp
Here is the simplest solution
<%@page import="java.util.*"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%!
//This List is just for demonstration purposes
List names = new ArrayList();
%>
<%
if(request.getParameter("name")!=null ){
names.add(request.getParameter("name"));
session.setAttribute("nameList", names);
//Here you put your database insert code
//Whenever the code execution reaches this line ,
//then it means that you have a new page submission
//and not a refresh or f5 case
response.sendRedirect("index.jsp");
}
%>
Name:
The trick lies with the response.sendRedirect("index.jsp");. This makes null all request parameters. If an f5 or refresh is hit, then the if is never executed. If a normal submit is the case, then the if is executed and you call the response.sendRedirect("index.jsp");.
In summary, all you really need to do is:
1) Check if(request.getParameter("name")!=null )
2) If above is true then do database inserts
3) If above is true then response.sendRedirect("index.jsp");
UPDATE
if(request.getParameter("name")!=null ){
DbUtility.addNameToDb(request.getParameter("name"));
ArrayList currentList = DbUtility.getAllNamesFromDb();
session.setAttribute("nameList", currentList);
response.sendRedirect("index.jsp");
}
You just have to implement these 2 methods. The addNameToDb(String) will make the insert in your database. And the getAllNamesFromDb() will return an ArrayList Object that will represent on the entries from your database. (And you no longer need the names list introduced before in my first answer)