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

时间:2015-11-12 09:57:02

标签: jsp servlets

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>

2 个答案:

答案 0 :(得分:0)

尝试添加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);
    }

答案 1 :(得分:-1)

看起来你的表单方法不正确