gpt4 book ai didi

java - Web 应用程序中静态对象的范围是什么

转载 作者:行者123 更新时间:2023-12-01 21:09:08 25 4
gpt4 key购买 nike

我有一个由 HTML 表单和 Servlet 组成的应用程序,该 Servlet 根据用户提交的参数创建一个新对象,然后将创建的 bean 添加到列表中。

有 JavaBean 类:

public class Client{

private String name, adress, tel, email;

public Client(String name, String adress, String tel, String email) {
this.name = name;
this.adress = adress;
this.tel = tel;
this.email = email;
}

//A bunch of Getters and Setters
}

这是 ClientsMan 类:

public abstract class ClientsMan {

private static List<Client> clientsList;

static
{
clientsList= new ArrayList<Client>();
}

public static void addClient(Client c)
{
clientsList.add(c);
}
}

下面是处理表单的 servlet 的 doPost() 方法:

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

//Getting the parametres
String name = request.getParameter("name");
String adress = request.getParameter("adress");
String tel = request.getParameter("tel");
String email = request.getParameter("email");

//Creating a Client object from the user inputs
Client client = new Client(name, adress, tel, email);

//Adding the bean in an arrayList
ClientsMan.addClient(client);

}

我需要保留所有添加的客户端的列表以供以后使用。

我的问题是:我的应用程序中列表的范围是什么,是请求范围还是应用程序范围?用户退出应用程序后我的列表会丢失吗?

最佳答案

What is the scope of the List in my application, is it a request scope or an Application Scope?

都不是,因为它的生命周期不是由您的应用程序管理的,List clientsList 是类 的一个 static 字段ClientsMan 这意味着它的作用域是初始化类 ClientsManClassLoader,即使用户退出应用程序,该类也应该存在。

关于java - Web 应用程序中静态对象的范围是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41576180/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com