java Servlet 错误 HTTP 状态 405 - 此 URL 不支持 HTTP 方法 GET

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27499323/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 11:53:54  来源:igfitidea点击:

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

javajspservletspostget

提问by Luna NA

I've written the following Servlet (Search1.java):

我编写了以下 Servlet (Search1.java):

package ergasia;

import java.sql.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.ArrayList;

public class Search1 extends HttpServlet
{       
   @Override
   public void doPost(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException 
   {
    response.setContentType("text/html");    
    Connection connection= null;    
    String url = "jdbc:mysql://localhost:3306/";
    String dbName = "ergasia";    
    String user = "root";
    String password = "password"; 
    PreparedStatement selectProteins = null;
    ResultSet resultSet = null;  
    ArrayList al = null;

        try {            
            connection = DriverManager.getConnection(url + dbName, user, password);
            String keyword = request.getParameter("keyword");            
            selectProteins = connection.prepareStatement("SELECT * FROM protein WHERE proteinName LIKE ?");
            selectProteins.setString(1, "%" + keyword + "%");
            resultSet = selectProteins.executeQuery();            

            ArrayList keyword_list = new ArrayList();             

                while (resultSet.next()) {
                    al = new ArrayList();
                    al.add(resultSet.getString(1));
                    al.add(resultSet.getString(2));
                    al.add(resultSet.getString(3));
                    al.add(resultSet.getString(4));
                    al.add(resultSet.getString(5));
                    al.add(resultSet.getString(6));
                    al.add(resultSet.getString(7));                
                    keyword_list.add(al);
                }

            request.setAttribute("results", keyword_list);        
            RequestDispatcher view = request.getRequestDispatcher("/search_proteins.jsp");
            view.forward(request, response);

        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    @Override
    public String getServletInfo() {
        return "info";
    }
}  

that I access from a jsp page with the following command:

我使用以下命令从 jsp 页面访问:

<form method="post" action="/ergasia/Search1"> 

but when I try to run it tomcat gives me the following error: HTTP Status 405 - HTTP method GET is not supported by this URL type:Status report message:HTTP method GET is not supported by this URL description:The specified HTTP method is not allowed for the requested resource.

但是当我尝试运行它时,tomcat 给了我以下错误:HTTP 状态 405 - 此 URL 类型不支持 HTTP 方法 GET:状态报告消息:此 URL 不支持 HTTP 方法 GET 说明:指定的 HTTP 方法不是允许请求的资源。

Here's my web.xml file too:

这也是我的 web.xml 文件:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">        
    <servlet>
        <servlet-name>Search_proteins</servlet-name>
        <servlet-class>ergasia.Search1</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>Search_proteins</servlet-name>
        <url-pattern>/Search_proteins</url-pattern>
    </servlet-mapping>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
</web-app>

Could you please help me find what I've done wrong?

你能帮我找出我做错了什么吗?

Unfortunately I can't post images yet, so here is my configuration, maybe it'll help:1

不幸的是我还不能发布图片,所以这是我的配置,也许它会有所帮助:1

采纳答案by Java

You servlet doesn't have url-pattern as /ergasia/Search1, try this instead:

你的 servlet 没有 url-pattern as /ergasia/Search1,试试这个:

<form method="post" action="Search_proteins"> 

回答by Java

Try this one :

试试这个:

<form action="/Search_proteins" method="post">

with url mapping as :

使用 url 映射为:

<servlet-mapping>
   <servlet-name>Search_proteins</servlet-name>
        <url-pattern>/Search_proteins</url-pattern>
</servlet-mapping>

回答by Ye Win

When, we write method="get" in form action, doGet() is written in Servlets, if method="post", then doPost() is written. But your error "HTTP method GET is not supported by this URL"is wired because your code form action and doPost() method is correct. I think some configuration are wrong in web.xml.

当,我们在form action中写method="get"时,在Servlets中写doGet(),如果method="post",则写doPost()。但是您的错误“此 URL 不支持 HTTP 方法 GET”是错误的,因为您的代码表单操作和 doPost() 方法是正确的。我认为 web.xml 中的某些配置是错误的。

Please change below code in your web.xml.

请在您的 web.xml 中更改以下代码。

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">        

 <servlet>    
    <servlet-name>eservlet</servlet-name>
    <servlet-class>ergasia</servlet-class>
 </servlet>

 <servlet-mapping>  
    <servlet-name>eservlet</servlet-name>
    <url-pattern>/Search1</url-pattern>    
 </servlet-mapping>

        <welcome-file-list>
            <welcome-file>index.jsp</welcome-file>
        </welcome-file-list>
    </web-app>