简体   繁体   中英

store value after log-in java for all other files

I am developing an application in which the first window is

在此处输入图片说明

after clicking on log-in the log-in window appears and after correct credentials is is directed to choose time stamp and data set id window. This is how I am accessing the value of API key ( Login.java )

String value1=text1.getText();
String value2=text2.getText();
URL url = new URL("http://website-link/method/user.json?userName="+value1+"&password="+value2);

HttpURLConnection httpCon = (HttpURLConnection) url.openConnection(); 
BufferedReader in = new BufferedReader(new InputStreamReader(httpCon.getInputStream()));
String inputLine;
StringBuilder sb = new StringBuilder();
while ((inputLine = in.readLine()) != null) {
  sb.append(inputLine);
}

String Result;
Result=sb.toString();
String jsonSource = Result;
JSONArray array;
try {
  array = new JSONArray(jsonSource);
  for (int i = 0; i < array.length(); i++) {
    JSONObject firstObject = (JSONObject) array.get(i);
    System.out.println("APIkey is  " + firstObject.getString("apiKey"));
    APIkey=firstObject.getString("apiKey");
  }

I want that after successful log-in the user can anytime choose data set and time stamps. That is Login.java is passing a value to Algorithm.java (for choosing the data set and timestamp window)

if (httpCon.getResponseCode()==200) {
  Algorithm frame=new Algorithm(APIkey);
  frame.setSize(450,200);
  frame.setVisible(true);
  dispose();
  JLabel label = new JLabel("Welcome:"+value1);
}

now this API key is used in Algorithm.java to set the connection and select some specific nodes based on entered time stamps and data set id.

Now I want that after log-in I can anytime click on the second button shown in this window and choose the time stamp and data set id. But in that case I don't have the value of API key as I am using this in Main.java (responsible for the above window). And in Main.java the value of API key is null. I don't want the user to enter the log in username and password again and again.

if (e.getSource() == myFirstButton) {
  Login frame1=new Login();
  frame1.setSize(450,200);
  frame1.setVisible(true);
}

if (e.getSource() == mySecondButton) {
  Algorithm frame=new Algorithm(APIkey);
  frame.setSize(450,200);
  frame.setVisible(true);
}

Now how can I have the value of API key stored in all files once the user entered the correct access credentials in Login window and can use it anywhere. Each of the file: Log-in.java , Algorithm.java,Main.java is extending jframe. and Main.java is having the main() function I am working in Netbeans 7.1.2. I am coding this in Swing Java. I am new to Java.

The fastest way to do this would be to have a global variable APIKey in Main.java and set it's value after the user logs-in. And if you do this, I would also add a Logout button to unset this variable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM