Тема: tomcat. Захист від повторної обробки форми
Всім Привіт! Намагаюся реалізувати PRG pattern, отримую помилку :
java.lang.IllegalStateException: Cannot call sendRedirect() after the response has been committed ...
@WebServlet("/Controller")
public class Controller extends HttpServlet{
private static final long serialVersionUID = 1L;
private boolean flag = false;
private List<String> name;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
if (flag == false){
process(request, response);
flag=true;
}
RequestDispatcher view = request.getRequestDispatcher("form.jsp");
view.forward(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
process(request, response);
flag=true;
response.sendRedirect("Controller");
}
@Override
public void init() throws ServletException {
name = new ArrayList<>();
}
private void process(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
req.setCharacterEncoding("UTF-8");
resp.setContentType("text/html;charset=utf-8");
HttpSession session = req.getSession();
String button = req.getParameter("submit");
if(button.equals("add")){
name.add(req.getParameter("name"));
session.setAttribute("name", name);
req.getRequestDispatcher("form.jsp").forward(req, resp);
}else if(button.equals("delete")){
name.removeAll(name);
req.getRequestDispatcher("form.jsp").forward(req, resp);
}
}
}