为什么String没有重置为null?

时间:2014-12-30 14:37:46

标签: java eclipse login client server

我正在尝试使用服务器和客户端创建登录系统。现在几乎所有东西都按照我想要的方式工作,但是有一个问题。当我使用客户端登录时,它会发送persion已经输入的用户名和密码。如果您使用正确的信息登录,您将获得登录信息。但是如果你在第一个persion登录后输入了错误的用户名,它仍然有效。这是一个例子。

//更正登录信息

Username: xoleo
Password: 123
 = LOGGED IN

//错误的登录信息,但在正确的登录信息

后仍然有效
Username: xoleo123
Password: 123
 = LOGGED IN

//错误的登录信息,这个剂量工作,我不知道为什么第二种方式工作,这一个dident ..

Username: xoleo
Password: 1234
 = LOGGED FAILED

这是客户端的一些代码,在您按下login后发送信息:

    button2.addActionListener(new ActionListener() {
        @SuppressWarnings("deprecation")
        public void actionPerformed(ActionEvent arg0) {
            send(username2.getText(), password2.getText(), InGame.address, InGame.port);
        }
    });

}

private void send(String name, String pass, String address, int port) {
    new LoginSend(name, pass, address, port);
}

以下是'send(String name,String pass,String address,int port)'的代码:

package ca.sidez.main;

import javax.swing.JFrame;
import javax.swing.JTextField;
import ca.sidez.menu.InGame;
import ca.sidez.menu.Main;
import ca.sidez.menu.Main.STATE;
import ca.sidez.screen.ClientLogin;

public class LoginSend extends JFrame implements Runnable {
private static final long serialVersionUID = 1L;

private JTextField txtMessage;
private Thread run, listen;
private ClientLogin clientLogin;

private boolean running = false;

public LoginSend(String name, String pass, String address, int port) {
    clientLogin = new ClientLogin(name, address, port);
    boolean connect = clientLogin.openConnection(address);
    if (!connect) {
        System.err.println("Connection failed!");
        System.out.println("Connection failed!");
    }
    System.out.println("Försöker att ansluta til " + address + ":" + port + ", username: " + name);
    String connection = "/u/" + name + "/u/" + "/p/" + pass + "/p/";
    clientLogin.send(connection.getBytes());
    running = true;
    run = new Thread(this, "Running");
    run.start();
}

public void run() {
    listen();
}

public void listen() {
    listen = new Thread("Listen") {
        public void run() {
            while (running) {
                String message = clientLogin.receive();
                if (message.startsWith("/xp/")) {
                    InGame.Xp = message.split("/xp/|/xp1/")[1];
                    InGame.Gold = message.split("/g/|/e/")[1];
                    Main.State = STATE.LOADING;
                    System.out.println("Successfully Received Game Data To Player!");
                }
            }
        }
    };
    listen.start();
}
}

以下是来自服务器的一些代码,用于检查用户名和密码:

    } else if (string.startsWith("/u/")) {
        user = string.split("/u/|/u/")[1];          
        pass = string.split("/p/|/p/")[1];

        passx = TextFile.readFile("/" + user + ".txt");
            System.out.println(user);
            System.out.println(pass);
            System.out.println(passx);

        if(pass.equals(passx)) {
            System.out.println(user);
            System.out.println(pass);
            System.out.println(passx);
            System.out.println("LOGGED IN");
            passx = "";
            user = null;
            pass = null;

        } else {
            System.out.println(user);
            System.out.println(pass);
            System.out.println(passx);
            System.out.println("LOGGED FAILED");
            passx = "";
            user = null;
            pass = null;
        }

服务器控制台:

//这是正确的第一个:

User input: xoleo
User input: 123
Server Checks: 123

LOGGED IN

//这是错误的第二次:

user input: xoleo
user input: 1234
correct password for the username: 123 (checks for a file with that name)
LOGGED FAILED

//此用户名存在,但密码是旧的

user input: leo
user input: 123
server checks: 123
LOGGED IN

1 个答案:

答案 0 :(得分:0)

引用数据类型参数(如对象)也按值传递给方法。这意味着当方法返回时,传入的引用仍然引用与以前相同的对象。但是,如果对象的字段值具有适当的访问级别,则可以在方法中更改它们的值。