Issue
I have an application that consists of an HTML form and a Servlet that Creates a new object from the parameters submitted by the user, then adds the created bean to a list.
There is the JavaBean class:
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
}
Here is the ClientsMan class:
public abstract class ClientsMan {
private static List<Client> clientsList;
static
{
clientsList= new ArrayList<Client>();
}
public static void addClient(Client c)
{
clientsList.add(c);
}
}
Here is the doPost() method of the servlet that handles the form:
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);
}
I will need to keep a list of all the clients added for later use.
My question is: What is the scope of the List in my application, is it a request scope or an Application Scope? Will I lose my list after the user quits the application?
Solution
What is the scope of the List in my application, is it a request scope or an Application Scope?
None of them because its lifecycle is not managed by your application, the List
clientsList
is a static
field of the class ClientsMan
which means that it is rather scoped to the ClassLoader
that initialized the class ClientsMan
which should exist even after the user quits the application.
Answered By - Nicolas Filotto