简体   繁体   English

使用Servlet的套接字服务器

[英]Socket server using servlets

I am about to make an application that needs to have a web interface for users to interact with and a low level UDP message interface with some existing applications. 我将要制作一个应用程序,该应用程序需要有一个供用户与之交互的Web界面以及一个与某些现有应用程序的低层UDP消息接口。 Is it possible for a java servlet based application to provide both a web interface and at the same time act as a UDP socket server. 基于Java Servlet的应用程序是否可以提供Web界面并同时充当UDP套接字服务器。

Both the interfaces are actually independent, the UDP interface is for receiving messages which shall be logged to a database, while the web interface is merely for viewing data from that database. 这两个接口实际上是独立的,UDP接口用于接收应记录到数据库的消息,而Web接口仅用于查看来自该数据库的数据。 I do not require any data to be pushed to the web interface asynchronously. 我不需要将任何数据异步推送到Web界面。

It is possible. 有可能的。 You can do it the spring way by using a ServletContextListener : 您可以通过使用ServletContextListener以春季方式进行操作:

  • you start your UDP server in an independant thread when receiving contextInitialized(ServletContextEvent sce) 您在接收contextInitialized(ServletContextEvent sce)时在独立线程中启动UDP服务器
  • you stop it (and clean all around) when receiving contextDestroyed(ServletContextEvent sce) 您在收到contextDestroyed(ServletContextEvent sce)时将其停止(并清理周围所有内容contextDestroyed(ServletContextEvent sce)

That way, your UDP service is aware of the servlet context and can share some information via the servlets through it. 这样,您的UDP服务就知道了servlet上下文,并且可以通过servlet共享一些信息。

But if the only interface between the 2 parts is the database, you can also have two independant applications in a KISS attitude ... 但是,如果这两个部分之间的唯一接口是数据库,那么您还可以以KISS的态度拥有两个独立的应用程序...

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

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