如何在 JSP/Servlet 应用程序中实例化公共资源?

发布于 2024-12-26 10:00:19 字数 852 浏览 2 评论 0原文

我正在开发一个基于 Java 的 Web 应用程序,它将由几个 JSP 和一些 servlet 组成。 JSP 和 servlet 都需要访问专有的远程资源,可以通过通过 TCP 提交基于文本的请求来访问该资源(响应也是纯文本)。

为了实现这一点,我使用各种 getXbyId() 样式方法创建了一个 DAO 样式对象。在内部,DAO 维护一个 SocketPool,即具有同步 get()put() 方法的 Socket 集合。如果池已耗尽,调用 get() 将导致池增长(我可能应该限制池的大小,但我还没有做到这一点)。

我有上面的代码适用于单个 servlet。具体来说,Servlet init() 方法实例化 DAO 对象并将其存储为本地对象。我的计划是通过将 DAO 对象的单个实例放入应用程序范围(可能通过使其成为单例来强制执行)来对此进行扩展。每个 JSP 和 servlet 都需要检查应用程序范围中该对象是否存在,并在需要时对其进行初始化。 JSP 将能够使用 ,而 servlet 则需要以编程方式执行此操作(即,从应用程序上下文中获取它,如果获取返回 null,则实例化它)。上述场景的问题在于 DAO 无法使用无参数构造函数进行有意义的初始化。它需要参数来指定远程资源的 IP 地址、端口等。这些值存储在我从 ServletContext 加载的属性文件中(通过 getResourceAsStream)。

那么问题是,我应该如何最好地让这个 DAO 对象的单个实例可供我的应用程序中的所有 servlet 和 JSP 使用,而不需要大量重复且容易出错的样板初始化代码?

谢谢, 菲尔

I'm working on a Java based web app which will consist of a couple of JSPs and some servlets. The JSPs and servlets all need to access a proprietary remote resource which is accessible by submitting a text based request over TCP (the response is also plain text).

To facilitate this, I have created a DAO style object with various getXbyId() style methods. Internally, the DAO maintains a SocketPool, that is, a collection of Sockets with synchronized get() and put() methods. Calling get() will cause the pool to grow if it is depleted (I should probably cap the size of the pool but I'm not there yet).

I have the above code working for a single servlet. Specifically, the servlet init() method instantiates the DAO object and stores it as a local object. My plan was to expand on this by putting a single instance of the DAO object into application scope (possibly enforced by making it a singleton). Every JSP and servlet would need to check for the existance of this object in the application scope and initialise it where required. JSPs would be able to use <jsp:useBean> while servlets would be need to do this programmatically (that is, fetch it from application context and instantiate it if the fetch returns null). The problem with the above scenario is that the DAO cannot meaningfully be initialised with a no-arg constructor. It needs arguments to specify the IP address, port etc. for the remote resource. These values are stored in a properties file which I load from the ServletContext (via getResourceAsStream).

The question then is, how should I best go about making a single instance of this DAO object available to all of the servlets and JSPs in my application without lots of repetitive and error prone boiler-plate initialisation code?

Thanks,
Phil

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

后知后觉 2025-01-02 10:00:19

您在这里重新发明了各种各样的东西:

  • 参数,特别是端口号、IP 地址等数据应该在使用 web.xml 中定义,除非您有非常令人信服的理由不要这样做。 (?)

  • 考虑到您(让我猜一下)尝试将 noSQL 后端挂钩到您的 Web 应用程序,理想情况下您应该使用 JCA(Java 连接器架构)机制来隔离驱动程序。直接从 servlet 中加入阻塞网络调用和套接字池 (!) 是可行的,但就 Java EE-Web 容器为您提供的保证而言,所有的赌注都已落空,我个人永远不会允许这样的 hack/app最终进入生产服务器。曾经。

  • 即使您坚持这样做,也可以使用 ServletContextListener 将 Web 应用程序的生命周期与连接器的生命周期进行映射。

You are re-inventing all sorts of things here:

  • Parameters, specially data such as port number, ip address, etc. should be defined in using web.xml, unless you have very compelling reason not to do so. (?)

  • Given that you are (let me guess) trying to hook a noSQL backend to your web-app, ideally you should use JCA (Java Connector Architecture) mechanisms to isolate the driver. Throwing in blocking network calls and a socket pool (!) from the servlet directly can work, but all bets are off in terms of the guarantees that the Java EE-Web container provides you and I personally would never permit such an hack/app to end up in production servers. Ever.

  • Even if you insist on doing this, having a ServletContextListener map the life-cycle of your web-app with the life-cycle of the connector is the way to do this.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文