GET HTTP method not supported by this url

I get error prone, could you 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> 
+6
source share
2 answers

You have the wrong parameters: first there should be a request, then an answer, for example:

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

So, currently you are not actually overriding the superclass method.

This is why the @Override annotation @Override so important that it allows you to find such errors at compile time. If you decorated your method with @Override , the compiler would notice that you were trying to override the method signature, which was not there.

+12
source

Is this also not suitable for POST?

Should <servlet-class>test.FirstClass not be <servlet-class>FirstClass instead?

0
source

Source: https://habr.com/ru/post/887256/


All Articles