There is an authorization system filter and servlet. The filter checks the session for id, and the servlet goes to the database and checks if there is such a user. But a strange thing happens: authorization does not take place the first time, but only if there is a session, and the instructions.getRequestDispatcher request.getRequestDispatcher("/WEB-INF/views/index.jsp") in the servlet are simply ignored.
Here is the filter:
@Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException, ServletException { final HttpServletRequest req = (HttpServletRequest) request; final HttpServletResponse res = (HttpServletResponse) response; final HttpSession session = req.getSession(false); if (nonNull(session) && sessionContainKey(session)) { req.getRequestDispatcher("/WEB-INF/views/index.jsp") .forward(req, res); } else { req.getRequestDispatcher("/WEB-INF/views/login.jsp") .forward(req, res); } filterChain.doFilter(request, response); } And this is the servlet:
@Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try { if (userExistInDB(request, getDBExecutor())) { setSessionAttribute(request.getSession(), request.getParameter("name")); request.getRequestDispatcher("/WEB-INF/views/index.jsp") //Вот здесь должен произойти перезод но его не происходит. Почему? .forward(request, response); } else {...} } catch (SQLException e) { e.printStackTrace(); } } That is, the problem is that the authorization only takes place when the session has been added, but not the first time, although it seems to me that the filter should have worked, and the solution is based on the servlet ...
Tell me what's wrong? How can I fix this? Thank.
response.sendRedirect(""). - Tsyklop