【问题标题】:HTTP Status 405 - HTTP method GET is not supported by this URL [duplicate]HTTP 状态 405 - 此 URL 不支持 HTTP 方法 GET [重复]
【发布时间】:2016-02-13 14:42:39
【问题描述】:

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

我无法让页面正常工作,我有我的表单方法要发布,我的 servlet 有 doPost,但是它一直显示我不支持 Post 方法。我只是想做一个简单的网站。

jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
Hello
<%=request.getAttribute("name") %>
</body>
</html>


Servlet


package esempio2;
import javax.servlet.RequestDispatcher;
import javax.servlet.http.*;
import java.io.*;
import javax.servlet.*;

public class Nome extends HttpServlet {
    public void doPost(HttpServletRequest request, HttpServletResponse response)throws IOException, ServletException{

        String name=request.getParameter("username");
        request.setAttribute("nome", name);

        RequestDispatcher view = request.getRequestDispatcher("index.jsp");
        view.forward(request, response);
    }
}

WEB.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<servlet>
<servlet-name>nome</servlet-name>
<servlet-class>esempio2.Nome</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>nome</servlet-name>
<url-pattern>/tester.do</url-pattern>
</servlet-mapping>
</web-app>

【问题讨论】:

  • 设置属性“nome”时输入错误
  • 您的表格在哪里?从异常“此 URL 不支持 HTTP 方法 GET”可以清楚地看出,表单正在请求 GET 方法,而您尚未在 servlet 中定义任何 GET 方法。默认情况下,表单的方法是 GET。您必须将其明确设置为 POST。 HTML Form Method

标签: jsp servlets


【解决方案1】:

尝试添加 doGet() 方法!

protected void doGet(HttpServletRequest request,HttpServletResponse response) throws IOException,ServletException{
  //your code here
}

尝试添加类似这样的内容

<input type="text" name="nome">

所以尝试添加一个包装doGet和doPost的方法,如下所示:

public void doGet(HttpServletRequest request, HttpServletResponse response)throws IOException, ServletException{
        processRequest(request,response);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)throws IOException, ServletException{
        processRequest(request,response);
    }

    public void processRequest(HttpServletRequest request, HttpServletResponse response)throws IOException, ServletException{
        String nome = "PutYourNameHere";
        request.setAttribute("nome",nome);

        RequestDispatcher view = request.getRequestDispatcher("index.jsp");
        view.forward(request, response);
    }

【讨论】:

  • 为什么 doGet 是必须的?其背后的任何具体原因。
  • 所以正确的解决方案是在表单提交本身中将方法更改为 POST。
【解决方案2】:

看来你的表单方法不正确

【讨论】:

    猜你喜欢
    • 2012-12-14
    • 2014-05-11
    • 2015-03-10
    • 2020-07-16
    • 2016-07-18
    • 2016-10-30
    • 1970-01-01
    • 2023-03-26
    相关资源
    最近更新 更多