问题
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.
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:
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
andEclipse
, so it would be great if I could easily add someant
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.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.
- 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 methodrun(String)
which will process the user's input and return the results for display. 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 (i.e., until I stop the server). Then, for each user request, I'd simply invoke therun
method of theEngine
instance, and display its results. How can this be accomplished in Java?
回答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.
- 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.
- 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?
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.
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.
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();
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();
}
回答2:
The technology you need to learn is the Sun Java Servlet specification since that is what ALL non-trivial java webservers implement. This enables you to write servlets which can do all the things you need server side. You can then develop against any container working well with your iDe, and deploy on any other container working well in production.
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.
For your Engine to work you can create a servlet with a singleton in web.xml, which you can then call. 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.
回答3:
EDIT: So far, I've decided on the following:
- Web Application Server: Jetty;
- Java Server Pages for the views;
- 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):
form.jsp
<form action="/servlet/EngineServlet" method="GET">
<input type="text" name="text" />
</form>
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
<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 ? I also wrote some doubts that I have in the code as comments. Thanks.
回答4:
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):
- 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. 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.
回答5:
Jetty is a very lightweight container, and perfect for your development scenario.
You might want to look at Wicket for your rendering side; you seem more comfortable doing code-like tasks as opposed to straight UI.
The pattern you are describing is the Singleton pattern. Take a look at the google results for singleton in java.
回答6:
- app server: tomcat
- web page: jsp
- You need a class which is Singleton or class with static method. Word of caution: Beware of race condition. You might need to use synchronized keyword for those methods involving update/modify operation.
回答7:
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. 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. It sounds like your needs are pretty straightforward so you probably want to go with a simple solution like 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.
来源:https://stackoverflow.com/questions/1283468/web-user-interface-for-a-java-application