Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTTP method GET is not supported by this URL

Tags:

servlets

I am getting the subjected error, could you please help

servlet

public class FirstClass extends HttpServlet {

    private static final long serialVersionUID = 1L;

    public void doGet(HttpServletResponse response, HttpServletRequest request) throws IOException, ServletException {
        PrintWriter out = response.getWriter();
        out.println("this is a sample");
        out.flush();
    }

    public void doPost(HttpServletResponse response, HttpServletRequest request) throws IOException, ServletException {
        PrintWriter out = response.getWriter();
        out.println("this is a sample");
        out.flush();
    }
}

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
    <display-name>hii</display-name>

    <servlet>
        <servlet-name>First</servlet-name>
        <servlet-class>test.FirstClass</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>First</servlet-name>
        <url-pattern>/first.do</url-pattern>
    </servlet-mapping>

    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
    </welcome-file-list>
</web-app>

index.html

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
    <a href="first.do">Click Me</a>
</body>
</html>
like image 890
user1448652 Avatar asked May 04 '11 06:05

user1448652


People also ask

What does HTTP method GET is not supported by this URL mean?

It's an HTTP response status code that indicates that the request method is known by the server but is not supported by the target resource.

What is Error 405 Request method GET not supported?

The HyperText Transfer Protocol (HTTP) 405 Method Not Allowed response status code indicates that the server knows the request method, but the target resource doesn't support this method.

When should I use HTTP 405?

The 405 HTTP Status Code means that the method is not allowed. In the 405 HTTP Status Code, the Request-Line isn't considered the resource distinguished by the Request-URI. The response should incorporate an Allow header containing a rundown of substantial strategies for the mentioned asset.


1 Answers

You've got the parameters the wrong way round - it should be the request first, then the response, like this:

public void doGet(HttpServletRequest request, HttpServletResponse response)
    throws IOException, ServletException {

So currently you're not actually overriding the superclass method.

This is why the @Override annotation is so important - it lets you find bugs like this at compile time. If you'd decorated your method with @Override, the compiler would have spotted that you were trying to override a method signature which didn't exist.

like image 121
Jon Skeet Avatar answered Oct 27 '22 20:10

Jon Skeet