HTTP方法GET不受此URL支持

HTTP方法GET不受此URL支持

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

问题描述

我写了下面的Servlet(Search1.java):

  package ergasia; 

import java.sql。*;
import java.io. *;
import javax.servlet。*;
import javax.servlet.http。*;
import java.util.ArrayList;
$ b $ public class Search1 extends HttpServlet
{
@Override $ b $ public void doPost(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException
{
response.setContentType(text / html);
连接连接=空;
String url =jdbc:mysql:// localhost:3306 /;
String dbName =ergasia;
String user =root;
字符串密码=密码;
PreparedStatement selectProteins = null;
ResultSet resultSet = null;
ArrayList al = null;

尝试{
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(){
returninfo;
}
}

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

 < form method =postaction =/ ergasia / Search1> 

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



以下是我的web.xml文件:

 < xml version =1.0encoding =UTF-8?> 
< web-app version =2.5xmlns =http://java.sun.com/xml/ns/javaeexmlns:xsi =http://www.w3.org/2001/ XMLSchema-instancexsi: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>

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

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

解决方案

您的servlet没有像 / ergasia / Search1 ,试试这个:

 < form method =postaction = Search_proteins > 


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 URLtype:Status reportmessage:HTTP method GET is not supported by this URLdescription: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

解决方案

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

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

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

09-03 19:04