[英]Servlet error HTTP Status 405 - HTTP method GET is not supported by this URL
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
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>
您的servlet没有/ergasia/Search1
url-pattern,而是尝试以下方法:
<form method="post" action="Search_proteins">
When, we write method="get" in form action, doGet() is written in Servlets, if method="post", then doPost() is written. 当我们在表单操作中编写method =“ get”时,将doGet()写在Servlet中,如果method =“ post”,则编写doPost()。 But your error "HTTP method GET is not supported by this URL" is wired because your code form action and doPost() method is correct. 但是,由于您的代码形式操作和doPost()方法正确,因此连接了错误“此URL不支持HTTP方法GET” 。 I think some configuration are wrong in web.xml. 我认为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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.