How To Create Login Form in Struts 2.x having Session Functionality?

Hello Friends, 
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:

1. org-netbeans-modules-framework-xwork.nbm
2. org-netbeans-modules-web-frameworks-struts2.nbm
3. org-netbeans-modules-web-frameworks-struts2lib-v2_0_14.nbm
4. org-netbeans-modules-web-frameworks-struts2lib-v2_2_3.nbm

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.

Download glassfish from any of the given links:
http://glassfish.java.net/public/downloadsindex.html
http://glassfish.java.net/downloads/3.1-final.html
http://glassfish.java.net/After selecting the server, you can leave the rest as it is and click on next.
6. Here is the last step:


If you have installed your Struts 2.x Netbeans Support plugins (mentioned on top) successfully then you will be able to see struts2 option under frameworks.

Select Struts2 option and dis-select create example page as shown in the diagram.

Click Finish.
You have successfully created the new project now its time to create JSP(Java Server Pages)
Here is a diagrammatic representation of what we have to do!

First we need to create three jsp pages:
1. index.jsp
2. success.jsp
3. expired.jsp

By Default there will be index.jsp file present in the root directory that is web pages so we need to open it and write the following code there:

This is index.jsp page code :

<%-- 
  Document   : index
    Created on : Sep 6, 2012, 11:50:48 AM Author     : Sudhanshu --%> <%@taglib uri="/struts-tags" prefix="s" %> <%@page contentType="text/html" pageEncoding="UTF-8"%> JSP Page

Hello World!

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:

This is success.jsp page code : 

<%--
  Document   : success
  Created on : Sep 6, 2012, 11:52:48 AM
Author     : Sudhanshu --%> <%@taglib uri="/struts-tags" prefix="s" %> <%@page contentType="text/html" pageEncoding="UTF-8"%>
JSP Page

Hello !

logout

Follow the same steps to create another jsp page expired.jsp as you have done in case of success.jsp

This is expired.jsp page code :

<%--
  Document   : expired
  Created on : Sep 6, 2012, 11:55:48 AM
  Author     : Sudhanshu
  --%>
  <%@taglib uri="/struts-tags" prefix="s" %>
  <%@page contentType="text/html" pageEncoding="UTF-8"%>
  
  
  
  
  JSP Page 
  
  
  Session Expired!!!
  
  

Now we have created our jsp pages. Lets move to our struts.xml file to perform action mapping. The struts.xml file can be found under configurations file category.

This is struts.xml file :


  
  
  
   
  success.jsp
  index.jsp
 
 
 index.jsp
 expired.jsp
  
 
 

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

 package action;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpSession;
 import org.apache.struts2.interceptor.ServletRequestAware;
 /**
 * @author Sudhanshu
 */
 
 public class Login implements ServletRequestAware{
 private HttpSession hs;
 private HttpServletRequest hsr;
 private String userid;
 private String pswd;
 public String getUserid() {
 return userid;
 }
 public void setUserid(String userid) {
 this.userid = userid;
 }
 public String getPswd() {
 return pswd;
 }
 public void setPswd(String pswd) {
 this.pswd = pswd;
 }
 public String execute(){
 hs=hsr.getSession();
 if(userid.equalsIgnoreCase("himanshu")&&pswd.equals("himanshu")){
 hs.setAttribute("userid",userid );
 return "success";
 }
 hs.setAttribute("msg", "Invalid user id and password!");
 return "input";
 }
 @Override
 public void setServletRequest(HttpServletRequest hsr) {
 this.hsr=hsr;
 }
 }

Save the file. Now click on Logout.java and write the following code there. 

Code of Logout.java

package action;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpSession;
 import org.apache.struts2.interceptor.ServletRequestAware;
 /**
 * @author Sudhanshu
 */
 
 public class Logout implements ServletRequestAware{
 private HttpSession hs;   
 private HttpServletRequest hsr;
 private String userid;
 public String execute(){
 hs=hsr.getSession();
 try{
 if(hs.getAttribute("userid")==null){
 hs.invalidate();
 return "expired";
 }else{
 hs.removeAttribute(userid);
 hs.setAttribute("msg", "Logged out Successfully!!");
 return "success";
 }
 }catch(Exception e){}
 return "success";
 }
 @Override
 public void setServletRequest(HttpServletRequest hsr) {
 this.hsr=hsr;
 }
 }

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):

Best Of Luck!!!

  • This is one of the best blog where you can learn everything about integrated latest technologies. One of the best topic in this blog is to learn struts 2.x. It is really very helpful & amazing. It increases your curiosity to know much about technology mostly in java.

%d bloggers like this: