0

I have a form in which I want to submit request to a servlet called registrationServlet.The register servlet is under a directory called "Controller". But it fails to access the servlet after submitting the form. How can I resolve this issue? I get the following error after submitting the form

HTTP Status 404 - /ClassProject/Views/RegisterServlet

description : The requested resource is not available.

form : (Form resides in Login.jsp page under Views directory)

    <form action="RegisterServlet" class="register" method="POST" >
        <fieldset>
            <legend>Registration</legend>
            Username : <input type="text" name="username" id="username">
            </br></br>

            Password : <input type="password" name="password" id="password">
            </br></br>
            Email : <input type="email" name="email" id="email">
            </br></br>
            Date of Birth : <input type="date" name="date" id="date">
            </br></br>
            <input type="submit" value="Submit">
        </fieldset>
    </form>

web.xml :

<servlet>
    <servlet-name>RegisterServlet</servlet-name>
    <servlet-class>Controller.RegisterServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>RegisterServlet</servlet-name>
    <url-pattern>/RegisterServlet</url-pattern>
</servlet-mapping>

Here is my directory structure :

enter image description here

Community
  • 1
  • 1
AL-zami
  • 8,902
  • 15
  • 71
  • 130

1 Answers1

2

You need to change

<form action="RegisterServlet" class="register" method="POST" >

to

<form action="../RegisterServlet" class="register" method="POST" >

Because, your Login.jsp resides under Views folder and you are trying to access RegisterServlet in same path. And, that's why request is going to

/ClassProject/Views/RegisterServlet

instead of

 /ClassProject/RegisterServlet

But, as per web.xml this servlet exists at project root.

Ravi
  • 30,829
  • 42
  • 119
  • 173
  • This will work but i recommend not to use relative path. Use `
    ` by this way it will work even the JSP is been forwarded from a servlet.
    – seenukarthi Oct 22 '17 at 10:25
  • @KarthikeyanVaithilingam I'm not sure about `${pageContext.request.contextPath}` could you explain me how does it move the request ?? I mean, how does it know whether it has to go to `../../` or `../`. Imagine you have `/ClassProject/Views/RegisterServlet` and `/ClassProject/Views/something/RegisterServlet`. Then, your `login.jsp` is at `/ClassProject/Views/else/login.jsp` ? – Ravi Oct 22 '17 at 10:31
  • Check this https://stackoverflow.com/questions/3655316/browser-cant-access-find-relative-resources-like-css-images-and-links-when-cal/3658735#3658735 – seenukarthi Oct 22 '17 at 10:34