HTTP Status 405 - HTTP method GET is not supported by this URL [duplicate]
Asked Answered
C

7

6

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 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>
Condonation answered 24/8, 2012 at 8:18 Comment(0)
S
12

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. : doGet and doPost in Servlets

Slovak answered 24/8, 2012 at 9:6 Comment(5)
Servlet calling URL:"localhost:8080/Test/PDFServlet" (Test:the project's name)Condonation
Now i don't know,but a new problem occur:in the myeclipse window,output is always:"file not found!",why? my path of file is right.Condonation
I solved the problem,thanks a lot! finally,it is the problem about the server,i'll take care of it.Condonation
Good to hear that the problem is resolved. You are wel-cpomeSlovak
@Condonation so what was the problem?Archle
O
14

I had this same problem just now. "HTTP Status 405 - HTTP method GET 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 problem in my constructor, because the "doGet" I was calling the super

Oeflein answered 28/5, 2013 at 18:20 Comment(1)
This one is what caught me - I thought that when overriding a method it was standard practice to call the parent's method. Calling super.doGet(req, resp) caused this error. (drove me crazy because a break point in my doGet was hit... then the server would respond with the 405.. despite the fact I was explicitly setting the status)Reenareenforce
S
12

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. : doGet and doPost in Servlets

Slovak answered 24/8, 2012 at 9:6 Comment(5)
Servlet calling URL:"localhost:8080/Test/PDFServlet" (Test:the project's name)Condonation
Now i don't know,but a new problem occur:in the myeclipse window,output is always:"file not found!",why? my path of file is right.Condonation
I solved the problem,thanks a lot! finally,it is the problem about the server,i'll take care of it.Condonation
Good to hear that the problem is resolved. You are wel-cpomeSlovak
@Condonation so what was the problem?Archle
L
-1

Replace the line

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

with

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

and then proceed again.

Lowlife answered 6/3, 2013 at 6:28 Comment(0)
K
-1

you need to do

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

in your index.jsp file

Kopaz answered 9/7, 2015 at 11:15 Comment(0)
F
-1

When the above error appears then override 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.
    }
Fustigate answered 2/2, 2016 at 9:57 Comment(0)
N
-1

I was using html file. To create web page. So when I encountered with this error. My solution was: Just to remove " index.html " path in my web.xml file. becayse my html file name was the same as "index.html"

Natka answered 14/8, 2016 at 20:39 Comment(0)
S
-3

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

Starflower answered 20/11, 2013 at 6:40 Comment(1)
no they don't. If you plan on supporting GET requests using standard servlet interface then yes, but you don't have to do that.Archle

© 2022 - 2024 — McMap. All rights reserved.