0

Getting a 404 error on click on submit button on the form. Getting error (The requested resource is not available.) for the URL mapping http://localhost:8080/HelloWorld/HelloServlet. I tried the reference this as well, Servlet returns "HTTP Status 404 The requested resource (/servlet) is not available" but this also doesn't seem to work.

1, index,html

   <!DOCTYPE html>
   <html>
   <head>
   <meta charset="ISO-8859-1">
   <title>Insert title here</title>
   </head>
   <body>
      <form action="HelloServlet">
        <input type="submit" value="HIT">
      </form>
   </body>
   </html>

2. HelloServlet

        package com.example.aman;

        import java.io.IOException;
        import javax.servlet.ServletConfig;
        import javax.servlet.ServletException;
        import javax.servlet.annotation.WebServlet;
        import javax.servlet.http.HttpServlet;
        import javax.servlet.http.HttpServletRequest;
        import javax.servlet.http.HttpServletResponse;

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

        /**
        * Default constructor. 
        */
        public HelloServlet() {
        // TODO Auto-generated constructor stub
        }

        /**
         * @see Servlet#init(ServletConfig)
        */
        public void init(ServletConfig config) throws ServletException {
        // TODO Auto-generated method stub
        System.out.println("DIVESH");
        }

        /**
         * @see HttpServlet#doGet(HttpServletRequest request, 
        HttpServletResponse response)
        */
        protected void doGet(HttpServletRequest request, HttpServletResponse 
        response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        response.getWriter().append("Served at: 
        ").append(request.getContextPath());
        response.getWriter().println("DIVESH");
        }

        /**
         * @see HttpServlet#doPost(HttpServletRequest request, 
         HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }

}

3. 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" id="WebApp_ID" version="3.0">
  <display-name>HelloWorld</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
</web-app>
Aman
  • 46
  • 6

1 Answers1

0

http://localhost:8080/HelloWorld/HelloServlet.

Straight away that tells me what the problem is. You are trying to access a servlet from within the same folder directory. This is what happens if you are not careful with your relative URLS

In your form, change the URL mapping to this:

   <form action="/HelloServlet">
        <input type="submit" value="HIT">
      </form>

Or if that didn't work, do this:

   <form action="../HelloServlet">
        <input type="submit" value="HIT">
      </form>

Adding a / to the path makes it absolute.

Adding ../ makes it go to the parent of the current directory.

Hope that helps.

Jonathan Laliberte
  • 2,672
  • 4
  • 19
  • 44