[英]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 ) ;
}
}
}
Well this Servlet compiles okay without any errors and also gets deployed on the server but shows this error when trying to access it through url:好吧,这个 Servlet 编译正常,没有任何错误,并且也部署在服务器上,但在尝试通过 url 访问它时显示此错误:
HTTP method GET is not supported by this URL
此 URL 不支持 HTTP 方法 GET
You have the HttpServletResponse and HttpServletRequest parameters reversed on your methods.您在方法上颠倒了 HttpServletResponse 和 HttpServletRequest 参数。
Side note: Using the @Override annotation on overridden methods would cause a compile error for this (assuming you're using at least Java 1.5)旁注:在重写方法上使用@Override 注释会导致编译错误(假设您至少使用 Java 1.5)
This happened to me when my method call当我的方法调用时发生在我身上
"super.doGet(req, resp)" or "super.doPost(req, resp)". “super.doGet(req, resp)”或“super.doPost(req, resp)”。
After i removed above super class calling from the doGet and doPost it worked fine.在我删除了从 doGet 和 doPost 调用的超级 class 之后,它工作正常。
Infact those super class calling codes were inserted by the Eclipse IDE template.事实上,那些超级 class 调用代码是由 Eclipse IDE 模板插入的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.