Issue
I am trying to create a simple servlet app, but when I deploy it into my Tomcat server (8.5), localhost:8080 gives me 405 method not allowed error. I don't know to handle this error
my 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>
My 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>");
}
}
My 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>
Solution
I think I've been missing the obvious: I suspect that your war file is not being deployed on slash. It may be deployed under your artifact ID. To deploy it on the root of the host requires more configuration. Try http://localhost:8080/com.devcolibri.servlet/
Edit:
or http://localhost:8080/com.devcolibri.servlet-1.0-SNAPSHOT/ It should be your war file name.
Answered By - Jon Sampson
Answer Checked By - Marilyn (JavaFixing Volunteer)