Java应用程序的Web用户界面 [英] Web User Interface for a Java application

查看:138
本文介绍了Java应用程序的Web用户界面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为Java应用程序创建Web用户界面。用户界面非常简单,包括一个页面,其中包含一个供用户提出查询的表单,以及一个结果页面 - 有点像Google的搜索引擎或Ask.com。

I'm trying to create a web user interface for a Java application. The user interface is going to be very simple, consisting of a single page with a form for users to pose their queries, and a results page -- sort of like Google's search engine or Ask.com.

我对Java的基本API非常熟悉,但我没有太多使用Java进行Web环境的经验(尽管我使用过ASP.NET) ),所以我正在寻找一些建议:

I'm quite familiar with the base API of Java, but I don't have much experience in using Java for Web environments (though I've used ASP.NET), so I'm looking for some advice:


  • Web应用程序服务器我应该使用吗?请注意,我的界面很轻,我只想要一些快速,易于启动/重置/停止和(重新)部署我的应用程序的东西。此外,我需要它在多种环境上工作,即GNU / Linux,Mac OS X和Windows XP / Vista。另外,我正在使用 ant Eclipse ,所以如果我可以轻松添加一些 ant 服务器管理目标,和/或使用IDE管理服务器。我已经研究过 Tomcat Jetty ,而后者似乎非常轻巧,易于安装和部署。这是理想的,因为GUI仅用于演示目的,我可能需要将其部署在不同的计算机中。但是,Tomcat已经存在了很长时间,而且似乎更成熟。

  • What web application server should I use? Note that my interface is very light, and I just want something that is fast, easy to start/reset/stop and (re)deploy my application. Also, I need it to work on multiple environments, namely, GNU/Linux, Mac OS X, and Windows XP/Vista. Additionally, I'm using ant and Eclipse, so it would be great if I could easily add some ant targets for server management, and/or manage the server using the IDE. I've looked into Tomcat and Jetty, and the latter seems to be very light and easy to install and deploy. This is ideal, because the GUI is just for demonstration purposes, and I'll probably need to deploy it in different computers. However, Tomcat has been around for a very long time, and it seems more mature.

至于网页,Java服务器页面看起来很合适,因为它们看起来非常简单,我正在努力完成(处理表单并输出结果),但我很满意建议。

As for the web pages, Java Server Pages look like a good fit, as they seem sufficiently simple for what I'm trying to accomplish (processing a form and outputting the result), but I'm all ears for suggestions.

推荐答案


  1. App Server。您认为Tomcat在运行时占用空间,学习量或......方面都很重要?我倾向于选择与IDE完全集成的东西。所以Eclipse + Tomcat或Apache Geronimo,可能在它的 WebSphere Community Edition 中幌子会做的。从我所看到的,这些对你想要的东西来说足够了,学习曲线非常易于理解。

  2. 是JSP。您可能会发现您的演示文稿需求变得更加复杂。转到JSF的额外努力可能会有所回报 - 很好的小部件,例如日期选择器。

  3. 在您的处理中,您将拥有一个servlet(如果您使用的是JSF,则为一个动作类)该类可以在启动时初始化Engine类型的成员变量,然后用于每个请求。要记住的是,许多用户将同时击中该servlet,从而击中该引擎。您的引擎是否可以安全地同时使用多个线程?

  1. App Server. You see Tomcat as heavy in terms of runtime footprint, or amount of learning or ...? I would tend to chose something that has well established integration with an IDE. So Eclipse + Tomcat or Apache Geronimo, perhaps in it's WebSphere Community Edition guise would do the job. From what I've seen these are sufficient for what you want, and the learning curves are really pretty manageable.
  2. Yes JSPs. You may yet find that your presentation needs become a tad more complex. The extra effort of going to JSF might yet pay off - nice widgets such as Date pickers.
  3. In your processing you will have a servlet (or an action class if you're using JSF) that class can have a member variable of type Engine initialised on startup and then used for every request. The thing to bear in mind is that many users will hit that servlet, and hence that engine at the same time. Is your Engine safe to be used from more than one thread at the same time?

要进行扩展。在实现JSP时,有两种模型被称为模型1和模型2(具有一些创造性)。参见这个解释

To expand in this point. When implementing JSPs, there are two models refered to as (with some inventiveness) as Model 1 and Model 2. See this explanation.

在模型1的情况下,您倾向于将代码直接放入JSP中,它扮演控制器角色。 Persoanlly,即使在处理小型,快速开发的应用程序时,我也不是这样。我总是使用Model 2.但是如果你选择你可以把一些Java放到你的JSP中。

In the case of model 1 you tend to put code directly into the JSP, it's acting in a controller role. Persoanlly, even when dealing with small, quickly developed apps, I do not so this. I always use Model 2. However if you choose you can just put some Java into your JSP.

<%  MyWorker theWorker = MyWorkerFactory.getWorker();
    // theWorker.work();
%>

我赞成拥有这样的工厂,以便您可以控制工人的创建。工厂会有类似的东西(举一个非常简单的例子)

I woudl favour having a factory like this so that you can control the creation of the worker. The factory would have something like (to give a really simple example)

private static MyWorker s_worker = new MyWorker();
public static synchronized getWorker() {
       return s_worker;
}

或者,您可以在首次调用该方法时创建工作程序。

Alternatively you could create the worker when that method is first called.

在模型2的情况下,你自然会有一个servlet,你要在其中放入一些代码,所以你可以只有

In the case of model 2 you naturally have a servlet into which you are going to put some code, so you can just have

private MyWorker m_worker = MyWorkerFactory.getWorker();

这将在加载servlet时初始化。无需担心将其设置为在启动时加载,您只需知道它将在第一个请求运行之前初始化。
更好的是,使用servlet的init()方法。保证在处理任何请求之前调用它,并且这是用于此类工作的servlet API架构位置。

This will be initialised when the servlet is loaded. No need to worry about setting it to load on startup, you just know that it will be initialsed before the first request is run. Better still, use the init() method of the servlet. This is guranteed to be called before any requests are processed and is the servlet API architected place for such work.

public class EngineServlet extends HttpServlet {

private Engine engine;

// init is the "official" place for initialisation
public void init(ServletConfig config) throws ServletException {
     super.init(config);
     engine = new Engine();
} 

这篇关于Java应用程序的Web用户界面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆