Thursday, May 16, 2013

Session Tracking using SessionAware Interface with Struts2, Oracle Database and Eclipse IDE

In this Tutorial we are performing Session Tracking using SessionAware Interface and this interface is used by actions that want to access user's http session which will give them access to a Map where they can put objects that can be made available to subsequent requests.



Here we are not Performing any type of Validation so for validation part go for other Tutorials.

Project Structure in Project Explorer:

Download Struts2 Jars

Struts2 jars setting, for More Details Read How to set Struts2 Environment
Java Build Path Libraries
you can't see struts2 jar here because we directly put it inside lib folder.
Deployment Assembly
Download Oracle JDBC jars

Create a Database Table and make two rows entry in it as shown below:
Login to oracle database and create table using GUI
Register Table
Register Table Data
login.jsp
create a user Login page.
 <%@ 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>welcome page</title>  
 </head >  
 <body>  
 <h4 align=right><a href=login>Login</a></h4>  
     <h5 align=right><a href=login.jsp>Home</a>&nbsp&nbsp  
     <a href=#>FeedBack</a>&nbsp&nbsp  
     <a href=#>Contact Us</a></h5><hr>  
 <center><h1>Login</h1>  
 <form action="login">  
           <table>  
                <tr>  
                     <td>UserName</td>  
                     <td><input type="text" name="uname" /></td>  
                </tr>  
                <tr>  
                     <td>Password</td>  
                     <td><input type="password" name="upass" /></td>  
                </tr>  
                </table>  
           <input type="submit" value="Login" /><br/>  
           </form></center>  
 </body>  
 </html>  

struts.xml
 <?xml version="1.0" encoding="UTF-8"?>  
 <!DOCTYPE struts PUBLIC  
   "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"  
   "http://struts.apache.org/dtds/struts-2.0.dtd">  
   <struts>  
   <package name="log" namespace="/" extends="struts-default">  
   <action name="login" class="in.blog.webideaworld.LoginAction" method="execute">  
   <result name="success">/user.jsp</result>  
   <result name="error">/login.jsp</result>  
   </action>  
   <action name="logout" class="in.blog.webideaworld.Logout" method="logout">  
   <result name="success">/login.jsp</result>  
   </action>  
   <action name="profile" class="in.blog.webideaworld.Profiledirect" method="execute">  
   <result name="success">/profile.jsp</result>  
   </action>  
   <action name="profile1" class="in.blog.webideaworld.Profiledirect" method="execute">  
   <result name="success">/update.jsp</result>  
   </action>  
   <action name="home" class="in.blog.webideaworld.Profiledirect" method="execute">  
   <result name="success">/user.jsp</result>  
   </action>  
   <action name="update" class="in.blog.webideaworld.UpdateProfile" method="execute">  
   <result name="success">/success.jsp</result>  
   </action>  
   </package>  
   </struts>  


web.xml
 <?xml version="1.0" encoding="UTF-8"?>  
 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">  
  <display-name>Struts2_SessionTracking</display-name>  
  <welcome-file-list>  
   <welcome-file>/login.jsp</welcome-file>  
    </welcome-file-list>  
    <filter>  
    <filter-name>struts2</filter-name>  
    <filter-class>  
      org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter  
    </filter-class>  
   </filter>  
   <filter-mapping>  
    <filter-name>struts2</filter-name>  
    <url-pattern>/*</url-pattern>  
   </filter-mapping>  
 </web-app>  

DBconn.java
 package in.blog.webideaworld.dao;  
 import java.sql.Connection;  
 import java.sql.DriverManager;  
 import java.sql.Statement;  
 public class DBconn {  
      Statement st;  
      public Statement getSt()throws Exception {  
           Class.forName("oracle.jdbc.OracleDriver");  
            Connection conn=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE","hr","hr");  
          st=conn.createStatement();  
          return st;  
      }  
 }  

LoginAction.java
create a Action java class for Login. SessionAware interface has a setSession(Map m) method which sets the Map of session attributes in the implementing class.
 package in.blog.webideaworld;  
 import java.sql.ResultSet;  
 import java.sql.Statement;  
 import java.util.Map;  
 import in.blog.webideaworld.dao.DBconn;  
 import org.apache.struts2.interceptor.SessionAware;  
 import com.opensymphony.xwork2.ActionSupport;  
 @SuppressWarnings("serial")  
 public class LoginAction extends ActionSupport implements SessionAware {  
      private String upass;  
      private String uname;  
      Map m;  
      public String getUpass() {  
           return upass;  
      }  
      public void setUpass(String upass) {  
           this.upass = upass;  
      }  
      public String getUname() {  
           return uname;  
      }  
      public void setUname(String uname) {  
           this.uname = uname;  
      }  
      public void setSession(Map m)  
      {  
           this.m=m;  
      }  
       public String execute() throws Exception{  
        try{  
            Statement st=new DBconn().getSt();  
            ResultSet rs=st.executeQuery("select * from register where uname='"+uname+"' and upass='"+upass+"'");  
            if(rs.next())  
            {    
                 m.put("id", rs.getInt(1));  
                 m.put("uname",rs.getString(2));  
                   m.put("upass", rs.getString(3));  
                 return SUCCESS;  
            }  
           }  
           catch (Exception e) {}  
          return ERROR;  
      }  
      }  

user.jsp
create a user welcome page.
 <%@page import="com.opensymphony.xwork2.ActionContext"%>  
 <%@page import="in.blog.webideaworld.LoginAction"%>  
 <%@page import="java.util.Map" %>  
 <%@ page language="java" contentType="text/html; charset=ISO-8859-1"  
   pageEncoding="ISO-8859-1" session="false"%>  
  <!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>Valid page</title>  
 </head>  
 <body>  
 <h4 align=right><a href=logout.action>Logout</a></h4>  
    <h5><a href=profile.action>Profile</a>&nbsp&nbsp  
     <a href=#>FeedBack</a>&nbsp&nbsp  
     <a href=#>Contact Us</a></h5><hr>  
     <center>  
     <%   
 Map m=ActionContext.getContext().getSession();  
 String s1=(String)m.get("uname");  
 String s2=(String)m.get("upass");  
 if(s1==null && s2==null){  
   response.sendRedirect("notvalid.jsp");  
   }  
 %>   
 --------Welcome to WebIdeaWorld---------<br/>  
 Hello: <%=s1 %>  
 </center>  
 </body>  
 </html>  

notvalid.jsp
create a page for Person not Authorized.
 <%@ 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>Not Valid User</title>  
 </head>  
 <body>  
 <h4 align=right><a href=login>Login</a></h4>  
     <h5 align=right><a href=login.jsp>Home</a>&nbsp&nbsp  
     <a href=#>FeedBack</a>&nbsp&nbsp  
     <a href=#>Contact Us</a></h5><hr>  
     <center>  
 You are not Authorized Person to this Website.</center>  
 </body>  
 </html>  

Profiledirect.java
create a setSession() method to make session object available to subsequent requests.Evey time when there is a request for session objects you need to pass it through setSession() method.
 package in.blog.webideaworld;  
 import java.util.Map;  
 import org.apache.struts2.interceptor.SessionAware;  
 import com.opensymphony.xwork2.ActionSupport;  
 public class Profiledirect extends ActionSupport implements SessionAware {  
      Map m;  
      public void setSession(Map m)  
      {  
           this.m=m;  
      }  
      public String execute() throws Exception{  
           return SUCCESS;  
      }  
 }  

profile.jsp
create a user's profile page.
 <%@page import="java.sql.*"%>  
 <%@page import="com.opensymphony.xwork2.ActionContext"%>  
 <%@page import="java.util.Map" %>  
 <%@page import="in.blog.webideaworld.dao.DBconn"%>  
 <%@ page language="java" contentType="text/html; charset=ISO-8859-1"  
   pageEncoding="ISO-8859-1" session="false"%>  
 <!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>Profile</title>  
 </head>  
 <body>  
 <h4 align=right><a href=logout.action>Logout</a></h4>  
     <a href=home.action>Home</a>&nbsp&nbsp  
     <a href=profile.action>Profile</a>&nbsp&nbsp  
     <a href=#>FeedBack</a>&nbsp&nbsp  
     <a href=#>Contact Us</a></h5><hr>  
      <%        
          Map m=ActionContext.getContext().getSession();  
     String s1=(String)m.get("uname");  
     String s2=(String)m.get("upass");  
     if(s1==null && s2==null){  
     response.sendRedirect("notvalid.jsp");  
     }  
     try{  
     Statement st=new DBconn().getSt();  
     ResultSet rs=st.executeQuery("select * from register where uname='"+s1+"' and upass='"+s2+"'");  
           while(rs.next()) {  
                %>  
          <center>  
          <form action='profile1' method='post'>  
       <h1><%=rs.getString(4)%>'s Profile</h1>  
       <table><tr><td>UserName:</td><td><%=rs.getString(2)%></td></tr>  
       <tr><td>Password:</td><td><%=rs.getString(3) %></td></tr>  
       <tr><td>First Name:</td><td><%=rs.getString(4) %></td></tr>  
       <tr><td> Last Name:</td><td><%=rs.getString(5) %></td></tr>  
       </table><br/><input type='Submit' value='update'><br></form>  
       </center>  
       <%}  
     }  
     catch(Exception e){}  
       %>  
 </body>  
 </html>  

update.jsp
create a user's profile update page.
 <%@page import="java.sql.*"%>  
 <%@page import="com.opensymphony.xwork2.ActionContext"%>  
 <%@page import="java.util.Map" %>  
 <%@page import="in.blog.webideaworld.dao.DBconn"%>  
 <%@ page language="java" contentType="text/html; charset=ISO-8859-1"  
   pageEncoding="ISO-8859-1" session="false"%>  
 <!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>Update Profile</title>  
 </head>  
 <body>  
 <h4 align=right><a href=logout.action>Logout</a></h4>  
     <a href=profile.action>Profile</a>&nbsp&nbsp  
     <a href=#>FeedBack</a>&nbsp&nbsp  
     <a href=#>Contact Us</a></h5><hr>  
     <%      
     Map m=ActionContext.getContext().getSession();  
      String s1=(String)m.get("uname");  
      String s2=(String)m.get("upass");  
      if(s1==null && s2==null){  
        response.sendRedirect("notvalid.jsp");  
        }  
     try  
     {  
       Statement st=new DBconn().getSt();  
       ResultSet rs=st.executeQuery("select * from register where uname='"+s1+"' and upass='"+s2+"'");  
       while(rs.next())  
       {out.println("<center><h1>"+rs.getString(4)+"'s Profile</h1></center>");%>  
     <center>  
     <form action="update" method="post">  
       <h1>Update your Profile</h1>  
       <table><tr><td>UserName:</td><td><input type="text" name="t1" value="<%=rs.getString(2)%>" ></td></tr>  
       <tr><td>Password:</td><td><input type="text" name="t2" value="<%=rs.getString(3)%>" ></td></tr>  
       <tr><td>Re-Type Password:</td><td><input type="text" value="<%=rs.getString(3)%>"></td></tr>  
       <tr><td>First Name:</td><td><input type="text" name="t3" value="<%=rs.getString(4)%>" ></td></tr>  
       <tr><td> Last Name:</td><td><input type="text" name="t4" value="<%=rs.getString(5)%>" ></td></tr>  
       </table>  
       *all fields are mandatory!<br>  
       <input type="Submit" value="Update" ><br>  
     </form>  
     </center>  
     <%  
        }  
     }  
     catch(Exception e){}  
 %>  
 </body>  
 </html>  

UpdateProfile.java
 package in.blog.webideaworld;  
 import java.sql.Statement;  
 import java.util.Map;  
 import in.blog.webideaworld.dao.DBconn;  
 import org.apache.struts2.interceptor.SessionAware;  
 import com.opensymphony.xwork2.ActionSupport;  
 public class UpdateProfile extends ActionSupport implements SessionAware {  
      private String t1;  
      private String t2;  
      private String t3;  
      private String t4;  
      Map m;  
      public void setSession(Map m)  
      {  
           this.m=m;  
      }  
   public String getT1() {  
           return t1;  
      }  
      public void setT1(String t1) {  
           this.t1 = t1;  
      }  
      public String getT2() {  
           return t2;  
      }  
      public void setT2(String t2) {  
           this.t2 = t2;  
      }  
      public String getT3() {  
           return t3;  
      }  
      public void setT3(String t3) {  
           this.t3 = t3;  
      }  
      public String getT4() {  
           return t4;  
      }  
      public void setT4(String t4) {  
           this.t4 = t4;  
      }  
 public String execute() throws Exception{  
           try{  
               int s1=(Integer)m.get("id");  
                  Statement st=new DBconn().getSt();  
            int i=st.executeUpdate("update register set uname='"+t1+"',upass='"+t2+"',fname='"+t3+"',lname='"+t4+"'"  
                               +"where id="+s1+"");  
            if(i!=0)  
            {  
                 m.put("uname", t1);  
                 m.put("upass", t2);  
                 return SUCCESS;  
            }  
           }  
           catch (Exception e) {}  
          return ERROR;  
      }  
 }  

success.jsp
create a user's profile successfully updated page.
 <%@ 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>Success</title>  
 </head>  
 <body>  
 <h4 align=right><a href=logout.action>Logout</a></h4>  
     <a href=profile.action>Profile</a>&nbsp&nbsp  
     <a href=#>FeedBack</a>&nbsp&nbsp  
     <a href=#>Contact Us</a></h5><hr>  
     <center>  
 Your Profile's record(s) updated successfully.</center>  
 </body>  
 </html>  

Logout.java
 package in.blog.webideaworld;  
 import java.util.Map;  
 import com.opensymphony.xwork2.ActionContext;  
 import com.opensymphony.xwork2.ActionSupport;  
 public class Logout extends ActionSupport{  
      public String logout() throws Exception{  
           Map session=ActionContext.getContext().getSession();  
           session.remove("id");  
           session.remove("uname");  
           session.remove("upass");  
           return SUCCESS;  
      }  
 }  

Download Code

View in Browser: 

Login Screen


Welcome Page


User's Profile


User's Profile Update Page

 
User's Profile After Update

No comments:

Popular Posts