Categories
forms http-status-code-404 java jsp servlets

Servlet returns “HTTP Status 404 The requested resource (/servlet) is not available”

113

I have an HTML form in a JSP file in my WebContent/jsps folder. I have a servlet class servlet.java in my default package in src folder. In my web.xml it is mapped as /servlet.

I have tried several URLs in action attribute of the HTML form:

<form action="/servlet">
<form action="/servlet.java">
<form action="/src/servlet.java">
<form action="../servlet.java">

But none of those work. They all keep returning a HTTP 404 error like below in Tomcat 6/7/8:

HTTP Status 404 — /servlet

Description: The requested resource (/servlet) is not available.

Or as below in Tomcat 8.5/9:

HTTP Status 404 — Not Found

Message: /servlet

Description: The origin server did not find a current representation for the target resource or is not willing to disclose that one exists

Or as below in Tomcat 10:

HTTP Status 404 — Not Found

Type: Status Report

Message: The requested resource (/servlet) is not available

Description: The origin server did not find a current representation for the target resource or is not willing to disclose that one exists

Why is it not working?

0

    4

    Scenario #1: You accidentially re-deployed from the command line while tomcat was already running.

    Short Answer: Stop Tomcat, delete target folder, mvn package, then re-deploy


    Scenario #2: request.getRequestDispatcher(“MIS_SPELLED_FILE_NAME.jsp”)

    Short Answer: Check file name spelling, make sure case is correct.


    Scenario #3: Class Not Found Exceptions
    (Answer put here because: Question# 17982240 )
    (java.lang.ClassNotFoundException for servlet in tomcat with eclipse )
    (was marked as duplicate and directed me here )

    Short Answer #3.1: web.xml has wrong package path in servlet-class tag.

    Short Answer #3.2: java file has wrong import statement.


    Below is further details for Scenario #1:


    1: Stop Tomcat

    • Option 1: Via CTRL+C in terminal.
    • Option 2: (terminal closed while tomcat still running)
    • ———— 2.1: press:Windows+R –> type:”services.msc
    • ———— 2.2: Find “Apache Tomcat #.# Tomcat#” in Name column of list.
    • ———— 2.3: Right Click –> “stop

    2: Delete the “target” folder.
    (mvn clean will not help you here)

    3: mvn package

    4: YOUR_DEPLOYMENT_COMMAND_HERE

    (Mine: java -jar target/dependency/webapp-runner.jar –port 5190 target/*.war )

    Full Back Story:


    Accidentially opened a new git-bash window and
    tried to deploy a .war file for my heroku project via:

    java -jar target/dependency/webapp-runner.jar –port 5190 target/*.war

    After a failure to deploy, I realized I had two git-bash windows open,
    and had not used CTLR+C to stop the previous deployment.

    I was met with:

    HTTP Status 404 – Not Found Type Status Report

    Message /if-student-test.jsp

    Description The origin server did not find a current representation
    for the target resource or is not willing to disclose that one
    exists.

    Apache Tomcat/8.5.31

    Below is further details for Scenario #3:


    SCENARIO 3.1:
    The servlet-class package path is wrong
    in your web.xml file.

    It should MATCH the package statement at top
    of your java servlet class.

    File: my_stuff/MyClass.java:

       package my_stuff;
    

    File: PRJ_ROOT/src/main/webapp/WEB-INF/web.xml

       <servlet-class>
       my_stuff.MyClass
       </servlet-class>
    

    SCENARIO 3.2:

    You put the wrong “package” statement
    at top of your myClass.java file.

    For example:

    File is in: “/my_stuff” folder

    You mistakenly write:

    package com.my_stuff
    

    This is tricky because:

    1: The maven build (mvn package) will not report any errors here.

    2: servlet-class line in web.xml can have CORRECT package path. E.g:

    <servlet-class>
    my_stuff.MyClass
    </servlet-class>
    

    Stack Used:
    Notepad++ + GitBash + Maven + Heroku Web App Runner + Tomcat9 + Windows10:

    1

    • Your AppName.war and thus the exploded folder name doesn’t match your expected name, for example when your war file is versioned like AppName-1.0-SNAPSHOT.war and you’re trying /AppName/.

      – jla

      Oct 30, 2018 at 23:41

    1

    Check if you have entered the correct URL Mapping as specified in the Web.xml

    For example:

    In the web.xml, your servlet declaration maybe:

    <servlet>
            <servlet-name>ControllerA</servlet-name>
            <servlet-class>PackageName.ControllerA</servlet-class>
    </servlet>
    
    <servlet-mapping>
            <servlet-name>ControllerA</servlet-name>
            <url-pattern>/theController</url-pattern>
    </servlet-mapping>
    

    What this snippet does is <url-pattern>/theController</url-pattern>will set the name that will be used to call the servlet from the front end (eg: form) through the URL. Therefore when you reference the servlet in the front end, in order to ensure that the request goes to the servlet “ControllerA”, it should refer the specified URL Pattern “theController” from the form.

    eg:

    <form action="theController" method="POST">
    </form>