0

I began studying servlets. Code Servlet:

package arver;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * Created by 35717 on 30.03.2016.
 */
public class MainServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doGet(req, resp);
        PrintWriter out = resp.getWriter();
        out.print("servlet");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doPost(req, resp);
    }
}

File web.xml

<?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xmlns="http://java.sun.com/xml/ns/javaee"
             xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
             version="3.0">

        <servlet>
            <servlet-name>MainServlet</servlet-name>
            <servlet-class>arver.MainServlet</servlet-class>
        </servlet>

        <servlet-mapping>
            <servlet-name>MainServlet</servlet-name>
            <url-pattern>/</url-pattern>
        </servlet-mapping>

    </web-app>

Server response: HTTP Status 405 - HTTP method GET is not supported by this URL

type Status report

message HTTP method GET is not supported by this URL

description The specified HTTP method is not allowed for the requested resource.

Apache Tomcat/9.0.0.M4

why am I getting HTTP Status 405 - HTTP method GET is not supported by this URL error in this Program.

2 Answers2

5

We extend the HttpServlet and @Override doPost but in our implementation we don't call its super since call to the super will give this message.

When you do super.doGet(request, response); in your Servlet's doGet() method, you actually call the doGet() of the HttpServlet class. So drop the super call. It's not needed.

Just remove these lines :

super.doGet(req, resp);
super.doPost(req, resp);
frianH
  • 5,901
  • 6
  • 13
  • 36
Sachin Parse
  • 965
  • 9
  • 12
1

Use Either get or Post Method

MainServlet.java

package arver;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * Created by 35717 on 30.03.2016.
 */
public class MainServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException 
{        
        PrintWriter out = resp.getWriter();
        out.print("servlet");
    }
}

web.xml file

<?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xmlns="http://java.sun.com/xml/ns/javaee"
             xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
             version="3.0">
<welcome-file-list>
    <welcome-file>index.html</welcome-file>
</welcome-file-list>

        <servlet>
            <servlet-name>MainServlet</servlet-name>
            <servlet-class>arver.MainServlet</servlet-class>
        </servlet>

        <servlet-mapping>
            <servlet-name>MainServlet</servlet-name>
            <url-pattern>/MainServlet</url-pattern>
        </servlet-mapping>

    </web-app>

Url must be like this: http://localhost:8080/Project name/MainServlet

khaja firoz
  • 221
  • 2
  • 14
  • Yes, I understand that it is not nice to display all the tags by means response and that proper use jsp,but I would like to understand why I get this error, I thought I could out.print(""); out.print(""); out.print(""); out.print(""); out.print(""); out.print(""); out.print("servlet"); out.print(""); out.print(""); – Александр Mar 30 '16 at 09:30
  • you should not use get and post methods in one servlet.you have to use either get or post method – khaja firoz Mar 30 '16 at 09:35
  • thanks for the help, unfortunately do not have enough reputation to be noted as a useful response. – Александр Mar 30 '16 at 09:38