Home:ALL Converter>Servlet mapping and HTTP status 404

Servlet mapping and HTTP status 404

Ask Time:2015-05-09T04:06:58         Author:harry-potter

Json Formatter

I have a problem with my Servlet implementation. I'm using Apache Tomcat as the Servlet engine and Eclipse as the IDE. First I created a search.html as follow:

<html>
    <head>
    <meta charset="UTF-8">
    <title>FirstServler</title>
    </head>
    <body>
        <form action="/myServlet" method="get">
        Name : <INPUT TYPE="text" NAME="name" SIZE="18"/>
        <input type="submit" value="OK">
        </form>
    </body>
</html>

Then I created a servlet, called Servlet1:

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/Servlet1")
public class Servlet1 extends HttpServlet
{
    private static final long serialVersionUID = 1L;

    public Servlet1() 
    {
        super();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
    {
        String s= request.getParameter("name");
        response.getWriter().write(s);
    }
}

And finally the web.xml, located in WebContent\WEB-INF:

<?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"   id="WebApp_ID" version="3.0">
  <display-name>Servlet</display-name>

  <servlet>
    <servlet-name>servlet1</servlet-name>
    <servlet-class>Servlet1</servlet-class>
 </servlet>
  <servlet-mapping>
    <servlet-name>servlet1</servlet-name>
    <url-pattern>/myServlet</url-pattern>
  </servlet-mapping>
  </web-app>

When I enter data inside the input box of search.html and press "OK" I get the following error:

HTTP Status 404 - /Servlet1.0/es1
type Status report
message /Servlet1.0/es1
description The requested resource is not available.

I think there are some mistakes with web.xml. In fact, if I change the url-pattern into es1, I don't get an error.

Author:harry-potter,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/30132056/servlet-mapping-and-http-status-404
yy