gpt4 book ai didi

java - 为什么我收到 'HTTP method GET is not supported by this URL' 错误?

转载 作者:行者123 更新时间:2023-12-01 06:58:02 25 4
gpt4 key购买 nike

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 访问它时显示此错误:

HTTP method GET is not supported by this URL

最佳答案

您的方法中的 HttpServletResponse 和 HttpServletRequest 参数颠倒了。

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

关于java - 为什么我收到 'HTTP method GET is not supported by this URL' 错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6561585/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com