Fastest way to deploy a Java servlet?
What is the fastest way to deploy a Java HttpServlet
? Is there a solution that allows you to do it very quickly like开发者_Go百科 I could do in Ruby/PHP/Python with minimal configuration?
I need something that will allow me to quickly run the servlets and swap them during the debugging cycle. I'm not very good with Java especially when it comes to deploying.
I don't want to spend hours setting it up, messing with configuration files and sorting out dependencies. I just want something that I can install and run. Something less "enterprise-oriented".
I'm not using an IDE.
Is there something that can reduce deployment to just running something in the terminal, like with Ruby. Something that is not painful. Something that doesn't make me want to jump out of the window. Without trillions of XML files.
The best option so far is to ditch the whole "servlet" idea and deploy on a Java-powered HTTP server. It seems about 1000 times easier than anything that involves servlets.
Check out embedded Jetty. The configuration is all done in Java so you don't have to dink with a bunch of configuration files. Its extremely fast -- it takes about 2 seconds to start and no IDE is required. I usually run it in a terminal and just bounce it when I make a change, though you may be able to configure it to dynamically reload your changes. If you scroll to the bottom of that link, you'll see details about configuring servlets.
Here is some example code from the Jetty wiki, the server code:
public class OneServletContext
{
public static void main(String[] args) throws Exception
{
Server server = new Server(8080);
ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
context.setContextPath("/");
server.setHandler(context);
context.addServlet(new ServletHolder(new HelloServlet()),"/*");
context.addServlet(new ServletHolder(new HelloServlet("Buongiorno Mondo")),"/it/*");
context.addServlet(new ServletHolder(new HelloServlet("Bonjour le Monde")),"/fr/*");
server.start();
server.join();
}
}
And a sample servlet:
public class HelloServlet extends HttpServlet
{
private String greeting="Hello World";
public HelloServlet(){}
public HelloServlet(String greeting)
{
this.greeting=greeting;
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
response.setContentType("text/html");
response.setStatus(HttpServletResponse.SC_OK);
response.getWriter().println("<h1>"+greeting+"</h1>");
response.getWriter().println("session=" + request.getSession(true).getId());
}
}
Check out JRebel.
Have you looked at Grails? It's basically Groovy on Rails, the bytecode is Java if that counts.
Also look at this article about One-step deployment with Grails
In Eclipse you can effectively run the servlet directly from your workspace - it links quite cleanly to TomCat or WebSphere CE. Pretty much edit/save (auto compile)/ debug/
The fastest way is using your IDE of choice, of course. As for the minimal configuration, check out WebServlet annotation which appeared in Servlet 3.0 API. Using this annotation, you don't need any to add any additional information to web.xml.
精彩评论