Reputation: 523
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>
Upvotes: 2
Views: 10499
Reputation: 4268
Use 'register' in action instead of '/register'
form action="register"
'/' in '/register' tells it to search your servlet at root of your context.
Upvotes: 0
Reputation: 103135
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.
Upvotes: 2