繁体   English   中英

为什么我收到“此 URL 不支持 HTTP 方法 GET”错误?

[英]Why I am getting 'HTTP method GET is not supported by this URL' error?

import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class LoginServlet extends HttpServlet
{
private void sendLoginPage (HttpServletResponse res, HttpServletRequest req, boolean error)
throws ServletException,IOException
{
    res.setContentType("text/html");
    PrintWriter o = res.getWriter();        
    o.println("<html><head><title>Sample Login Page</title></head><body>Welcome to Login Page : ");
    if (error)
    o.println("<fieldset><legend>Login Form : </legend>");
    o.println("Login Failed, Please Try Again");
    o.println("<form method="+"post"+">");
    o.println("<br/><input type="+"text"+"value="+"username"+"/>");
    o.println("<br/><input type="+"password"+"value="+""+"/>");
    o.println("<br/><input type="+"button"+"value="+"Submit"+"/>");
    o.println("</form></fieldset></body></html>");
}

public void doGet (HttpServletResponse res, HttpServletRequest req)
throws ServletException,IOException
{
    sendLoginPage ( res, null, false ) ;
}

public void doPost (HttpServletResponse res, HttpServletRequest req)
throws ServletException,IOException
{
    String username = req.getParameter("username");
    String password = req.getParameter("password");
    if ( username.equals("*******") && password.equals("*******") )
    {
        res.sendRedirect ("http://localhost:7001/ten/r1");
    }
    else 
    {
        sendLoginPage ( res, null, true ) ;
    }
}           
}                   

好吧,这个 Servlet 编译正常,没有任何错误,并且也部署在服务器上,但在尝试通过 url 访问它时显示此错误:

此 URL 不支持 HTTP 方法 GET

您在方法上颠倒了 HttpServletResponse 和 HttpServletRequest 参数。

旁注:在重写方法上使用@Override 注释导致编译错误(假设您至少使用 Java 1.5)

当我的方法调用时发生在我身上

“super.doGet(req, resp)”或“super.doPost(req, resp)”。

在我删除了从 doGet 和 doPost 调用的超级 class 之后,它工作正常。

事实上,那些超级 class 调用代码是由 Eclipse IDE 模板插入的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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