Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Language: Java Servlets
Expertise: Beginner
Sep 25, 2000



Building the Right Environment to Support AI, Machine Learning and Deep Learning

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 if you 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 to bind session beans to servlet client sessions.

DevX Pro
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date