tags:

views:

440

answers:

6

For the last couple of years I've had my head in Python, where there are numerous choices for simple, minimal frameworks that allow me to stand up a website or service easily (eg. web.py). I'm looking for something similar in Java.

What is the simplest, least-moving-parts way of standing up simple services using Java these days? I'm looking for something as simple as:

  • the ability to receive HTTP requests
  • the ability to dispatch those requests to handlers (preferably a regular expression based url to handler mapping facility)
  • the ability to set HTTP headers and generally fully control the request/response

Bonus points if the framework plays well with Jython.

[Update] Thanks for the responses, some of these look quite interesting. I'm not seeing the url dispatch capability in these, however. I'm looking for something similar to Django's url.py system, which looks like:

urlpatterns = patterns('',
    (r'^articles/2003/$', 'news.views.special_case_2003'),
    (r'^articles/(\d{4})/$', 'news.views.year_archive'),
    (r'^articles/(\d{4})/(\d{2})/$', 'news.views.month_archive'),
    (r'^articles/(\d{4})/(\d{2})/(\d+)/$', 'news.views.article_detail'),
)

Where you specify a url regular expression along with the handler that handles it.

+2  A: 

I liked to worth the Simple HTTP Server from the Simple Framework. It offers a nice Tutorial about how to start as well.

Daff
+1 Simple is also the fastest NIO server container for Java. Also, the smallest footprint with no external dependencies other than Java 5.
ng
+2  A: 

there are several alternatives:

  • servlets
  • restlet: lightweight REST framework
  • jax-rs: using jersey or the restlet module implementing the jax-rs specs
  • grizzly: NIO based server (with HTTP support + handlers)
  • apache mina: event-driven, async server (with HTTP support)

all these frameworks come with a built-in server.

EDIT

jax-rs has a similar approach using url templates:

@Path("/users/{username}")
public class UserResource {

    @GET
    @Produces("text/xml")
    public String getUser(@PathParam("username") String userName) {   
    }
}

then put your handlers in an Application object:

public class MyApplicaton extends Application {
    public Set<Class> getClasses() {
        Set<Class> s = new HashSet<Class>();
        s.add(UserResource.class);
        return s;
    }
}

another example with JAX-RS:

@GET
@Produces("application/json")
@Path("/network/{id: [0-9]+}/{nid}")
public User getUserByNID(@PathParam("id") int id, @PathParam("nid") String nid) {
}

EDIT 2

Restlet supports a centralized configurations like Django, in your Application object:

// Attach the handlers to the root router  
router.attach("/users/{user}", account);  
router.attach("/users/{user}/orders", orders);  
router.attach("/users/{user}/orders/{order}", order);
dfa
+1  A: 

Servlets might be the way to go. To do very simple things you only need to override one method of one class. More complicated stuff is of course possible, but you can go a long way with a little work.

Investigate Tomcat or Jetty - both are open source and well supported.

public class HelloWorldServlet extends HttpServlet {
    public void doGet( HttpServletRequest request, HttpServletResponse response )
        throws ServletException, IOException 
    {
        response.setContentType( "text/plain" );
        PrintWriter out = response.getWriter();
        out.print( "hello world!" );
    }
}
banjollity
+1  A: 

Note: This is more general discussion than answer.

I'm having similar issues coming from Python for 10+ years and diving, as it were, back into Java. I think one thing I'm learning is that the "simplicity" factor of Python is very different from that of Java. Where Python abounds with high-level framework-- things like web.py, Java seems much more lower level. Over the past few months, I've gone from saying "What's the Java way to do this easy in Python thing" to "How does one build up this thing in Java." Subtle, but seems to bring my thoughts around from a Python-centric view to a more Java-centric one.

Having done that, I've realized that standing up a website or service is not simple for a Java outsider, that's because there's a large amount of info I have to (re)grok. It's not as simple as python. You still need a webserver, you need to build a "container" to drop your Java code into, and then you need the Java code (am I wrong on this, everyone? Is there a simpler way?).

For me, working with Scala and Lift has helped- and not even those, but this one thread by David Pollack. This was what I needed to build a Jetty server. Take that, follow the directions (somewhat vague, but might be good enough for you) and then you have a servlet container ready to accept incoming traffic on a port (or 3 ports, in his case). Then you can write some Java code using HTTPServlet or something to go the rest of the way.

Again, this is just what I did to get past that barrier, but I'm still not a Java guru. Good luck.

JohnMetta
A: 

I've hard about: Apache Mina

But quite frankly I don't even know if it is what you need.

:-/

:)

OscarRyz
A: 

Jetty is a pretty nice embedded http server - even if it isn't possible to do the mapping like you describe, it should be pretty easy to implement what you are going for.

Kevin Day