在此学习一些Java Web开发并尝试启动我的第一个servlet。但我收到此错误:HTTP method GET is not supported by this URL
获取这两个HTTP方法GET不受此URL支持,并且HTTP方法POST不受此URL支持
description The specified HTTP method is not allowed for the requested resource (HTTP method GET is not supported by this URL).
我用我的HTML表单的“获得”的方法,正如你可以看到我已经实现了doGet方法。但我不知道为什么我会收到此错误。这可能与我的web.xml
有关吗?
我尝试使用POST方法,将html方法更改为'post',并使用doPost方法,但我也得到了等效的错误。我只做了这个测试,我不想使用帖子。
的web.xml:
<web-app
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">
<servlet>
<servlet-name>AppleFox</servlet-name>
<servlet-class>com.AppleFox.web.ProcessQuery</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>AppleFox</servlet-name>
<url-pattern>/ProcessQuery.do</url-pattern>
</servlet-mapping>
</web-app>
servlet代码:
package com.AppleFox.web;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class ProcessQuery
*/
public class ProcessQuery<HttpServletRequest> extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String query = ((ServletRequest) request).getParameter("query");
out.println("Sorry we could find any results for " + query + ".");
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
return;
}
}
我也用我的HTML形式的 '得到' 方法。不知道为什么我得到这个错误。
哇,谢谢!我从未想过这件事。 –