0

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?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
HAO
  • 89
  • 9
  • https://stackoverflow.com/questions/5243754/difference-between-getattribute-and-getparameter – Momus Aug 21 '17 at 04:26

4 Answers4

1

For login page use below code :

<form action="servlet1" method="post">
Name:<input type="text" name="username"/><br/><br/>
Password:<input type="password" name="userpass"/><br/><br/>
<input type="submit" value="login"/>
</form>

and to get attribute of that form create a class and write the below code :

public class FirstServlet extends HttpServlet {  
public void doPost(HttpServletRequest request, HttpServletResponse response)  
    throws ServletException, IOException {  

response.setContentType("text/html");  
PrintWriter out = response.getWriter();  

String n=request.getParameter("username");  
String p=request.getParameter("userpass");  


}  
}  
Braj Ankit
  • 333
  • 1
  • 2
  • 19
0

The values "username" and "password" being passed from the form are parameters and not attributes.

Difference between getAttribute() and getParameter()

Momus
  • 394
  • 2
  • 13
0

Instead of getAttribute use getParameter as in below code ;

String username= request.getParameter("username"); String password= request.getParameter("password");

arjun99
  • 358
  • 1
  • 8
0

@HAO, request.getAttribute() is only used in the server side. Use request.getParameter() instead. It is the method we should use to get the value from client side to server side. For more details refer this. Thanks Momus & Braj Ankit

Difference between getAttribute() and getParameter()