Storing InitialContext in Servlet Sessions

Storing InitialContext in Servlet Sessions

Suppose a client of a Web application logs in using a login-handler servlet, and then some back-end operations using EJBs need to be performed. Can I store the initial context that I create and/or the home interface itself?

Yes, you can store both the InitialContext instance and the home interface instance. You will only want to store the InitialContext ifyou plan on creating other EJBs later in the session. Rather than store an InitialContext in an HttpSession, you should save it as a member variable since you can use the same InitialContext to create EJBs for different sessions.

You can store a home interface in the client’s session, but keep in mind that it is better to do this only with session beans. You will typically create one session bean per client session and let the session bean interact with entity beans. You may want to create multiple session beans in the servlet during the life of a session to perform different tasks, such as authentication.

Alternatively, you may want to create a single session bean that encapsulates all of the actions that may occur during a session. This master session bean will create other session beans as needed to perform the desired tasks. Ultimately, one of the important roles a servlet in J2EE is tobind session beans to servlet client sessions.

See also  Get the Most Out of Virtual Credit Cards For Business

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist