1

I am trying to submit a simple form for processing in a servlet, but I keep getting a 404 whenever I submit. The code compiles and deploys fine. I've been through just about every tutorial on the subject, so I understand that there is a lot of information out there about it. Any advice on which rookie mistake I am making? Thanks in advance for any help!

jsp is located in /webapps/registerWidget servlet located in /webapps/registerWidget/WEB-INF/classes/com/sample/applet web.xml located in /webapps/registerWidget/WEB-INF

Here is my jsp:

    <!DOCTYPE HTML>
    <html>
    <head>
        <title>Sample Applet</title>
    </head>
    <body>
        <header>Please Register</header>
        <section>
            <form action="/register" method="POST">
                First name: <input type="text" name="firstname"><br>
                Last name: <input type="text" name="lastname"><br>
                Email: <input type="text" name="email"><br>
                <input type="submit" value="Submit">
            </form>
        </section>
    </body>
</html>

Here is my servlet:

package com.sample.applet;

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

public class RegisterServlet extends HttpServlet {

    public void doGet(HttpServletRequest req, HttpServletResponse res)
            throws IOException, ServletException {
        doPost(req, res);
    }

    public void doPost(HttpServletRequest req, HttpServletResponse res)
            throws ServletException {

        String first = req.getParameter("firstname");
        String last = req.getParameter("lastname");
        String email = req.getParameter("email");

        ....some processing code....

    }
}

Here is my web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
    http://java.sun.com/xml/ns/j2ee/Web-app_2_4.xsd">

    <servlet>
        <servlet-name>registerServlet</servlet-name>
        <servlet-class>com.sample.applet.RegisterServlet</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>registerServlet</servlet-name>
        <url-pattern>/register</url-pattern>
    </servlet-mapping>
</web-app>
petfreshman
  • 513
  • 2
  • 8
  • 21
  • Have you looked at the URL which appears in browsers address bar while the 404 is presented? Is that URL really, really right? (the 404 error at its whole own tells it's not). Shouldn't there be a context path in that URL? By the way... Servlet 2.4 is rather ancient. We're currently already at Servlet 3.0 for more than 3 years and 3.1 is coming close. Are you sure that you're reading right and up to date resources? Put your mouse on top of the `[servlets]` tag which you placed on the question until a black box shows up and then click therein the *info* link. – BalusC Mar 18 '13 at 16:18
  • Remove the slash in front of register in the form action: action="register" – Vincent Ramdhanie Mar 18 '13 at 16:19
  • possible duplicate of [Servlet Url Mapping Error](http://stackoverflow.com/a/10839483) – BalusC Mar 18 '13 at 16:21

2 Answers2

1

The url in the action in your form starts with a slash. That implies that you are starting from the server root and looking for a servlet named register at that point. However, your servlet is actually in a context of some sort. So you can either say: /mycontext/register or remove the slash altogether.

Vincent Ramdhanie
  • 102,349
  • 23
  • 137
  • 192
0

Use 'register' in action instead of '/register'

form action="register"

'/' in '/register' tells it to search your servlet at root of your context.

VishalDevgire
  • 4,232
  • 10
  • 33
  • 59