简体   繁体   English

为什么从另一个Java Servlet或Java类调用Java Servlet类中的方法会返回null?

[英]Why method in Java servlet class return null when called from another java servlet or java class?

I have two Java Servlets Organization1 and Organization2. 我有两个Java Servlets Organization1和Organization2。 I have saved the response value of Organization1 to one global variable called org1. 我已经将Organization1的响应值保存到一个名为org1的全局变量中。 Then I created a method getOrg1Name() in Organization1 which returns the value which is saved in that global variable org1. 然后,我在Organization1中创建了方法getOrg1Name(),该方法返回保存在该全局变量org1中的值。 Please check code below: 请检查以下代码:

   public class Organization1 extends HttpServlet {

    private String org1;

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        PrintWriter pw = response.getWriter();
        response.setContentType("text/html");

        this.org1 = request.getParameter("org1_name");

}
    public String getOrg1Name()
    {
        return this.org1;
    }

} Then after I created a 2nd servlet Organization2. 然后,在创建第二个servlet Organization2之后。 Inside the doPost() method of Organization2, I created a instance of Organization1 so that I can call that method getOrg1Name() which returns the value saved in global variable org1. 在Organization2的doPost()方法中,我创建了Organization1的实例,以便可以调用该方法getOrg1Name(),该方法返回保存在全局变量org1中的值。 Please check code below: 请检查以下代码:

    public class Organization2 extends HttpServlet {

        private String org2;

        public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

            PrintWriter pw = response.getWriter();
            response.setContentType("text/html");

            this.org2 = request.getParameter("org2_name");

            Organization1 organization1 = new Organization1();
            String org1 = organization1.getOrg1Name();

            // org1 is always null. Why??   
    }
    }

But each time method getOrg1Name() returns null. 但是每次方法getOrg1Name()返回null。 Can someone help me with this issue? 有人可以帮我解决这个问题吗?

Field org1 in Organization1 is not a global variable in your case - it's a private field of class Organization1. 在您的情况下,Organization1中的org1字段不是全局变量-它是Organization1类的私有字段。 That means when you create new instance of Organization1 field org1 is set to its default value. 这意味着当您创建Organization1的新实例时,字段org1将被设置为其默认值。 Default value of String is null. String的默认值为null。

If you want everything to work the way I see you want, you have to declare field org1 as static. 如果您希望一切按照我希望的方式工作,则必须将字段org1声明为静态。

private static String org1;

In that case all instances of class Organization1 will have a link to one org1 instance. 在这种情况下,类Organization1的所有实例将具有一个org1实例的链接。

However, this approach has a problem. 但是,这种方法存在问题。 Value of a field org1 will be rewritten with every request on servlet Organization1. 字段org1的值将随servlet Organization1上的每个请求一起重写。 So it is a good task to understand how static fields work, but code smell in real programming. 因此,了解静态字段的工作原理是一件好事,但在实际编程中却会闻到代码的味道。

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

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