我有一個 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>
在其中,我輸入我的用戶名和密碼,然后單擊提交按鈕。我在除錯中查看資料沒有進入 servlet,我被重定向到帶有地址的頁面
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>
還有 servlet 本身。你可以看到我試圖弄清楚發生了什么。
@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);
}
}
}
uj5u.com熱心網友回復:
您的問題繼續:我在除錯中查看了資料沒有進入 servlet,我被重定向到地址為 ...
這是由于“GET”和“POST”方法的不同。
在您的代碼中,表單操作方法是 GET,這會導致您報告的字串:
http://localhost:8094/course_war_exploded/pages/controller?command=login&login=mylogin&password=mypassword
POST 方法略有不同,它不是將帶有表單資料的字串附加到 URL,而是將 POST 陣列發送到您的控制器,您可以讀取該陣列。
因此,在您的情況下,將表單方法更改為:
<form name="loginForm" method="post" action="controller.jsp">
uj5u.com熱心網友回復:
我假設你想達到
http://localhost:8094/course_war_exploded/controller?command=login&login=mylogin&password=mypassword
代替
http://localhost:8094/course_war_exploded/pages/controller?command=login&login=mylogin&password=mypassword
為此,您應該action以<form>這種方式編輯標簽中的屬性:
<form name="loginForm" method="get" action="${pageContext.request.contextPath}/controller">
${pageContext.request.contextPath} 是一個變數,在這種情況下計算為 /course_war_exploded
問題是您在action屬性中指定了相對路徑,因此瀏覽器假定您想要訪問./controller
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/406292.html
標籤:
上一篇:Django:如何更新無效表單欄位的類屬性以在Bootstrap5中顯示錯誤訊息?
下一篇:物件陣列回傳空JSX元素
