Hi Guys,
Lets try to create two jsp pages now, one is "LoginSuccess.jsp" which we redirect to, when the given input credentials are valid and other one is "LoginFailure.jsp" which we redirect to, when the given input credentials are not valid.
Step 1: Creating LoginSuccess.jsp
Right click on "WebContent" folder in "Project Explorer" of eclipse and select "New --> JSP File". "New JSP File" window will be opened. Give "LoginSuccess.jsp" as the "File name" and click "Next --> Finish". "LoginSuccess.jsp" file will be created and opened in the eclipse editor and ready for changes now.
What we do here is, we print some welcome message on this page as we know this page is getting shown to the user only when user enters correct username and password (correct in the sense, this data already persisted in the database), So we will print a welcome message of the below form,
Login Success.
Welcome <firstName> <lastName>
But, how do we get these first name and last name of the user from the "LoginSuccess.jsp" though they exist in the database. That is what the purpose of HttpSession in the LoginServlet class. If you look at the source code of "LoginServlet.java", you can observe the below lines
HttpSession session = request.getSession(true);
session.setAttribute("FirstName", rs.getString("firstname"));
session.setAttribute("LastName", rs.getString("lastname"));
session.setAttribute("FirstName", rs.getString("firstname"));
session.setAttribute("LastName", rs.getString("lastname"));
Here with the first statement, we are trying to get the session from the HttpServletRequest and if session is not existed then we create a new session. In this case, session is no where existed and thus new session will be created. Then with the second and third statements, we try to add the firstname and lastname fields from the database results into the crated session with the parameters "FirstName" and "LastName". So these values will be available to any servlet through out the application.
Copy the below content into "LoginSuccess.jsp"
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Login Success</title> </head> <body> <center> <center>Login Success</center> Welcome <%= session.getAttribute("FirstName") + " " + session.getAttribute("LastName") %> </center> </body> </html>
Now, "LoginSuccess.jsp" will be looking like below
Step 2: Creating LoginFailure.jsp
Right click on "WebContent" folder in "Project Explorer" of eclipse and select "New --> JSP File". "New JSP File" window will be opened. Give "LoginFailure.jsp" as the "File name" and click "Next --> Finish". "LoginFailure.jsp" file will be created and opened in the eclipse editor and ready for changes now. In this page, we are going to print some error page of the below form:
Login Failed
Incorrect User ID / Password. Please try again!
Back button
Back button
Copy the below code into "LoginFailure.jsp"
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Login Failure</title> </head> <body> <center> <center>Login Failed</center> Incorrect User ID / Password. Please try again! <br> <center><input type=button name=back value=Back onClick="history.go(-1);" /></center> </center> </body> </html>
Now, "LoginFailure.jsp" will be looking like below
Step 3: Run the Login application
Right click on "LoginForm.jsp" and select "Run As --> Run on Server". "Run On Server" window will be opened. Click on "Next --> Finish". Opt for "Restart server", if it asks. Login form page will be opened now.
First let us test with some invalid input data. Enter "ganesh" in "UserId" field and "techblog" in "Password" field and click "Submit". Since this data is not existed in the database, error page (LoginFailure.jsp) will be displayed with the error message. Now, you can see the error page like below:
If you click on "Back" button then you will be coming back to previous page that is login page. So click on "Back" button.
Lets try now with the valid data. Enter "akshay" in "UserId" field and "virat123" in "Password" field and click "Submit". Since this data is already existed in the database, welcome page (LoginSuccess.jsp) will be displayed with the welcome message. Now, you can see the welcome page like below:
Thank U so much for your article :-)
ReplyDeleteExplained in very easy and eye friendly manner :-)
This saved my day! I wanted also to know if there is a way where if i log in as admin, to display e.g. "Welcome to the admin panel." or if i log in as a user to display a "Welcome to the homepage, user" jsp. Basically to have multiple type of users to display different content when logged in.
ReplyDeleteThanks a lot for this nicely explained tutorial.
Nice and detailed explanation, nice work and keep writing..!
ReplyDeleteThanks
Thank you Very Much
ReplyDeleteThank you very much
ReplyDeletethank you sir..
ReplyDeleteyou really help me this time..
i'm really grateful for this...
Good Site explained simple and super...
ReplyDeleteThankyou very much
ReplyDeleteBrilliant article!! very helpful thank you!!
ReplyDeleteAwesome its very easy to understand i like to say thank u....
ReplyDeleteAwesome its very easy to understand i like to say thank u so much...
ReplyDeletewill this code work on Netbeans with amache server
ReplyDeleteNo it wont...
DeleteThe best article I show..Which is Very useful..and Given all step by step...Help me ...very much
ReplyDeleteMy family always say that I am killing my time here at web, however I know I am getting know-how every day by reading such nice posts.
ReplyDeleteBefore watching this article i was always afraid of java but now your article have given me strength to overcome from this fear, feeling so confident right now, all scare has gone.
ReplyDeleteThanx a ton
Thanks a lot for this !!
ReplyDeletethanks for your help, it is explained very simply :)
ReplyDeleteI am getting a black screen after clicking on submit. what should be the error ? Any idea ? I just wrote the same code.
ReplyDeleteExcellent article.. Thank you so much. This saved my time.
ReplyDeleteAfter searching for ages and trying lots of other approaches, finally one where everything and all the code actually works! Not only that, but clear explanations of why the code was there or needed to be changed. Thanks so much!
ReplyDeletevery helpful.
ReplyDeleteI want tabel name
ReplyDeletesuper..very helpful..
ReplyDeletethank you so much
Awesome tutorials, you just made me love Java again :)
ReplyDeletehi!thanks for your help,it really means a lot for someone like me with interest on java..i want to develop a restaurant management system which is integrated with point of sale...what can you advice me?..where do i start?
ReplyDelete