3

I have made a simple login app. But the servlet receives null data whenever the form is sent.

Below is login page

<body>
<form action="login">
    Enter login Name : <input type="text" name="userName"/><br/>
    Enter password   : <input type="password" name="userPassword"/><br/>
    <input type="submit" value="Log In"/>
</form>

The below is the servlet code to check the values of variable entered

@WebServlet("/login")
public class loginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

/**
 * @see HttpServlet#HttpServlet()
 */
public loginServlet() {
    super();
    // TODO Auto-generated constructor stub
}

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String name = (String)request.getAttribute("userName");
    String password = (String)request.getAttribute("userPassword");

    System.out.print("in doGet method");
    System.out.print(name+" "+password+" are these");


    if(name!=null && password!=null){
        System.out.print("in null method");
        if(name=="admin" && password=="admin"){
//              Cookie cookie = new Cookie("userName", name);
//              response.addCookie(cookie);
//              RequestDispatcher dispatch = request.getRequestDispatcher("success.jsp");
//              dispatch.forward(request, response);

            response.sendRedirect("success.jsp");
        }
        else{
            System.out.print("in error method");
            response.sendRedirect("error.jsp");
        }
    }
}

/**
 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
 */
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

}

}

I am using System.out.print method to print the values in Tomcat console.

Tiny Jaguar
  • 433
  • 3
  • 12
  • 30

3 Answers3

5

These calls to Request.getAttribute(String)

String name = (String)request.getAttribute("userName");
String password = (String)request.getAttribute("userPassword");

should be calls to Request.getParameter(String) (per the Javadoc, that Returns the value of a request parameter as a String). Like

String name = request.getParameter("userName");
String password = request.getParameter("userPassword");

Also,

if(name=="admin" && password=="admin"){

should be something like

if (name.equals("admin") && password.equals("admin")) {

because String is an Object, and you want equivalent values (not identical references).

Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
0

Instead of request.getAttribute("userName"); use request.getParameters("username");

do it for password also.

RajSharma
  • 1,941
  • 3
  • 21
  • 34
0

Problem is your are sending your data via html form, no way you could set the attributes on request, but instead those are parameters. So instead of request.getAttribute() try request.getParameter().

Also redirecting to next view using response.sendRedirect() is good practice, however if you wish to get previous parameters(user name/ password) on the next view, you are loosing it here. In order to get those details either you can use RequestDispatcher or you can continue with response.sendRedirect() and use session to take care of the details using session.setAttribute(<key>, <value>).