0

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);
}

1 Answer 1

1

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>
Sign up to request clarification or add additional context in comments.

4 Comments

when I am updating based on this - it is displying the code in text like 'style="display:block;" style="display:none;" > × Unsuccessful! Invalid Input'
no i didn't but now when paste it is saying "can not find the tag library descriptor" still trying to figure out
hey Amit could you update the answer with this line : style="display:${loginFailed ? 'block' : 'none'}" >
@yashgarg1232 , sorry , it was a typo :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.