簡體   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