You don't have to put all those ifs in the index jsp because what you are trying to do should be divided into 2 jsps.
Assuming that your first jsp is called: login.jsp. There you don't have to check if the user is logged in. All you have to do is have the login form. You can also have a message displayed if it is not null:
String message = (String)request.getAttribute("message");
if message is not null display it
- login form - with input fields and a submit button
Then submit to your login Servlet. If the user name is not correct, fill the appropriate message:
request.setAttribute("message", "Invalid username, password"); and redirect back to the login.jsp
If the username password are correct then redirect to the main page of your application (welcome page).
In there you will get the username from the session and check if it is null.
So you will not have many ifs in one page. One page for the login. One servlet for validation. Then depending on the results, go back to the login page or go to your main page