ruskaof
ruskaof

Reputation: 197

What is a proper way to inject an object that needs parameter inside servlets in java ee?

I'm making a java application using jsp and servlets. I want to store some data in HTTP session but I want to use the session only inside a RepositoryImpl class. I also have an interface called Repository with methods like addItem() and getData(). So my RepositoryImpl class implements these methods and uses HTTP session to store the data.

I want to inject the RepositoryImpl class into different servlets to use it as an instance of Repository interface. The problem is I don't know how to pass the session to the RepostioryImpl instance with ejb or cdi. What is the best way to handle my problem?

Upvotes: 0

Views: 25

Answers (1)

JM.
JM.

Reputation: 36

Let me see if i understand.

You want to store data in session, like a shopping cart?

If you want to store in session an entire object you can set it in a usebean.

<jsp:useBean id="objectname" scope="session" class="com.mypackage.myclass"/>

This way you can call the object and it's methods wherever the usebean is.

I hope this could help.

Upvotes: 1

Related Questions