我正在关注来自https://www.mkyong.com/servlet/a-simple-servlet-example-write-deploy-run/网站的简单示例,并尝试运行一个简单的servlet。 简单的应用程序称为httpservlet。我在应用程序的$ CATALINA_HOME / webapps目录中创建了一个单独的目录。 servlet的代码如下
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class helloservlet extends HttpServlet{
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException{
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<body>");
out.println("<h1>Hello Servlet Get</h1>");
out.println("</body>");
out.println("</html>");
}
}
它只是在浏览器上打印hello servlet消息。 helloservlet.java文件已成功编译到
中$CATALINA_HOME/webapps/httpservlet/WEB-INF/classes
目录。 对应的web.xml文件具有以下内容
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" 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
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<servlet>
<servlet-name>hello</servlet-name>
<servlet-class>helloservlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>hello</servlet-name>
<url-pattern>/Demo1</url-pattern>
</servlet-mapping>
</web-app>
当我输入
localhost:8181/httpservlet/Demo1
在地址栏中我得到了
HTTP Status 405 - HTTP method GET is not supported by this URL
type Status report
message HTTP method GET is not supported by this URL
description The specified HTTP method is not allowed for the requested resource.
Apache Tomcat/6.0.45
我猜我在web.xml文件中犯了一个错误。任何有助于解决错误的帮助都将受到高度赞赏