【问题标题】:Why doGET is being called instead of doPOST?为什么调用 doGET 而不是 doPOST?
【发布时间】: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"&lt;form&gt;,其中包含一个按钮。点击它,看看调用了什么方法。

标签: jsp


【解决方案1】:

您是否也在 web.xml 中声明了安全角色?

   <security-role>
      <role-name>users</role-name>
   </security-role>

如果您正在访问您的 servlet,例如

   SecurityCheck/myServlet

您想得到正确的登录屏幕提示吗?您正在通过 GET 方法(默认)发出资源请求。这应该添加到您的安全约束 > Web 资源集合标记中。

   <http-method>GET</http-method>
   <http-method>POST</http-method>

目前,如果还没有用户通过身份验证,则只会提示 SecurityCheck 路径中的 POST 方法登录。

【讨论】:

  • 试过了,还是一样的奇怪。
  • 谢谢...现在至少我得到了登录页面,但请求仍在通过 GET 方法。我怎样才能通过 POST 做到这一点。我只是在关注与此视频中相同的内容:link。我不知道它是如何为他而不是我工作的。 :(
  • 对不起。在视频中,他正在吃doGET。我的错。
  • 如果您直接从浏览器访问您的 servlet,默认调用将始终是 GET。如果你想通过 POST 访问你的 servlet,你可以尝试在另一个页面上创建一个
    并设置 action="/myServlet" 和 method="post"。提交此表单后,您将通过 POST 方法访问 servlet。您是否有理由希望它通过 POST?为什么不使用 GET?
  • 是的。现在一切正常。感谢您的投入。我将通过制作表单来尝试 POST 方法。没有理由使用 POST。我只是在学习东西。 :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-09
  • 2016-04-25
  • 1970-01-01
  • 1970-01-01
  • 2011-08-18
相关资源
最近更新 更多