简体   繁体   中英

The problem with the transition from JSP to Tomcat 7 servlet. The post method is not called from JSP page

I have a jsp page 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>

A command is executed that redirects to the next page

<%@ 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>

In it, I enter my username and password, and click on the submit button. I looked in the debug that the data does not go to the servlet, I am redirected to a page with the address

http://localhost:8094/course_war_exploded/pages/controller?command=login&login=mylogin&password=mypassword

My 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>

And the servlet itself. You can see my attempts to figure out what's going on.

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

I assume you want to reach

http://localhost:8094/course_war_exploded/controller?command=login&login=mylogin&password=mypassword  

Instead of

http://localhost:8094/course_war_exploded/pages/controller?command=login&login=mylogin&password=mypassword  

In order to do that, you should edit the action attribute in your <form> tag this way:

<form name="loginForm"  method="get" action="${pageContext.request.contextPath}/controller">  

${pageContext.request.contextPath} is a variable that in this case evaluates as /course_war_exploded

The problem was that you specified a relative path in the action attribute, so the browser assumed you wanted to reach ./controller

Your question resumes to: I looked in the debug that the data does not go to the servlet, I am redirected to a page with the address...

This is due to the difference of "GET" and "POST" methods.

in your code, the form action method is GET, which leads to the string you are reporting: http://localhost:8094/course_war_exploded/pages/controller?command=login&login=mylogin&password=mypassword

the POST method is slightly different, instead of appending a string with the form data to the URL it sends a POST array to your controller, which you than can read in.

so in your case change form method to:

<form name="loginForm"  method="post" action="controller.jsp">

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM