简体   繁体   中英

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:

HTTP method GET is not supported by this URL

You have the HttpServletResponse and HttpServletRequest parameters reversed on your methods.

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)

This happened to me when my method call

"super.doGet(req, resp)" or "super.doPost(req, resp)".

After i removed above super class calling from the doGet and doPost it worked fine.

Infact those super class calling codes were inserted by the Eclipse IDE template.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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