I am trying to send data from a JSP file to a Java Servlet. I've seen so many examples on here on how to do it, and I believe I am doing it the right way, but for some reason the doPost() methods is not being called inside my servlet.
Here is what I added to my web.xml file:
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<servlet>
<servlet-name>loginServlet</servlet-name>
<servlet-class>src.action.LoginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>loginServlet</servlet-name>
<url-pattern>/login</url-pattern>
</servlet-mapping>
</web-app>
Here is my JSP file
<html>
<head>
</head>
<body>
<form action = "localhost:8080/UserModule/src/action/login" method="post">
Username<input type = "text" name = "username"><br><br>
Password<input type = "password" name = "password"><br><br>
<input type = "submit" value = "Submit">
</form>
</body>
</html>
Here is my Java servlet:
package action;
import java.io.IOException;
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 LoginServlet
*/
@WebServlet("/login")
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public LoginServlet() {
super();
// TODO Auto-generated constructor stub
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
String username = (request.getAttribute("username")).toString();
System.out.println("asd");
String password = (String) request.getAttribute("password");
System.out.println(username);
}
}
It's giving me an error on import javax.servlet.annotation.WebServlet;
and on @WebServlet("/login")
It's because they were only implemented in the spec v3.0, while I am only allowed to use spec v2.5 (required to at work). Anyone has any idea how to solve this issue?
My directory: