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

﹥>﹥吖頭↗ 提交于 2019-12-12 01:53:15

问题


I've written the following 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:

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

Here's my web.xml file too:

<?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:


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

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



回答2:


Try this one :

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

with url mapping as :

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



回答3:


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.

Please change below code in your 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>


来源:https://stackoverflow.com/questions/27499323/servlet-error-http-status-405-http-method-get-is-not-supported-by-this-url

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!