When performing the task, I wanted to experiment with a very interesting situation, when -> the user enters data in the form -> clicks on the "create" button -> (data is recorded in the database) -> the user, after clicking on the button, sees on the same page that data are in the database (all) and the record just added.

I got everything but one nuance, after clicking on the button, the data is added to the database, but the user just entered the data is not displayed, but they are displayed after another click on the "create" button or add a new record.

Probably, maybe I’m wrong, I suppose that the answer to the user (displaying a page with data) comes faster than the record enters the database and only after the next addition the previous record is visible.

So how to make the data instantly displayed to the user and be real (those that are actually in the database, and not a substitution list (list) before adding to the database to simulate a quick add). Please help with this situation.

Data that is displayed to the user in the JSP file

<body> <div> <div class="form"> <%!private static final UserStore userStore = UserStore.getInstance();%> <% CopyOnWriteArrayList<User> storeResult = UserStore.getInstance().getResult(); if (!storeResult.isEmpty()) { for (User user : storeResult) { %> <div> <p> <b>Name:</b> <%=user.getName()%><br> <b>Login:</b> <%=user.getLogin()%><br> <b>Date:</b> <%=user.getCreateDate()%><br> </p> </div> <%} } else {%> <pre> <b>Can't find any users</b> </pre> <%}%> <hr> </div> </div> <body> 

Start page is also a JSP file

 <body> <div class="mainBlock"> <div> <form action="insert" method="POST"> <label for="userName">Name</label> <input id = "userName" type="text" name="user" placeholder="User name"><br> <label for="userLogin">Login</label> <input id = "userLogin" type="text" name="login" placeholder="User login"><br> <label for="userEmail">Email</label> <input id = "userEmail" type="email" name="email" placeholder="User email"><br> <label for="userPassword">Password</label> <input id = "userPassword" type="password" name="password" placeholder="User password"><br> <input type="submit" value="create"> </form> </div> <hr> <div class="center"> <form class="inline" action="result" method="POST"> <input type="submit" value="get users" name="getUsers"> </form> <form class="inline" action="update" method="POST"> <input type="submit" value="update user" name="updateUser"> </form> <form class="inline" action="delete" method="POST"> <input type="submit" value="delete user" name="deleteUser"> </form> </div> <div> <br> <jsp:include page="get.jsp" flush="true"/> </div> </div> </body> 

Servlet insert

 public class InsertServlet extends HttpServlet { private static final Logger LOG = LoggerFactory.getLogger(UsersServlet.class.getName()); private static UserStore userStore; /** * Get UserStore object to manipulate with database. * @throws ServletException exp. */ @Override public void init() throws ServletException { userStore = UserStore.getInstance(); } /** * doPost method insert data to database. * @param req request. * @param resp response. * @throws ServletException servlet exception. * @throws IOException input output exception. */ @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { this.insertIfNotEmpty(req, resp); } /** * Create User instance with received value from the request and insert to the database. * @param req request. * @param resp response. * @throws ServletException servlet exception. * @throws IOException input output exception. */ private void insertIfNotEmpty(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { User user; if (!req.getParameter("user").equals("") && !req.getParameter("login").equals("") && !req.getParameter("password").equals("") && !req.getParameter("email").equals("")) { user = new User(req.getParameter("user"), req.getParameter("login"), req.getParameter("password"), req.getParameter("email")); LOG.debug("Add user to database with name {} and login {}", user.getName(), user.getLogin()); req.getRequestDispatcher("/forms/index.jsp").forward(req, resp); userStore.insert(user); } req.getRequestDispatcher("/forms/index.jsp").forward(req, resp); } } 
  • somewhere get.jsp should be a get.jsp page - Drakonoved
  • It seems to me that I found a mistake. Probably need to change 2 lines of places. req.getRequestDispatcher ("/ forms / index.jsp"). forward (req, resp); userStore.insert (user); - Vardan Matevosyan
  • Well, the most important thing brought the necessary thought .. - Drakonoved

1 answer 1

Found the answer. Just swapped 2 lines

 userStore.insert(user); req.getRequestDispatcher("/forms/index.jsp").forward(req, resp); 

First write to the database, and then send the page to the user with select all users in the database.

And if there was a task, “Show start page”, then probably it would be because when asking a question, we first show the page and add backwards that the user would not notice the time that the database spends on adding data. Probably for a small amount of data it makes no sense to do it, but still.