itzyjr itzyjr - 7 months ago 52
Java Question

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

The code below is from a book,so it'll not be incorrect.But I don't know how to solve this below error.When delete the method doGet(),the same error!

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

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;
protected void doGet(HttpServletRequest request,HttpServletResponse response)
throws IOException,ServletException{
protected void doPost(HttpServletRequest request,HttpServletResponse response)
throws IOException,ServletException{
ServletOutputStream out=response.getOutputStream();
File pdf=null;
BufferedInputStream buf=null;
pdf=new File("C:\\Users\\lk\\Desktop\\Desktop\\ example.pdf");
FileInputStream input=new FileInputStream(pdf);
buf=new BufferedInputStream(input);
int readBytes=0;
while((!=-1) out.write(readBytes);
}catch(IOException e){
System.out.println("file not found!");
if(out!=null) out.close();
if(buf!=null) buf.close();


<?xml version="1.0" encoding="UTF-8"?>
-<web-app xsi:.........." version="2.5">
<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>


The Servlet code seems correct. Provide web.xml entry and Servlet calling URL.

There are two main reasons which cause this error:

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

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

2) You made a HTTP post request from a HTML form, but you do not have a doPost() method to handle it. The doGet() can not handle the “Post” request.

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

Read @BalusC's answer for more details. : Servlets: doGet and doPost