91-9990449935 0120-4256464 |
Struts 2 Login and Logout ExampleBefore creating the login and logout application using struts 2, you must clear the concepts of aware interfaces in struts 2. In this example, we have used the SessionAware interface to put the information in the session scope and ServletActionContext class to get the information from the session scope. This example contains three links login, logout and profile. The end user cannot click on the profile page until he/she is logged in. After getting logged in, he/she may go the profile page. If the end user clicks on the logout page, he will not be able to access the profile page. Here, we assume that you have a table in oracle database named user3333 that contains records. The table query is: Example of creating login and logout application using struts 2In this example, we are need following pages
1) Create index.jsp for inputThis jsp page creates three links for login, logout and profile. index.jsp2) Define action and result in struts.xmlThis xml file defines one package and 4 actions. Each action defines at least one result page. For the loginprocess and logout actions, we are using the same action class but there invocation methods are different. struts.xml3) Create the action class for login and logoutThis action class implements the SessionAware interface and overrides the setSession method to store the information in the session scope. For logout, we are simply calling the invalidate() method of SessionMap. Login.java4) Create the Dao class to authenticate userThis class simply validates the user from the table stored in the oracle database. LoginDao.java5) Create the Profile classThis class gets the information from the session scope, if any information is found in the session scope with login name, it returns success otherwise false. Profile.java6) Create view componentsThere are many view components:
view components for loginlogin.jspThis page creates the login form. loginsuccess.jspThis page prints the welcome message with the username. loginerror.jspThis page displays the error message. view components for logoutlogoutsuccess.jspThis page simply displays the successfully logged out message. view components for profileprofilesuccess.jspThis page prints the welcome to profile message. profileerror.jspThis page prints the message to login first and includes the login.jsp page.
Next TopicStruts2 Fetching All Records Of A Table
|