简体   繁体   English

为什么不将String重置为null?

[英]Why does the String not reset to null?

im trying to create a login system with an server and a client. 我试图用服务器和客户端创建登录系统。 right now almost every thing works as i want to but there is one problem.. When i login with the client it sends the Username and password that the persion have inputed.if you login with correct information you will get logged in ofcurse. 现在几乎所有的事情的作品,我想,但有一个问题。当我用它发送的用户名和密码,该persion有inputed.if你,你会得到登录ofcurse正确的信息登录客户端登录。 but if you put in a wrong username after the first persion logged in. it will still work.? 但如果你把一个错误的用户名,第一persion后登陆,它仍然可以工作? heres an exempel. 这是一个范例。

//Correct Login info //正确的登录信息

Username: xoleo
Password: 123
 = LOGGED IN

//Wrong Login Info but still works after the correct login info //错误的登录信息,但在正确的登录信息后仍然可以使用

Username: xoleo123
Password: 123
 = LOGGED IN

//Wrong Login Info, this dosent work, and i dont know why the second way worked and this one dident.. //错误的登录信息,这dosent工作,我不知道为什么第二种方式工作,这一个dident ..

Username: xoleo
Password: 1234
 = LOGGED FAILED

here is some code from the client side that sends the info after you press 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);
}

Here is the code that for the 'send(String name, String pass, String address, int port)': 下面是为“发(字符串名称,字符串传递,字符串地址,INT端口)”的代码:

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();
}
}

Here is some code from the server that will check the username and password: 下面是从将检查用户名和密码的服务器的一些代码:

    } 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;
        }

The Server Console: 服务器控制台:

//this is the correct first one: //这是正确的第一个:

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

LOGGED IN

//this is the wrong second time: //这是第二次错误:

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

//this username doent exist, but the password is the old one //这个用户名确实存在,但是密码是旧的

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

Reference data type parameters, such as objects, are also passed into methods by value. 引用数据类型参数(例如对象)也按值传递到方法中。 This means that when the method returns, the passed-in reference still references the same object as before. 这意味着当方法返回时,传入的引用仍然引用与以前相同的对象。 However, the values of the object's fields can be changed in the method, if they have the proper access level. 但是,如果对象的字段的值具有适当的访问级别,则可以在方法中更改它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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