开发者

How to access STATIC Variables from java class to another in JAVA STRUTS2?

I am trying to access STATIC Variables from one java class to another but am not getting the value!

Below are my java classes.

Static Contents, class 1:

package com.globalProperties;
public class GlobalVariables {

static{
    someFunctionName();
}

    private static String SMTP_SERVER = "";
    private static String SMTP_USERNAME = "";
    private static String SMTP_PASSWORD = "";

    private static void someFunctionName(){
         Connection con = null;
         Statement statement = null;
         ResultSet resultset = null;
         LinkedHashMap<String,String> sysParmHashMap = new LinkedHashMap<String, String>();
             con = DatabaseConnection.getDbConnection();
             statement = con.createStatement();
             String queryStr = "SELECT * FROM SMTPCONNECTION";
             resultset = statement.executeQuery(queryStr);
             while(resultset.next()){
                 sysParmHashMap.put(resultset.getString("KEY"), resultset.getString("KEY_VALUE"));
             }

                // First i tried this to set value 
                SMTP_SERVER = sysParmHashMap.get("SMTP_SERVER").trim();
                SMTP_USERNAME = sysParmHashMap.get("SMTP_USERNAME").trim();
                SMTP_PASSWORD = sysParmHashMap.get("SMTP_PASSWORD").trim();

                // Second time, i tried this to set value 
                setSMTP_SERVER(sysParmHashMap.get("SMTP_SERVER").trim());
                setSMTP_USERNAME(sysParmHashMap.get("SMTP_USERNAME").trim());
                setSMTP_PASSWORD(sysParmHashMap.get("SMTP_PASSWORD").trim());


                System.out.println("SMTP_SERVER :" +SMTP_SERVER);
                System.out.println("SMTP_USERNAME :" +SMTP_USERNAME);
                System.out.println("SMTP_PASSWORD :" +SMTP_PASSWORD);
         }

    public static String getSMTP_SERVER() {
        return SMTP_SERVER;
    }

    public static void setSMTP_SERVER(String sMTP_SERVER) {
        SMTP_SERVER = sMTP_SERVER;
    }

    public static String getSMTP_USERNAME() {
        return SMTP_USERNAME;
    }

    public static void setSMTP_USERNAME(String sMT开发者_Python百科P_USERNAME) {
        SMTP_USERNAME = sMTP_USERNAME;
    }

    public static String getSMTP_PASSWORD() {
        return SMTP_PASSWORD;
    }

    public static void setSMTP_PASSWORD(String sMTP_PASSWORD) {
        SMTP_PASSWORD = sMTP_PASSWORD;
    }

    public static String getGATEWAY_SERVICE_URL() {
        return GATEWAY_SERVICE_URL;
    }

}

Here i want to acess those static variables Class 2:

package com.unilog.sendMails;
import com.globalProperties.GlobalVariables;
improt .... some imports ....;

public class sendEmailTo extends ActionSupport implements ServletResponseAware {


    public String MailContact(){

        String result = "Fail";

    if(Some condition){
      result = sendingEmail(  --  Parameters  --);
        }



    }

    public String sendingEmail(String toAddress,String fromAddress,String mailSubject,String mailBody){
        String result="FAIL";

        // For Below variable am not getting any value they are Empty but not null
            String smtpHost = GlobalVariables.getSMTP_SERVER();  
            final String smtpUserName = GlobalVariables.getSMTP_USERNAME();
        final String smtpPassword = GlobalVariables.getSMTP_PASSWORD();

    }
}


Issue solved, i was assigning VARIABLE ="" (empty value for static variable in declaration time)

It was :

 private static String SMTP_SERVER = "";
    private static String SMTP_USERNAME = "";
    private static String SMTP_PASSWORD = "";

Where it was suppose to be like this :

 private static String SMTP_SERVER;
    private static String SMTP_USERNAME;
    private static String SMTP_PASSWORD;

Now its working fine......

Thank you.....

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜