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

The code is below from the book, so it will not be erroneous. But I do not know how to solve this error below. When you delete the doGet () method, the same error!

"HTTP Status 405 - The HTTP GET method is not supported by this URL

import java.io.BufferedInputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class PDFServlet extends HttpServlet{ private static final long serialVersionUID = 1L; @Override protected void doGet(HttpServletRequest request,HttpServletResponse response) throws IOException,ServletException{ this.doPost(request,response); } @Override protected void doPost(HttpServletRequest request,HttpServletResponse response) throws IOException,ServletException{ response.setContentType("application/pdf"); ServletOutputStream out=response.getOutputStream(); File pdf=null; BufferedInputStream buf=null; try{ pdf=new File("C:\\Users\\lk\\Desktop\\Desktop\\ example.pdf"); response.setContentLength((int)pdf.length()); FileInputStream input=new FileInputStream(pdf); buf=new BufferedInputStream(input); int readBytes=0; while((readBytes=buf.read())!=-1) out.write(readBytes); }catch(IOException e){ System.out.println("file not found!"); }finally{ if(out!=null) out.close(); if(buf!=null) buf.close(); } } } 

web.xml:

 <?xml version="1.0" encoding="UTF-8"?> -<web-app xsi:.........." version="2.5"> -<servlet> <description>This is the description of my Java EE component</description> <display-name>This is the display name of my Java EE component</display-name> <servlet-name>PDFServlet</servlet-name> <servlet-class>PDFServlet</servlet-class> </servlet> -<servlet-mapping> <servlet-name>PDFServlet</servlet-name> <url-pattern>/PDFServlet</url-pattern> </servlet-mapping> -<welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> -<login-config> <auth-method>BASIC</auth-method> </login-config> </web-app> 
+6
source share
7 answers

The servlet code seems correct. Specify the web.xml URL and the servlet invocation URL.

There are two main reasons that cause this error:

1) You do not have a valid doGet () method, when you enter the servlet path in the address bar directly, the Tomcat web container will try to call the doGet () method.

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

2) You made an HTTP request from an HTML form, but you do not have the doPost () method to process it. DoGet () cannot process the Publish request.

 public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException{ .... } 

Read @ BalusC answer for more details .: doGet and doPost in servlets

+7
source

I had the same problem just now. "HTTP Status 405 - The HTTP GET method is not supported by this URL." My solution was as follows:

 public abstract class Servlet extends HttpServlet { protected HttpServletRequest req; protected HttpServletResponse resp; @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { this.req = req; this.resp = resp; this.requestManager(); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { this.req = req; this.resp = resp; this.requestManager(); } protected abstract void requestManager() throws IOException; } 

I had a problem in my constructor because "doGet" I was calling super

+11
source

Replace string

 pdf=new File("C:\\Users\\lk\\Desktop\\Desktop\\ example.pdf"); 

from

 pdf=new File("C:/Users/lk/Desktop/Desktop/example.pdf"); 

then try again.

-1
source

you need to do

 <form action="servlet name " method="post"> 

in index.jsp file

-1
source

If the above error occurs, override the doGet() method.

 @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { processRequest(req, resp); //To change body of generated methods, choose Tools | Templates. } 
-1
source

I used the html file. Create a web page. Therefore, when I came across this error. My solution was: Just delete the path "index.html" in my web.xml file. becayse my html file name was the same as "index.html"

-1
source

Each servlet must contain a doGet () method, which is executed by the server by default. so see that you have a doGet method.

-3
source

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


All Articles