o12d10
o12d10

Reputation: 798

modify css conditionally based on controller result

I have created a login page using JSP (account.jsp) if login is successful the servlet redirects to home.jsp if notstays/ redirects to account.jsp.

In case of unsuccessful login I won't s to display an error message. for that I have created a error div that is set to hidden.i.e. style="display:none;" When ever the login goes wrong I want to change it's display setting to display, ie, `style="display:block;"

I want to do it from servlet. It is possible to that? If yes, how?

Login Form

<div class="col-sm-4">
    <h3><STRONG>Login</STRONG></h3>
    <div id="login_alert" class="alert alert-danger" style="display:none;">
        <a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a>
        <strong>Unsuccessful!</strong> Invalid Input
    </div>
    <form role="form" action="login" method="post">
        <div class="form-group">
            <input type="email" class="form-control" id="email" name="email" placeholder="Enter Email">
        </div>
        <div class="form-group">
            <input type="password" class="form-control" id="pwd" name="pwd" placeholder="Enter Password">
        </div>
        <div class="checkbox">
            <label><input type="checkbox"> Remember me</label>
        </div>
        <button id ="submit" type="submit" class="btn btn-success">Submit</button>
    </form>
</div>

Servlet Code

if(user exists){
    HttpSession session = request.getSession();
    session.setAttribute("user",user);
    session.setAttribute("pwd",pwd);
    request.getRequestDispatcher("./home.jsp").forward(request, response);
}else{
    //document.getElementById('login_alert').style.display='block';
    request.getRequestDispatcher("./account.jsp").forward(request, response);
}

Upvotes: 0

Views: 189

Answers (1)

Amit.rk3
Amit.rk3

Reputation: 2417

1 - add a request attribute

else{
    request.setAttribute("loginFailed",true);
    request.getRequestDispatcher("./account.jsp").forward(request, response);
}

2- Use it conditionally in JSP

 <div id="login_alert" class="alert alert-danger" 

     style="display:${loginFailed ? 'block' : 'none'}" >

      <a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a>
      <strong>Unsuccessful!</strong> Invalid Input
 </div>

Upvotes: 1

Related Questions