There is a start page index.jsp. There is an authorization / registration form on it. Auth is responsible for authorization Auth servlet, respectively, when you click on the button, the servlet is accessed by the post method. He works out but the problem is different. There is a home page where users get after successful login - home.jsp (home). Accordingly, the Auth servlet is redirected if the user is authorized. And the problem is that after pressing the authorization button, the url address becomes: http: // localhost: 8080 / mysite / Auth . And so even if the authorization is not successful and even if we switched to home.jsp. This address does not change.

Register in web.xml like this:

<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> <display-name>mysite</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <servlet> <servlet-name>Index</servlet-name> <jsp-file>/index.jsp</jsp-file> </servlet> <servlet-mapping> <servlet-name>Index</servlet-name> <url-pattern>/index</url-pattern> </servlet-mapping> <servlet> <servlet-name>Home</servlet-name> <jsp-file>/home.jsp</jsp-file> </servlet> <servlet-mapping> <servlet-name>Home</servlet-name> <url-pattern>/home</url-pattern> </servlet-mapping> <servlet> <servlet-name>Auth</servlet-name> <servlet-class>ru.tsyklop.mysite.Auth</servlet-class> </servlet> <servlet-mapping> <servlet-name>Auth</servlet-name> <url-pattern>/auth</url-pattern> </servlet-mapping> </web-app> 

index has changed. Instead of index.jsp in the address bar is now index. but the other has not changed.

What's wrong?

Auth Servel Code:

 package ru.tsyklop.mysite; import java.io.IOException; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Servlet implementation class Auth */ @WebServlet("/Auth") public class Auth extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public Auth() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //PrintWriter out = response.getWriter(); //out.append("POST!"); RequestDispatcher dispatcher = null; String Email = request.getParameter("email"); String Pass = request.getParameter("pass"); if(Email.equals("test@gmail.com") && Pass.equals("123456")) { dispatcher = request.getRequestDispatcher("home"); } else { //response.sendRedirect("index"); dispatcher = request.getRequestDispatcher("index"); request.setAttribute("error", "Email or password incorrect. Try again!"); } dispatcher.forward(request, response); } } 

    1 answer 1

    Hello. The essence of the problem is in the method request.getRequestDispatcher(String path).forward - it invisible to the user redirects the request in a new way. The transfer of control occurs in the container and the browser does not participate.

    But the use of the method response.sendRedirect(String path); will return the header back to the client and oblige it (client or browser) to send a new request to the specified address (which of course will be reflected in the URL line).

    • The problem is that the Dispatcher is used to change the attributes that I ask. This is an error with the wrong login, for example. If I use sendRedirect then setAtrbute will not work. And I need it. Thank. - Tsyklop
    • @ Alexander Lemyagov validate the user in the filter on the way to the home servlet - Peter Slusar
    • @Alexander Lemyagov and you will not depend on the URL - Peter Slusar
    • I do not quite understand what it is about the filter ... I will say that I made cookies. If the user is not authorized, he will be redirected to index. - Tsyklop
    • @Aleksandr Lemjagov I’m referring to the implementation of the javax.servlet.Filter interface. This interface interface is most certainly presented in your IDE. For the sake of completeness, I recommend reading what the listener is (in the context of web development). (Although if this is your first, not a combat application, then you should not even think about this). - Peter Slusar