简体   繁体   English

在 java ee 中注入 servlets 中需要参数的 object 的正确方法是什么?

[英]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.我正在使用 jsp 和 servlets 制作 java 应用程序。 I want to store some data in HTTP session but I want to use the session only inside a RepositoryImpl class.我想在 HTTP session 中存储一些数据,但我想仅在RepositoryImpl中使用 session I also have an interface called Repository with methods like addItem() and getData() .我还有一个名为Repository的接口,其中包含addItem()getData()等方法。 So my RepositoryImpl class implements these methods and uses HTTP session to store the data.所以我的RepositoryImpl class 实现了这些方法,并使用 HTTP session 来存储数据。

I want to inject the RepositoryImpl class into different servlets to use it as an instance of Repository interface.我想将RepositoryImpl class 注入不同的 servlets 以将其用作Repository接口的实例。 The problem is I don't know how to pass the session to the RepostioryImpl instance with ejb or cdi .问题是我不知道如何使用ejbcdisession传递给RepostioryImpl实例。 What is the best way to handle my problem?处理我的问题的最佳方法是什么?

Let me see if i understand.让我看看我是否明白。

You want to store data in session, like a shopping cart?您想像购物车一样将数据存储在 session 中吗?

If you want to store in session an entire object you can set it in a usebean.如果你想在 session 中存储整个 object,你可以在一个 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.通过这种方式,您可以调用 object 及其方法,无论 usebean 位于何处。

I hope this could help.我希望这会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM