View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2016 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  import java.io.PrintWriter;
23  
24  import javax.servlet.ServletException;
25  import javax.servlet.http.HttpServletRequest;
26  import javax.servlet.http.HttpServletResponse;
27  
28  import org.eclipse.jetty.server.Request;
29  import org.eclipse.jetty.server.handler.AbstractHandler;
30  
31  public class HelloHandler extends AbstractHandler
32  {
33      final String greeting;
34      final String body;
35  
36      public HelloHandler()
37      {
38          this("Hello World");
39      }
40  
41      public HelloHandler( String greeting )
42      {
43          this(greeting, null);
44      }
45  
46      public HelloHandler( String greeting, String body )
47      {
48          this.greeting = greeting;
49          this.body = body;
50      }
51  
52      public void handle( String target,
53                          Request baseRequest,
54                          HttpServletRequest request,
55                          HttpServletResponse response ) throws IOException,
56                                                        ServletException
57      {
58          response.setContentType("text/html; charset=utf-8");
59          response.setStatus(HttpServletResponse.SC_OK);
60  
61          PrintWriter out = response.getWriter();
62  
63          out.println("<h1>" + greeting + "</h1>");
64          if (body != null)
65          {
66              out.println(body);
67          }
68  
69          baseRequest.setHandled(true);
70      }
71  }