Hello! Faced such a problem: I need to create an authorization page. During authentication, the entered login is compared with the login from the database and if everything is OK, redirect to the welcome page. Otherwise, it should display an error on the page. I try to display an error using the if tag on a jsp page that checks the error attribute, which I pass from the servlet. A redirect occurs, but the error is not displayed. Servlet
import javax.servlet.ServletConfig; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.context.support.SpringBeanAutowiringSupport; import java.io.IOException; import java.io.PrintWriter; @WebServlet(urlPatterns = "/login") public class LoginServlet extends HttpServlet { @Autowired UserBusiness service; @Override public void init(ServletConfig config) throws ServletException { super.init(config); SpringBeanAutowiringSupport.processInjectionBasedOnServletContext(this, config.getServletContext()); } @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { req.getRequestDispatcher("login.jsp").forward(req, resp); } protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String username = (String) req.getParameter("login"); String password = (String) req.getParameter("pd"); if(service.authentication(username, password)) { req.getRequestDispatcher("welcome.jsp").forward(req, resp); } else { req.getSession().setAttribute("error", true); resp.sendRedirect("login"); } } } JSP page
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%@ page import="com.sun.org.apache.xpath.internal.operations.Bool" %> </head> <body> <form action="login" method="POST"> Login: <br /> <input name="login" type="text" size="25" maxlength="30" value="" /> <br /> Password: <br/> <input name="pd" type="password" size="25" maxlength="30" value="" /> <br /> <input name="remember" type="checkbox" value="yes" /> Remember <br /> <input type="submit" name="enter" value="Enter" /> <c:if test="${error}"> ERROR </c:if> </form> <form action="registration.jsp" method="POST"> <input type="submit" name="registration" value="Registration" /> </form> </body> </html>