Issue
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.
Solution
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);
Answered By - Sachin Parse
Answer Checked By - Dawn Plyler (JavaFixing Volunteer)