我正在尝试创建一个简单的servlet应用程序,但是当我将它部署到我的Tomcat服务器(8.5)时,localhost:8080为我提供了 405方法不允许错误。 我不知道处理这个错误
我的pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.devcolibri</groupId>
<artifactId>com.devcolibri.servlet</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
</dependencies>
</project>
我的MainServlet.java
package com.devcolibri.servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class MainServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
PrintWriter out = resp.getWriter();
out.print("<h1>Hello Servlet</h1>");
}
}
我的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"
version="3.0">
<servlet>
<servlet-name>mainServlet</servlet-name>
<servlet-class>com.devcolibri.servlet.MainServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>mainServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
答案 0 :(得分:0)
我想我一直错过了显而易见的事情:我怀疑你的war文件没有部署在斜线上。它可以部署在您的工件ID下。要将其部署在主机的根目录上,需要进行更多配置。试试http://localhost:8080/com.devcolibri.servlet/
编辑:
或http://localhost:8080/com.devcolibri.servlet-1.0-SNAPSHOT/它应该是您的战争档案名称。
答案 1 :(得分:-2)
看起来访问servlet的方式不适用于受保护类型的doGet方法。请尝试doGet公开。
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
PrintWriter out = resp.getWriter();
out.print("<h1>Hello Servlet</h1>");
}
您可以在此处参阅有关受保护和公开的更多信息:what is the difference between public service() method and protected service() method of httpservlet