Tuesday, September 2, 2014

How to work session variable

Java Action class :

package com.allClass.Action;


import com.allClass.Database.DatabaseConnection;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;// This is use for add field error



import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;


import java.util.Date;// For date
import util.DateFormating;//For date




public class LogInPageAction extends ActionSupport{
   
    // use for log in username and passward kaniz
    private String userName;
    private String password;
    private String datVal;
   
    //
    private Connection con = null;
    private Statement st = null;
    private CallableStatement cs = null;
    private ResultSet rs = null;
    private DatabaseConnection dbc = new DatabaseConnection();
    String query = "";   

    //START Login code **************
    public String userSignIns(){
           
         int usrname = 0;
        //String usrname = "";
        System.out.println("hhhh");
            boolean fg = true;
            con = dbc.connectDB();

            if (con == null) {
                fg = false;
            }
           
           
           
            if (fg) {
                try {

                    st = con.createStatement();

                  
                   
      //rs.getString for:// query = "select USER_ID from  login_users where USER_ID='" + userName + "' and PASSWORD='" + password + "'";              
      query = "select * from  login_users where LOGIN_NAME='" + userName + "' and PASSWORD='" + password + "'";                           
                    System.out.println(query);

                    rs = st.executeQuery(query);

                    while (rs.next()) {
                        usrname = rs.getRow();
                        //usrname = rs.getString("USER_ID"); or usrname = rs.getString("LOGIN_NAME");
                       
                    }
                   
                   

                } catch (SQLException sq) {            
                    sq.printStackTrace();
                } finally {
                    try {
                        if (rs != null) {
                            rs.close();
                        }
                      
                        con.close();
                    } catch (SQLException ex) {
                        ex.printStackTrace();
                    }
                }
            }
           
           // if (!usrname.isEmpty()) {
            if (usrname>0) {
               
                //************************ Start My necessary call function ************************
               
                              // Start for date and import java dot util
                                DateFormating df=new DateFormating();
                                Date date_today=new Date();
                                 datVal=df.dateFormatDBOracle(date_today);
     ActionContext.getContext().getSession().put("logInTimekkk",datVal);
                             // End For date java dot util
               
               
                //************************ End My necessary call function ************************
               
                return "userSignIns";
            }else{
               
                addFieldError("logMsg","Your Account Is Not active!!"); //N.B: After add field error message
                return "logInFail";   
            }
           
           
            //return "enterLogInPageFail";
        }

   
   
   
   
   
   
   
    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }








           
    //END Log in code ***************   
       

   

    public String getDatVal() {
        return datVal;
    }

    public void setDatVal(String datVal) {
        this.datVal = datVal;
    }
   
   
   
   
   
   
   
}





Now jsp page get session value and show this value :

   <%
 
      String loginTimeVal =(String)ActionContext.getContext().getSession().get("logInTimekkk");

%>




<tr>
  <th><span class="">Name :</span> <span><s:property value="userName"/></span>&nbsp;&nbsp;&nbsp;&nbsp;</th>
  <!-- th><span class="">Logged on at :</span> <span> <s:property value="datVal"/></span></th-->
  <th><span class="">Logged on at :</span> <span> <%=loginTimeVal %></span></th>
</tr>

No comments:

Post a Comment