HTTP状态405-此URL不支持HTTP方法GET [英] HTTP Status 405 - HTTP method GET is not supported by this URL

查看:363
本文介绍了HTTP状态405-此URL不支持HTTP方法GET的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的代码来自一本书,所以它不会是错误的.但是我不知道如何解决以下错误.删除方法doGet()时,会出现同样的错误!

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状态405-此URL不支持HTTP方法GET"

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>

推荐答案

Servlet代码似乎正确. 提供web.xml条目和Servlet调用URL.

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

导致此错误的主要原因有两个:

There are two main reasons which cause this error:

1)您没有有效的doGet()方法,当您直接在地址栏中键入servlet的路径时,像Tomcat这样的Web容器将尝试调用doGet()方法.

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)您从HTML表单发出了HTTP发布请求,但是没有doPost()方法来处理它. doGet()无法处理发布"请求.

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{
....
}

有关更多详细信息,请阅读@BalusC的答案. : Servlet中的doGet和doPost

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

这篇关于HTTP状态405-此URL不支持HTTP方法GET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆