I wrote a login HTML and submit the username, password to the servlet
@WebServlet(name = "loginServlet")
public class loginServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("post");
Enumeration enumeration = request.getAttributeNames();
while (enumeration.hasMoreElements()) {
System.out.println(enumeration.nextElement());
}
String username = (String) request.getAttribute("username");
String password = (String) request.getAttribute("password");
System.out.println(username);
System.out.println(password);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
}
My HTML:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Login</title>
</head>
<body>
<h1>Login</h1>
<form action="/loginServlet" method="post">
User Name: <input required name="username" value="name">
Password: <input required type="password" name="password" value="pw">
<input type="submit" value="Submit">
</form>
</body>
</html>
The output of the servlet is: post null null
I checked the request.getAttribute returning null in servlet But that situation is JSP, not HTML.
Are there anything wrong? Or can I use the HTML instead of the JSP to get the username and password?