Section 6.1 Implementing Handlers

The following code implements an anonymous hello handler and starts a jetty server with it:

  Handler handler=new AbstractHandler()

{

public void handle(String target, HttpServletRequest request, HttpServletResponse response,
 int dispatch) 

throws IOException, ServletException

{

response.setContentType("text/html");

response.setStatus(HttpServletResponse.SC_OK);

response.getWriter().println("%<h1>Hello</h1>%");((Request)request).
setHandled(true);

 }

}

}

Server server = new Server(8080);

server.setHandler(handler);

 server.start(); 

  • Currently 3.05/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
  Flag Inappropriate Content 0 comments