How To Create Login Form in Struts 2.x having Session Functionality?
This is a simple approach made to describe the users in making use struts 2.x framework by developing a login form and show the concept of session. I have used Netbeans IDE 7.2 which can be downloaded from www.netbeans.org to develop this small web application. To get the support of struts 2.x in the netbeans we need to download some plugins for the installed netbeans version. Here The Netbeans IDE Version is 7.2 and we have the names of following plugins which are required to get struts 2.x support:
The plugins vary for different IDE versions. Visit www.netbeans.org for any information an d download regarding struts 2.x plugin support for Netbeans IDE.
After downloading the plugins install it using the following steps:
1. Click on Tools from the menu options
2. Click on plugins
3. Click on downloaded option from the new popped window
4. Select add plugins and browse through the folders to locate the plugins
5. The final step is to click on install after adding all the plugins
6. Restart the netbeans IDE
7. Your plugins are installed and you are ready to develop projects using struts 2.x framework
Now, to develop a simple login page in struts 2.x follow the following steps:
1. Click on file option from menu
2. Click on New Project
3. Select Java Web under categories list (on Left) and Web Application (on Right) and Click Next.
4. After clicking on next a new window will appear asking you to enter the project name and location to store the project, by default it will have some values:
You can change the name of project or can leave it as highlighted in blue and simply click on next. Here we have changed the name of project as session and stored in our own created folder on drive D(Though you can leave the fields to have default values as it is and click next).
5. After clicking on Next in step 4. You will get a new window asking for sever by default it is glassfish server (This depends upon the version of netbeans IDE)
(Here in my case it is Apache Tomcat. If you have problem in locating the server. You can add it but before that you need to have the server installed on your computer. If you don’t have any server download the latest glassfish server from internet install or extract it, locate it here and use it.
Now to create success.jsp:
1. Locate the Projects category on the left side of netbeans IDE as shown in the above image.
2. Right Click on the root directory folder name web pages
3. Click on New Option
4. If you can see the jsp option then click on it, a new window will appear. Now type the jsp file name as success without .jsp extension and click on finish.
(If you are not able to see jsp option on right clicking Web Pages and clicking new then select other option which can be found at the bottom of pop up menu and select web from left and jsp from right in the appeared window and click next -> then name the jsp file as mentioned and click finish)
5. Now you can see the file success.jsp in the projects directory.
Click on success.jsp to open it in working area on netbeans and type the following code:
Now time to do some java code:
1. Under Source packages option right click on default package
2. Click on new and then Click on Java Package
3. Type the java package name as action and click finish
4. Now right click on the newly created action package
5. Click new and then Click on Java Class
6. Type the java class name as Login and click finish
7. Repeat step 4-6 to create Logout class.
Code of Login.java
First click on Login.java and then write the following code there
TO RUN : Right Click on the created project “session” or “WebApplication1” from the projects category and click run!!! Output:
When the project is successfully deployed you can have the following look(Remember : user name and password is himanshu):