Imagine a bacon-wrapped Ferrari. Still not better than our free technical reports.

JRebel and Java EE – Let’s be productive series: WebServlets


We had a great webinar with Red Hat showing off JRebel enabling developers to be more productive while writing Java EE applications. With webinars though, it can be kind of hard to keep track of the code written and it’s hard to study and learn from it later. This will serve as the first part in a series teaching you how to be more productive with Java EE by using JRebel with your existing Java EE applications and tech stack.

Today’s post is about adding new HttpServlets to your application using the @WebServlet annotation. Traditionally you would have to restart your server to add a new servlet, but we’ve got JRebel, so why waste valuable time you could be using to do literally anything else besides waiting for your app to restart?

This code should work on any Java EE application server, but we’re going to stick with Red Hat’s WildFly 8.2, like we used in the webinar. If you already have a Java EE 6 or 7 application, that’s great and you can add code or you can start with an empty dynamic web project using the Servlet 3.1 spec like we did.

Here are links to the rest of the series:
Part 2: Creating new JPA entities
Part 3: CDI Bean Injection


When we start the application, it’s kind of a waste of RAM, it sits there and doesn’t do much, but we’ll fix that shortly! If we open http://localhost:8080/HelloJavaEE7/ in our browser we see the empty directory listing for our application.

The default page before adding the index.html, it looks sad and is just a directory listing

The default page before adding the index.html, it looks sad and is just a directory listing

Let’s make our application a little friendlier by serving a welcome index.html

The default page after adding a more friendly index.html to the application.

The default page after adding a more friendly index.html to the application.

Now we’re treating WildFly like an over-engineered simple webserver, so let’s make this more useful and use a proper Java EE Servlet. If we try to go to http://localhost:8080/HelloJavaEE7/HelloServlet/ in our browser right now, we get this sad Not Found error:

Not found error before the servlet's been added.

Not found error before the servlet’s been added.

Let’s add a HelloServlet, it extends the HttpServlet class and we’re going to annotate it with the @WebServlet annotation:

public class HelloServlet extends HttpServlet {
  protected void doGet(HttpServletRequest req, HttpServletResponse resp)
    throws ServletException, IOException {
      resp.getOutputStream().println("Hello from JRebel!");

Now that we’ve added the class, let’s try again.

Now we've got a fully functioning servlet, awesome

Now we’ve got a fully functioning servlet!

Awesome! Because we’ve got JRebel installed, we were able to add a new WebServlet and not have to restart our application. Great success all around and let’s go home early. Just think of all of the time that you’ll save by using JRebel… You could do anything, even take up creating Buddhist sand mandalas.

As you can see, JRebel let us make a fairly significant change to our Java EE application while the app remained up! No redeploys, and we were able to reload the code changes instantly. Now, that’s productive! Try JRebel for yourself in your app and see how you can save over one full work-month per year!



If you have any questions you can find me on twitter at @akoblentz or leave a comment below!