【发布时间】:2014-08-09 11:35:31
【问题描述】:
我正在使用 Servlet/JSP 测试基于表单的安全身份验证。我只是直接运行 servlet,它应该根据 web.xml 文件要求我进行登录身份验证。但它只是每次都进入doGET方法。是的,我已经在 tomcat-users.xml 文件中添加了“角色”“用户”。我是 J2EE 的新手。所以请耐心等待我的愚蠢问题。
这里是 Login.jsp
<form method="post" action="j_security_check">
<table>
<tr>
<td>User name: </td>
<td><input type="text" name="j_username"></td>
</tr>
<tr>
<td>Password: </td>
<td><input type="password" name="j_password"></td>
</tr>
</table>`enter code here`
<input type="submit" value="Login">
</form>
这里是 Servlet:
@WebServlet("/SecurityCheck")
public class SecurityCheck extends HttpServlet {
private static final long serialVersionUID = 1L;
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
response.getWriter().println("I went to doGET");
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
response.getWriter().println("success........");
}
}
这是 web.xml:
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<security-constraint>
<web-resource-collection>
<web-resource-name>Security check</web-resource-name>
<url-pattern>/SecurityCheck/*</url-pattern>
<http-method>POST</http-method>
</web-resource-collection>
<auth-constraint>
<role-name>users</role-name>
</auth-constraint>
</security-constraint>
<login-config>
<auth-method>FORM</auth-method>
<form-login-config>
<form-login-page>/Login.jsp</form-login-page>
<form-error-page>/Faliure.jsp</form-error-page>
</form-login-config>
</login-config>
</web-app>
【问题讨论】:
-
" 我只是直接运行 servlet" 你这是什么意思?您是否在浏览器中输入了 servlet 的 URL?还是您实际上是通过其他形式的 POST 请求到达它?
-
在 servlet 页面上,我只是通过右键单击它来执行“运行服务器”。我正在使用 Eclipse IDE。
-
如果您的 servlet 是通过 GET 方法调用的,它将运行
doGet()方法。在根上下文中创建另一个 HTML 页面,在其中创建一个带有method="post"和action="SecurityCheck"的<form>,其中包含一个按钮。点击它,看看调用了什么方法。
标签: jsp