在这里学习一些 Java Web 开发并尝试启动我的第一个 servlet。但我收到此错误: HTTP method GET is not supported by this URL
description The specified HTTP method is not allowed for the requested resource (HTTP method GET is not supported by this URL).
我在我的 html 表单中使用了“get”方法,如您所见,我已经实现了 doget 方法。但我不确定为什么会收到此错误。会不会和我的有关系web.xml
?
我尝试通过将 html 方法更改为“post”并使用 doPost 方法来使用 POST 方法,但我也得到了等效的错误。我这样做只是为了测试,我不想使用 post。
网页.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>
小服务程序代码:
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 表单中使用了“get”方法。不知道为什么我会收到此错误。