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); } }