基于java的网站架构

发布于 2025-01-03 01:23:42 字数 1235 浏览 6 评论 0原文

我刚刚开始使用 Java 编程,所以可能看起来是一个愚蠢的问题,但我无法在互联网上找到简单的答案。这是我在深入开发之前必须回答的“大”问题,因此您可以帮助我节省大量尝试不同方法的时间。

我目前正在使用 Java 创建一个网站。该网站的重要功能将通过单独的 Java 线程(如守护进程)来实现,只要用户在网站上,该线程就必须在后台运行。同时,网站小程序必须具有与该线程通信的手段。

我看到了三种可能的解决方案:

  1. 创建传统的多页面网站,在每个单独的页面中使用独立的 Java 小程序。我不确定是否可能,出现以下问题:

    • 在用户导航到另一个网页(在同一网站上)后,java小程序创建的java线程是否可以继续执行?我想是的。
    • 新启动的java小程序是否可以与已经在后台运行的java线程进行通信? (我已经看过部分文档介绍了通过 JavaScript 在 Java 小程序之间进行通信,不确定这是否可以用于我的情况。还有其他选择吗?)
  2. 创建单页网站,使用一个 Java 小程序,负责所有操作导航和渲染所有页面。这解决了后台守护程序的问题,作为单个小程序的一部分,后台守护程序变得易于实现和通信,但又提出了一个问题:

    • 我知道小程序可以修改当前网页。利用这个功能模拟不同页面之间的导航是否可行?
  3. 为独立应用程序

我希望整个内容具有网站的外观和感觉,因此我更喜欢选项 3,而不是选项 2,更喜欢选项 1,而不是选项 2。

感谢您分享的任何想法。

更新: 有谁知道p1下两个问题的具体答案吗?是否可以按照描述的方式使用 java 线程?

现在我很可能会选择制作 Java Webstart 应用程序。这应该是最不痛苦的方法。

更新2: 我最终决定开发单个 java applet,如果需要,它可以轻松转换为 JWS 应用程序。我的项目的性质是我需要给人留下与网站合作的印象,这就是为什么我付出额外的努力让它看起来像一个网站。对于知识渊博的人来说,很明显,它更像是本地应用程序。 我选择的解决方案在我的情况下有以下好处: - 可以轻松地在 JWS 应用程序和 Java 小程序之间转换。 - 运行后台线程并与之通信没有问题。 - 更可靠的安全性(这意味着我不需要使用任何机制将会话 ID 从一个小程序传递到另一个小程序

) - 如果尺寸变大,启动会很慢 - 我希望避免这种情况。 - 安全问题 - 我尝试签署该小程序,它有很大帮助。 - 浏览器中导航按钮的工作(来回) - 我希望能够在小程序中复制它。认为小程序应该能够捕获这个事件。

I've just started programming with Java, so might appear as silly question, but I was not able to find simple answer on the internet. This is "big" question which I have to answer before getting too deep into development, so you can help me to save a lot of time on trying different approaches.

I am currently creating a website using Java. Important feature of this website would be realized through separate Java thread (like daemon) which must be run in background for as long as the user is on the website. At the sametime, website applets must have means of communication with this thread.

I see three potential solutions to this:

  1. Create traditional multipage website with stand-alone java applets in each separate page. I am not sure if it is possible, the following questions arise:

    • is it possible for java thread created by java applet to continue execution after user navigated to another webpage (on the same website)? I assume that yes.
    • is it possible for newly launched java applet to communicate with java thread already running in background? (I've seen part of documentation covering communication between java applets through JavaScript, not sure that this can be used in my case. Any other options?)
  2. Create single-page website, with one single java applet, responsible for all navigation and rendering all pages. This solves the problem with background daemon, which becomes easy to implement and communicate with, as part of single applet, but raises one more questions:

    • I know that applet can modify current webpage. Is it feasible to use this feature to simulate navigation between different pages?
  3. Create Java Webstart application, basically by taking the single java applet from p.2 and converting it into stand alone application.

I want the whole thing to have a look and feel of website, so I would prefer option 3 over option 2 and option 1 over option 2.

Thank you for any thoughts you share.

UPDATE:
Does anybody know answers specifically to the two questions under p1? If it is possible to work with java threads the way described?

Now I would most probably opt for making a Java Webstart Application. This should be the least painful way.

UPDATE 2:
I finally decided to work on single java applet, which can be easily converted to JWS application if needed. Nature of my project is that I need to make impression of working with website, that's why I am putting additional efforts to make it appear as a website. For knowledgeable people it will be obvious, that it is more like a local application.
Solution I chose has following benefits in my situation:
- easily convertable from JWS application to Java applet and back.
- no problems with running background thread and communicating with it.
- more reliable security (meaning that I don't need to use any mechanisms to pass over session ids from one applet to the other)

Contras:
- if size gets large, start up will be slow - I hope to avoid this.
- Security issues - I tried signing the applet and it helped a lot.
- Work of navigation buttons in browser (back and forth) - I hope to be able to replicated it in applet. Think applet should be able to catch this event.

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

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

发布评论

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

评论(3

梦过后 2025-01-10 01:23:42

Java 线程 [...] 只要用户在网站上,它就必须在后台运行

如果分叉的线程是为了在客户端登录时保留状态,那么我将使用数据库、内存缓存或某些其他持久层保存客户端会话状态。这是一个更典型的模型。您还可以拥有多个通过网络共享会话信息的前端。

如果您不与浏览器对话,那么创建独立的 Web 应用程序可能是最佳选择。您仍然可以使用 HTTP 作为传输协议,在这种情况下,我建议使用 Java Web 实现,例如 Jetty。这将涉及显着减少的技术和复杂性。

如果您需要实现网页,我肯定会使用适当的前端模型和技术。您应该将页面分成多个小程序 - 或 MVC 模型中的多个控制器/视图。使用小程序应该没问题。他们(或控制器)应该调用一个集中式服务,根据信息流启动、通信和停止后台线程。

我当然还会运行另一个后台线程,以使客户端线程超时,以防客户端永远不会返回。您的工作线程可能会在等待一定时间后退出。

希望这有帮助。


当用户导航到另一个网页(在同一网站上)后,java小程序创建的java线程是否可以继续执行?我认为是的。

是的。分叉的线程将继续运行,直到它们终止或(如果是守护进程)进程终止。

新启动的java小程序是否可以与已经在后台运行的java线程通信?

当然。他们需要的是某种能够共享数据的机制。例如,您的后台线程服务可以保存线程对象的Map,其键是某种客户端ID。每当一个线程想要与其后台线程对话时,它就可以调用该服务通过 id 查找该线程。例如:

BackgroundStream thread = threadService.getThread(clientId);
thread.callMethod(...);

如果有某种同步问题/响应,那么您需要有一个条件变量或其他东西。阅读 Java 线程 了解更多信息。

Java thread [...] which must be run in background for as long as the user is on the website

If the thread being forked is to preserve state while the client is logged in then I would use a database, memory cache, or some other persistance layer to hold the client session state. This is a much more typical model. You can also have multiple frontends that share session information across the network.

If you are not talking to a browser then creating a stand-alone web application may be the best choice. You can still use HTTP as your transfer protocol in which case I'd recommend using a Java web implementation like Jetty. This would involve significantly less technology and complexity.

If you need to implement web pages, I would certainly use proper frontend models and technology. You should separate your pages into multiple applets -- or multiple controllers/views in the MVC model. Using the applets should be fine. They (or the controllers) should call a centralized service which starts, communicates with, and stops the background threads depending on the information flow.

I would also certainly have another background thread running to time out your client threads in case a client never returns. Your worker threads could just exit after a certain amount of waiting.

Hope this helps.


is it possible for java thread created by java applet to continue execution after user navigated to another webpage (on the same website)? I assume that yes.

Yes. Threads that are forked will continue to run until they terminate or (if daemon) the process terminates.

is it possible for newly launched java applet to communicate with java thread already running in background?

Sure. What they need is some mechanism to be able to share data. For example, your background thread service could keep a Map of the thread objects with the key being some sort of client-id. Whenever a thread wanted to talk to it's background thread then it could call the service to lookup the thread by id. Something like:

BackgroundStream thread = threadService.getThread(clientId);
thread.callMethod(...);

If there was some sort of synchronous question/response then you'd need to have a condition variable or something. Read up on Java threads for more information.

睫毛溺水了 2025-01-10 01:23:42

Java 网站应用程序中使用了一种架构,称为 模型-视图-控制器。 Java Server Faces(Java EE 5 及更高版本的标准)、Struts(1.x 或 2.x)、Spring、Apache Wicket 等框架旨在使用 MVC 模型创建 Web 应用程序。问题是,您是否喜欢基于组件的框架架构(例如 JSF)或不喜欢(此时您不必担心)

Applet 绝对是一个糟糕的选择,因为 Applet 是下载到客户端的。某些浏览器不支持小程序,尤其是在移动网络浏览器中,并且很难将安全设置应用于不受信任的小程序,而且您可能不知道客户端是否阻止了小程序。

There's an architecture used in Website applications in Java, it's called a Model-View-Controller. Frameworks such as Java Server Faces (Standard on Java EE 5 and higher), Struts (1.x or 2.x), Spring, Apache Wicket, etc. were designed to create web applications using MVC model. The question is, would you prefer the component-based architecture of the framework (such as JSF) or not (which you shouldn't be worried about at this moment)

Applets is defnitely a bad choice as applets are downloaded to client side. Some browsers don't support Applets especially in mobile web browsers and its difficult to apply security settings to untrusted applets, plus you may not know if the client has blocked the applet or not.

人间不值得 2025-01-10 01:23:42

Applet 是一个糟糕的选择,原因有两个:

1)首先,它们在客户端浏览器上执行,而不是在服务器上执行。因此,您无法使用小程序执行任何后端处理(业务逻辑或从服务器数据库获取数据)。

2) 小程序有很多错误并且存在安全问题。这就是小程序现在已经过时的原因。

现在介绍如何使用 Java 技术创建网站,为此您需要开始了解 Java 服务器端编程。开始学习 Java Servlet 和 Java Server Pages。简单来说,它们是在 Web 服务器或应用程序服务器上执行的 java 程序。

然后开始阅读有关 Java 企业版的内容。

请参阅Java 企业版教程

Applets are a bad choice because of 2 reasons:

1) First, they are executed on client's browser and not on server. Therefore you cannot perform any backend processing(Business logic or fetching data from server database) using applets.

2) Applets are very buggy and have security issues. That's why applets are out of fashion these days.

Now coming to how you can create website using java technology, for that you need to start understand Java Server Side programming. Start learning about Java Servlets and Java Server Pages. To put it in simple terms, they are java programs which are executed on a web-server or application server.

then start reading about Java Enterprise Edition.

refer this tutorial for Java Enterprise Edition

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