Java 传递到URL的参数无效

Java 传递到URL的参数无效,java,servlets,Java,Servlets,当我在表单中输入值时,它会相应地通过if-else 但是,当我通过URL传递参数时,如下所示: http://localhost:8080/sessiondemo/index.html?user=wronguser&pass=wrongpass 它没有经历任何事情。没有一个println正在执行(甚至user/pass/end也没有)。它只是回到形式上 我能知道为什么会这样吗 <form action = "processlogin.cgi" method = "post">

当我在表单中输入值时,它会相应地通过if-else

但是,当我通过URL传递参数时,如下所示:

http://localhost:8080/sessiondemo/index.html?user=wronguser&pass=wrongpass

它没有经历任何事情。没有一个println正在执行(甚至user/pass/end也没有)。它只是回到形式上

我能知道为什么会这样吗

<form action = "processlogin.cgi" method = "post">
    <p>Username: <input type = "text" name = "user" size = "25"/></p>
    <p>Password: <input type = "password" name = "pass" size = "25"/></p>
    <input type = "submit" value = "Login"/>
</form>



@WebServlet("/processlogin.cgi")
public class ProcessLoginServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String user = request.getParameter("user");
        String pass = request.getParameter("pass");

        System.out.println("User: " + user);
        System.out.println("Pass: " + pass);

        if(UserManager.isValidUser(user, pass)) {
            System.out.println("valid");

        } else {
            System.out.println("invalid")
        }

        System.out.println("end");
    }
}

用户名:

密码:

@WebServlet(“/processlogin.cgi”) 公共类ProcessLoginServlet扩展了HttpServlet{ 私有静态最终长serialVersionUID=1L; 受保护的void doGet(HttpServletRequest请求,HttpServletResponse响应)抛出ServletException,IOException{ doPost(请求、响应); } 受保护的void doPost(HttpServletRequest请求、HttpServletResponse响应)引发ServletException、IOException{ 字符串user=request.getParameter(“用户”); 字符串pass=request.getParameter(“pass”); System.out.println(“用户:“+User”); System.out.println(“通过:+Pass”); if(UserManager.isValidUser(用户,通过)){ System.out.println(“有效”); }否则{ System.out.println(“无效”) } 系统输出打印项次(“结束”); } }
您的servlet只处理POST请求

当您输入URL时,如

http://localhost:8080/sessiondemo/index.html?user=wronguser&pass=wrongpass
或者在HTML表单中设置方法,以获取正在生成的GET请求

要处理GET请求,您需要覆盖
HttpServlet.doGet
HttpServlet服务

您在
doPost
方法中使用的代码对于GET请求非常有效

更新:

您的servlet已注册为path
/processlogin.cgi
,因此要测试来自浏览器的GET调用,您需要输入

http://localhost:8080/sessiondemo/processlogin.cgi?user=wronguser&pass=wrongpass

您的servlet只处理POST请求

当您输入URL时,如

http://localhost:8080/sessiondemo/index.html?user=wronguser&pass=wrongpass
或者在HTML表单中设置方法,以获取正在生成的GET请求

要处理GET请求,您需要覆盖
HttpServlet.doGet
HttpServlet服务

您在
doPost
方法中使用的代码对于GET请求非常有效

更新:

您的servlet已注册为path
/processlogin.cgi
,因此要测试来自浏览器的GET调用,您需要输入

http://localhost:8080/sessiondemo/processlogin.cgi?user=wronguser&pass=wrongpass

如果url参数在等式中,并且您正在以这种形式
request.getParameter(“”
)检索它们,那么您应该在/重写get方法中完成工作,该方法是您尝试生成的请求类型

话虽如此,这里有更多的见解:


如果url参数在等式中,并且您正在以此表单
request.getParameter(“”
)检索它们,那么您应该在/重写get方法中完成工作,该方法是您尝试生成的请求类型

话虽如此,这里有更多的见解:


如果通过url传递参数,则是HttpGet方法,而不是HttpPost。您还必须覆盖
doGet
。如果通过url传递参数,则是HttpGet方法,而不是HttpPost。您也必须覆盖
doGet
。您好,我已将其更改为
method=“get”
,并在
doGet()
中复制了相同的代码,但结果是相同的。@silver然后调用index.html不会命中您的servlet。哦,这就是您所说的“不命中servlet”的意思。我正在传递到html本身。我真傻。谢谢你,接受并投票支持你。:)您好,我已将其更改为
method=“get”
,并在
doGet()
中复制了相同的代码,但结果是相同的。@silver然后调用index.html不会命中您的servlet。哦,这就是您所说的“不命中servlet”的意思。我正在传递到html本身。我真傻。谢谢你,接受并投票支持你。:)