![](/img/trans.png)
[英]Tomcat error HTTP Status 405 - HTTP method GET is not supported by this URL
[英]Reason for error : HTTP Status 405 - HTTP method GET is not supported by this URL
我不知道為什么當我嘗試運行NewFile.html時在Eclipse Web瀏覽器中出現此錯誤。 在我的Dummy動態Web項目中,我可以從NewFile.html中“調用”兩個servlet之一。 但是,我故意將我的web.xml設置為使得對此Dummy的任何url請求都將僅發送到Servlet1。 我看到了有關SO的相關文章,但是根據那里給出的答案,我的代碼沒有發現錯誤。 我不知道我哪里錯了。 請幫助我完成這項工作。 我的代碼如下。
NewFile.html:
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h2>All roads will lead to servlet 1</h2>
<form method="POST" action="/Dummy/Servlet1">
<input type="submit" name="submit" value="Servlet1" />
</form>
<form method="POST" action="/Dummy/Servlet2">
<input type="submit" name="submit" value="Servlet2" />
</form>
</body>
</html>
Servlet 1或2:
package foo;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Servlet1 extends HttpServlet {
private static final long serialVersionUID = 1L;
public Servlet1() {
super();
// TODO Auto-generated constructor stub
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<h1>Welcome to Servlet - 1</h1>");
}
}
web.xml:
<servlet>
<description></description>
<display-name>Servlet1</display-name>
<servlet-name>Servlet1</servlet-name>
<servlet-class>foo.Servlet1</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Servlet1</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
編輯-
僅添加到Servlet1的方法
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
RequestDispatcher dispatcher = request.getRequestDispatcher("/html/NewFile.html");
dispatcher.forward(request, response);
}
項目結構
似乎您正在將GET請求發送到/context/NewFile.html
。 您的web.xml已將Servlet1
映射為使用/*
模式/*
,所有內容)處理請求。 因此, Servlet1
用於處理對/NewFile.html
的請求,但是它沒有GET請求的處理程序方法,因此它以405響應。
嘗試進行以下修改:-首先對html操作使用以下命令:
...
<form method="POST" action="Servlet1">
...
然后在web.xml中使用以下映射:
...
<servlet-mapping>
<servlet-name>Servlet1</servlet-name>
<url-pattern>/Servlet1</url-pattern>
</servlet-mapping>
...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.