How to prevent duplicate entries while refreshing?

后端 未结 4 1803
情深已故
情深已故 2021-01-02 23:17

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

4条回答
  •  暗喜
    暗喜 (楼主)
    2021-01-02 23:38

    I thought of using POST-REDIRECT-GET pattern but the problem is when I redirect I don't get those data to be displayed using JSTL.

    Just send a request parameter along identifying the information you'd like to display in the new GET request.

    // ...
    Long id = dataService.save(data);
    // ...
    response.sendRedirect(request.getContextPath() + "/index?editId=" + id);
    

    and then in the servlet which is mapped on an URL pattern of /index

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Long editId = Long.valueOf(request.getParameter("editId")); // Handle nullcheck yourself.
        Data editData = dataService.find(editId);
        request.setAttribute("editData", editData); // For the edit form.
        List allData = dataService.list();
        request.setAttribute("allData", allData); // For the table/list of all data.
        request.getRequestDispatcher("/index.jsp").forward(request, response);
    }
    

提交回复
热议问题