Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fastest way to deploy a Java servlet?

Tags:

java

servlets

What is the fastest way to deploy a Java HttpServlet? Is there a solution that allows you to do it very quickly like 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.

like image 910
Kristina Brooks Avatar asked Aug 11 '11 19:08

Kristina Brooks


3 Answers

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());
    }
}
like image 145
jonathan.cone Avatar answered Oct 23 '22 01:10

jonathan.cone


Check out JRebel.

like image 20
Matt Ball Avatar answered Oct 23 '22 01:10

Matt Ball


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

like image 1
Ali Avatar answered Oct 23 '22 03:10

Ali