Servlets HTTP状态405-此URL不支持HTTP方法GET
下面的代码来自一本书,所以不会有错误。但我不知道如何解决下面的错误。删除方法doGet()时,同样的错误 “HTTP状态405-此URL不支持HTTP方法GET” web.xml:Servlets HTTP状态405-此URL不支持HTTP方法GET,servlets,http-get,http-status-code-405,Servlets,Http Get,Http Status Code 405,下面的代码来自一本书,所以不会有错误。但我不知道如何解决下面的错误。删除方法doGet()时,同样的错误 “HTTP状态405-此URL不支持HTTP方法GET” web.xml: <?xml version="1.0" encoding="UTF-8"?> -<web-app xsi:.........." version="2.5"> -<servlet> <description>This is the description of my
<?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>
- Servlet代码似乎是正确的。
提供web.xml
条目和Servlet调用URL
导致此错误的主要原因有两个:
1) 您没有有效的doGet()方法,当您直接在地址栏中键入servlet的路径时,像Tomcat这样的web容器将尝试调用doGet()方法
2) 您从HTML表单发出了HTTP post请求,但没有doPost()方法来处理它。doGet()无法处理“Post”请求
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws IOException{
....
}
阅读@BalusC的答案了解更多详情: 更换管路
pdf=new File("C:\\Users\\lk\\Desktop\\Desktop\\ example.pdf");
与
然后再继续。我刚才也遇到了同样的问题。“HTTP状态405-此URL不支持HTTP方法GET”。我的解决办法如下:
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;
}
我的构造函数有问题,因为我调用super的“doGet”必须包含doGet()方法,默认情况下由服务器执行。
所以请注意,你有多吉特方法 你需要做什么
<form action="servlet name " method="post">
在index.jsp文件中,当出现上述错误时,请重写doGet()
方法
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
processRequest(req, resp); //To change body of generated methods, choose Tools | Templates.
}
我使用的是html文件。创建网页。
所以当我遇到这个错误时。我的解决办法是:
只需删除web.xml文件中的“index.html”路径。
因为我的html文件名与“index.html”相同您也能提供您的web.xml文件吗?Servlet调用URL:”(测试:项目名称)现在我不知道,但出现了一个新问题:在myeclipse窗口中,输出总是:“找不到文件!”,为什么?我的文件路径是正确的。我解决了这个问题,非常感谢!最后,是服务器的问题,我会处理好的。很高兴听到问题解决了。你很好-cpome@itzyjr那么问题出在哪里呢?这一点让我很吃惊——我认为当重写一个方法时,调用父方法是标准的做法。调用super.doGet(req,resp)导致此错误。(让我发疯,因为我的doGet中有一个断点被击中……然后服务器会用405响应……尽管我明确设置了状态)不,他们没有。如果您计划使用标准servlet接口支持GET请求,那么是的,但您不必这样做。
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;
}
<form action="servlet name " method="post">
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
processRequest(req, resp); //To change body of generated methods, choose Tools | Templates.
}