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 com.acme;
20  
21  import java.io.IOException;
22  
23  import javax.servlet.ServletConfig;
24  import javax.servlet.ServletException;
25  import javax.servlet.ServletOutputStream;
26  import javax.servlet.http.HttpServlet;
27  import javax.servlet.http.HttpServletRequest;
28  import javax.servlet.http.HttpServletResponse;
29  
30  
31  /* ------------------------------------------------------------ */
32  /** Dump Servlet Request.
33   * 
34   */
35  public class LoginServlet extends HttpServlet
36  {
37      /* ------------------------------------------------------------ */
38      @Override
39      public void init(ServletConfig config) throws ServletException
40      {
41          super.init(config);
42      }
43  
44      /* ------------------------------------------------------------ */
45      @Override
46      public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
47      {
48          doGet(request, response);
49      }
50  
51      /* ------------------------------------------------------------ */
52      @Override
53      public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
54      {
55          response.setContentType("text/html");
56          ServletOutputStream out = response.getOutputStream();
57          out.println("<html>");
58          out.println("<br/>Before getUserPrincipal="+request.getUserPrincipal());
59          out.println("<br/>Before getRemoteUser="+request.getRemoteUser());
60          String param = request.getParameter("action");
61  
62          if ("login".equals(param))
63          {
64                request.login("jetty", "jetty");
65          }
66          else if ("logout".equals(param))
67          {
68               request.logout();
69          }
70          else if ("wrong".equals(param))
71          {
72               request.login("jetty", "123");
73          }  
74  
75          out.println("<br/>After getUserPrincipal="+request.getUserPrincipal());
76          out.println("<br/>After getRemoteUser="+request.getRemoteUser());
77          out.println("</html>");
78          out.flush();
79      }
80  }