简体   繁体   English

Java应用程序的Web用户界面

[英]Web User Interface for a Java application

I'm trying to create a web user interface for a Java application. 我正在尝试为Java应用程序创建Web用户界面。 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. 用户界面非常简单,包括一个页面,其中包含一个供用户提出查询的表单,以及一个结果页面 - 有点像Google的搜索引擎或Ask.com。

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

  • What web application server should I use? 我应该使用什么Web应用程序服务器 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. 此外,我需要它在多种环境中工作,即GNU / Linux,Mac OS X和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. 另外,我正在使用antEclipse ,因此如果我可以轻松地为服务器管理添加一些ant目标和/或使用IDE管理服务器,那将会很棒。 I've looked into Tomcat and Jetty , and the latter seems to be very light and easy to install and deploy. 我已经研究过TomcatJetty ,后者看起来非常轻巧,易于安装和部署。 This is ideal, because the GUI is just for demonstration purposes, and I'll probably need to deploy it in different computers. 这是理想的,因为GUI仅用于演示目的,我可能需要将其部署在不同的计算机中。 However, Tomcat has been around for a very long time, and it seems more mature. 然而,Tomcat已经存在了很长时间,而且似乎更成熟。

  • 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. 至于网页 ,Java Server Pages看起来很合适,因为它们看起来非常简单,我正在努力完成(处理表单并输出结果),但我很满意建议。

  • I also have another requirement, which requires me to explain the "basic" workflow of the application: Basically, I have a class Engine which has a method run(String) which will process the user's input and return the results for display. 我还有另一个要求,这需要我解释应用程序的“基本”工作流程:基本上,我有一个类Engine ,它有一个方法run(String) ,它将处理用户的输入并返回显示结果。 This class is the core of the application. 这个类是应用程序的核心 Now, I'd like to instantiate this class only once , as it requires a lot of memory, and takes a very long time to startup, so I'd like to create it when the application/server starts, and store that reference for the entire span of the application (ie, until I stop the server). 现在,我只想将这个类实例化 一次 ,因为它需要大量内存,并且需要很长时间才能启动,所以我想在应用程序/服务器启动时创建它,并存储该引用应用程序的整个范围(即,直到我停止服务器)。 Then, for each user request, I'd simply invoke the run method of the Engine instance, and display its results. 然后,对于每个用户请求,我只需调用Engine实例的run方法,并显示其结果。 How can this be accomplished in Java? 如何用Java实现这一目标?

  1. App Server. App Server。 You see Tomcat as heavy in terms of runtime footprint, or amount of learning or ...? 您认为Tomcat在运行时占用空间,学习量或......方面都很重要? I would tend to chose something that has well established integration with an IDE. 我倾向于选择与IDE完全集成的东西。 So Eclipse + Tomcat or Apache Geronimo, perhaps in it's WebSphere Community Edition guise would do the job. 所以Eclipse + Tomcat或Apache Geronimo,或许在它的WebSphere Community Edition中就可以完成这项工作。 From what I've seen these are sufficient for what you want, and the learning curves are really pretty manageable. 从我所看到的,这些足以满足你的需求,学习曲线非常易于管理。
  2. Yes JSPs. 是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. 进入JSF的额外努力可能会得到回报 - 像日期选择器这样的小工具。
  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. 在您的处理过程中,您将拥有一个servlet(如果您使用的是JSF,则为动作类)该类可以在启动时初始化Engine类型的成员变量,然后用于每个请求。 The thing to bear in mind is that many users will hit that servlet, and hence that engine at the same time. 要记住的是,许多用户将同时击中该servlet,从而击中该引擎。 Is your Engine safe to be used from more than one thread at the same time? 您的引擎是否可以安全地同时使用多个线程?

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 . 在实现JSP时,有两种模型被称为(具有一些创造性)作为模型1和模型2.请参阅此解释

In the case of model 1 you tend to put code directly into the JSP, it's acting in a controller role. 在模型1的情况下,您倾向于将代码直接放入JSP中,它扮演控制器角色。 Persoanlly, even when dealing with small, quickly developed apps, I do not so this. Persoanlly,即使在处理小型,快速开发的应用程序时,我也不是这样。 I always use Model 2. However if you choose you can just put some Java into your JSP. 我总是使用Model 2.但是如果你选择的话,你可以把一些Java放到你的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. 或者,您可以在首次调用该方法时创建worker。

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 在模型2的情况下,你自然会有一个servlet,你要在其中放入一些代码,所以你可以拥有

private MyWorker m_worker = MyWorkerFactory.getWorker();

This will be initialised when the servlet is loaded. 这将在加载servlet时初始化。 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. 更好的是,使用servlet的init()方法。 This is guranteed to be called before any requests are processed and is the servlet API architected place for such work. 保证在处理任何请求之前调用它,并且这是用于此类工作的servlet API架构位置。

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();
} 

The technology you need to learn is the Sun Java Servlet specification since that is what ALL non-trivial java webservers implement. 您需要学习的技术是Sun Java Servlet规范,因为这是所有非平凡的Java Web服务器实现的。 This enables you to write servlets which can do all the things you need server side. 这使您可以编写servlet,它可以完成服务器端所需的所有操作。 You can then develop against any container working well with your iDe, and deploy on any other container working well in production. 然后,您可以针对任何与iDe配合良好的容器进行开发,并部署在生产中运行良好的任何其他容器上。

You also need to learn basic HTML as you otherwise would need to learn JavaServer Faces or similar which is a rather big mouthfull to create the submit button you need with the other entries in a HTML form. 您还需要学习基本的HTML,因为您需要学习JavaServer Faces或类似的东西,这是一个相当大的口碑,用于创建HTML表单中其他条目所需的提交按钮。

For your Engine to work you can create a servlet with a singleton in web.xml, which you can then call. 要使您的Engine工作,您可以在web.xml中创建一个带有单例的servlet,然后可以调用它。 Be absolutely certain it is thread safe otherwise you will have a lot of pain. 绝对肯定它是线程安全的,否则你会有很多痛苦。 For starters you can declare your invoking servlet synchronized to ensure that at most one call of run() is active at any time. 对于初学者,您可以声明您的调用servlet是同步的,以确保最多一次run()调用在任何时候都是活动的。

EDIT : So far, I've decided on the following: 编辑 :到目前为止,我已决定以下内容:

  • Web Application Server: Jetty ; Web应用程序服务器: Jetty ;
  • Java Server Pages for the views; 用于视图的Java Server Pages ;
  • Based on the suggestions of @djna, I've read a couple of articles regarding Model 2 , and I came up with this solution (which I haven't tested yet, because I need to finish my application before moving into the interface): 根据@djna的建议,我读了几篇关于模型2的文章,我想出了这个解决方案(我还没有测试过,因为我需要在进入界面之前完成我的应用程序):

form.jsp form.jsp

<form action="/servlet/EngineServlet" method="GET">
  <input type="text" name="text" />
</form>

EngineServlet.java EngineServlet.java

public class EngineServlet extends HttpServlet {
  private Engine engine = new Engine(); 
  // does this make sure engine only gets instantiated one time in the entire lifespan of the web application; from what I've read from the servlet lifecycle, it seems like it, but I'd like to hear opinions

  public void doGet(HttpServletRequest request,
                    HttpServletResponse response) {
    String text = request.getParameter("text");
    ResultBean result = engine.run(text);
    request.setAttribute("result", result);
    RequestDispatcher dispatcher = request.getRequestDispatcher("result.jsp");
    dispatcher.forward(request, response); 
    // what's the difference between forward, and request.sendRedirect() ?
  }    
}

result.jsp result.jsp中

<div>The result was: ${result.text}</div>

What do you think of this solution? 您如何看待这个解决方案? Any problems that may not be obvious to someone coming from a J2SE background ? 对于来自J2SE背景的人来说,这些问题可能并不明显吗? I also wrote some doubts that I have in the code as comments. 我还在代码中写了一些疑问,作为评论。 Thanks. 谢谢。

Assuming this isn't a one-off application which doesn't need any kind of updating/maintenance in the future, I'd recommend you do the view layer with Apache Wicket for the following reasons ( read the short infoblurb from the homepage first ): 假设这不是一个一次性的应用程序,将来不需要任何更新/维护,我建议您使用Apache Wicket执行视图层,原因如下( 首先从主页阅读简短的信息页面 ):

  • Since Wicket separates the view layer and works in the model layer of MVC in a clean way, it can be easily explained that view is completely separated from rest of the application and Wicket's IModel interface is used to link the data from controller layer to the view layer in a reliable way. 由于Wicket以清晰的方式分离视图层并在MVC的模型层中工作,因此可以很容易地解释视图与应用程序的其余部分完全分离,并且Wicket的IModel接口用于将数据从控制器层链接到视图层以可靠的方式。 Thus your controller layer maybe a single application singleton as long as you use it that way. 因此,只要您以这种方式使用控制器层,您的控制器层就可以是单个应用程序单例。
  • Wicket code is stunningly easy to maintain, also extending functionality of your web application can be done very easily since it's OOP framework instead of markup mixed with other kind of markup which expresses code. Wicket代码非常易于维护,因为它是OOP框架而不是与其他表示代码的标记混合的标记,因此可以非常轻松地扩展Web应用程序的功能。
  1. Jetty is a very lightweight container, and perfect for your development scenario. Jetty是一个非常轻量级的容器,非常适合您的开发场景。

  2. You might want to look at Wicket for your rendering side; 你可能想看看Wicket的渲染方面; you seem more comfortable doing code-like tasks as opposed to straight UI. 你似乎更喜欢做类似代码的任务,而不是直接的UI。

  3. The pattern you are describing is the Singleton pattern. 您描述的模式是Singleton模式。 Take a look at the google results for singleton in java . 看一下java中单例的google结果。

This is a pretty open ended question, and there are a huge number of possible answers depending on your requirements. 这是一个非常开放的问题,根据您的要求,有大量可能的答案。 The standard way to write web applications is using the Java EE platform, which means JSPs, servlets, and EJBs for the business logic. 编写Web应用程序的标准方法是使用Java EE平台,这意味着业务逻辑的JSP,servlet和EJB。 However, there are quite a few popular and valid alternatives such as Spring, Seam, GWT, and even more radical alternatives such as JRuby on Rails. 但是,有很多流行和有效的替代品,如Spring,Seam,GWT,甚至更激进的替代品,如JRuby on Rails。 It sounds like your needs are pretty straightforward so you probably want to go with a simple solution like Jetty + Servlets + JSP. 听起来你的需求非常简单,所以你可能想要使用像Jetty + Servlets + JSP这样的简单解决方案。

I am assuming your Engine can handle multiple simultaneous requests? 我假设你的引擎可以同时处理多个请求? If not, you may want to look at figuring out a way to queue requests, such as JMS. 如果没有,您可能想要找出一种排队请求的方法,例如JMS。

  1. app server: tomcat app server:tomcat
  2. web page: jsp 网页:jsp
  3. You need a class which is Singleton or class with static method. 你需要一个Singleton类或静态方法类。 Word of caution: Beware of race condition. 注意事项:谨防竞争状况。 You might need to use synchronized keyword for those methods involving update/modify operation. 您可能需要对涉及更新/修改操作的那些方法使用synchronized关键字。

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

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