View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2014 Mort Bay Consulting Pty. Ltd.
4   //  ------------------------------------------------------------------------
5   //  All rights reserved. This program and the accompanying materials
6   //  are made available under the terms of the Eclipse Public License v1.0
7   //  and Apache License v2.0 which accompanies this distribution.
8   //
9   //      The Eclipse Public License is available at
10  //      http://www.eclipse.org/legal/epl-v10.html
11  //
12  //      The Apache License v2.0 is available at
13  //      http://www.opensource.org/licenses/apache2.0.php
14  //
15  //  You may elect to redistribute this code under either of these licenses.
16  //  ========================================================================
17  //
18  
19  package org.eclipse.jetty.embedded;
20  
21  import java.io.IOException;
22  
23  import javax.servlet.ServletException;
24  import javax.servlet.http.HttpServletRequest;
25  import javax.servlet.http.HttpServletResponse;
26  
27  import org.eclipse.jetty.server.Request;
28  import org.eclipse.jetty.server.handler.AbstractHandler;
29  
30  public class HelloHandler extends AbstractHandler
31  {
32      final String _greeting;
33      final String _body;
34      
35      public HelloHandler()
36      {
37          _greeting="Hello World";
38          _body=null;
39      }
40      
41      public HelloHandler(String greeting)
42      {
43          _greeting=greeting;
44          _body=null;
45      }
46      
47      public HelloHandler(String greeting,String body)
48      {
49          _greeting=greeting;
50          _body=body;
51      }
52      
53      public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
54      {
55          response.setContentType("text/html;charset=utf-8");
56          response.setStatus(HttpServletResponse.SC_OK);
57          baseRequest.setHandled(true);
58          
59          response.getWriter().println("<h1>"+_greeting+"</h1>");
60          if (_body!=null)
61              response.getWriter().println(_body);
62      }
63  }