Проблемы с с вызовом методов GET и POST из JSP
Пересмотрел большое количество форумов и тем, но самостоятельно решить проблему не смог, потратив много времени. Если чего-то не хватает в информации, напишите. Дополню. Итак, у меня есть jsp-страница index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<fmt:setLocale value="${locale}" scope="session"/>
<fmt:setBundle basename="local.pagecontent"/>
<html>
<head>
<title>Index Page</title>
</head>
<body>
<c:redirect url="/controller?command=go_to_login_page"/>
<h1>dd</h1>
</body>
</html>
Выполняется команда, которая перенаправляет на следующую страницу
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<%--
<fmt:setLocale value="${locale}" scope="session" />
--%>
<fmt:setBundle basename="local.pagecontent"/>
<html>
<head>
<title><fmt:message key="login.title"/></title>
</head>
<body>
<!DOCTYPE html>
<html>
<head>
<title>Insert title here</title>
</head>
<body>
<div align="middle">
<h1>Employee Login Form</h1>
<form name="loginForm" method="get" action="controller">
<input type="hidden" name="command" value="login"/>
<table>
<tr>
<td>Login</td>
<td><label>
<input type="text" name="login" />
</label></td>
</tr>
<tr>
<td>Password</td>
<td><label>
<input type="password" name="password" />
</label></td>
</tr>
</table>
<button type="submit">sign in</button>
<a href="registration.jsp">No account?</a>
</form>
</div>
</body>
</html>
В нем я ввожу свое имя пользователя и пароль и нажимаю кнопку отправки. Посмотрел в отладке, что данные на сервлет не идут, меня перенаправляют на страницу с адресом
http://localhost:8094/course_war_exploded/pages/controller?command=login&login=mylogin&password=mypassword
Мой web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="https://jakarta.ee/xml/ns/jakartaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee https://jakarta.ee/xml/ns/jakartaee/web-app_5_0.xsd"
version="5.0">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<error-page>
<exception-type>java.lang.Throwable</exception-type>
<location>/errors/exception_error.jsp</location>
</error-page>
<!--<servlet>
<servlet-name>ControllerServlet</servlet-name>
<servlet-class>com.example.course.controller.ControllerServlet</servlet-class>
</servlet>-->
<!--<servlet-mapping>
<servlet-name>ControllerServlet</servlet-name>
<url-pattern>controller</url-pattern>
</servlet-mapping>-->
<error-page>
<error-code>404</error-code>
<location>/errors/error_404.jsp</location>
</error-page>
</web-app>
И сам сервлет. Вы можете видеть мои попытки выяснить, что происходит.
@WebServlet(name = "ControllerServlet", value = "/controller")
public class ControllerServlet extends HttpServlet {
private static final Logger logger = LogManager.getLogger();
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
processRequest(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
processRequest(request, response);
}
private void processRequest(HttpServletRequest request, HttpServletResponse response) throws IOException {
String stringCommand = request.getParameter(RequestParameter.COMMAND);
Command command = CommandProvider.defineCommand(stringCommand).orElseThrow(IllegalArgumentException::new);
/*
Command = new CreateAccountCommand();
*/
System.out.println(stringCommand + "--------");
try {
Router router = command.execute(request);
System.out.println(router.getPage());
System.out.println(router.getType().name());
switch (router.getType()) {
case FORWARD -> request.getRequestDispatcher(router.getPage()).forward(request, response);
case REDIRECT -> response.sendRedirect(request.getContextPath() + router.getPage());
default -> {
logger.log(Level.ERROR, "Router type {} is incorrect", router.getType());
response.sendRedirect(request.getContextPath() + PagePath.ERROR_404);
}
}
} catch (IOException | ServletException | ServiceException | ControllerException e) {
logger.log(Level.ERROR, "Error when executing command {} ", stringCommand);
request.getSession().setAttribute(EXCEPTION, e);
response.sendRedirect(request.getContextPath() + PagePath.EXCEPTION_ERROR_REDIRECT);
}
}
}