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

时间:2014-12-16 07:10:03

标签: java jsp servlets post get

我编写了以下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";
    }
}  

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

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

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

这里也是我的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>

你能帮我找到我做错的事吗?

很遗憾,我还无法发布图片,所以这是我的配置,也许它可以提供帮助:1

3 个答案:

答案 0 :(得分:0)

试试这个:

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

将网址映射为:

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

答案 1 :(得分:0)

您的servlet没有/ergasia/Search1的url-pattern,请尝试这样做:

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

答案 2 :(得分:0)

当我们在表单操作中写method =“get”时,doGet()用Servlet编写,如果method =“post”,则编写doPost()。但是您的错误“此方法不支持HTTP方法GET”已连线,因为您的代码格式操作和doPost()方法是正确的。我认为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>