WebBox Klassenbibliothek
Ulrich
2017-03-03 22f5de9ee605ec60eefd36d1117b854e375a2d8e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package de.uhilger.wbx.web;
 
import java.io.IOException;
import java.io.OutputStream;
import java.security.Principal;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.catalina.servlets.DefaultServlet;
 
/**
 *
 */
public class ViewServlet extends DefaultServlet {
  
  private static final Logger logger = Logger.getLogger(ViewServlet.class.getName());
 
  /**
   * Handles the HTTP <code>GET</code> method.
   *
   * @param request servlet request
   * @param response servlet response
   * @throws ServletException if a servlet-specific error occurs
   * @throws IOException if an I/O error occurs
   */
  @Override
  protected void doGet(HttpServletRequest request, HttpServletResponse response)
          throws ServletException, IOException {
    /*
            ServletOutputStream out = response.getOutputStream();
            out.print("<!DOCTYPE html><html><head><title>Testausgabe</title></head><body>");
            super.doGet(request, response);
            out.print("</body></html>");                
    */
 
      String userName = getUserName(request);
      String requestURLStr = request.getRequestURL().toString();
      String contextPath = request.getContextPath();
      
      ServletOutputStream out = response.getOutputStream();
      out.print("<!DOCTYPE html><html><head><title>Testausgabe</title></head><body>");
      out.print(getHtml("userName",  userName));      
      out.print(getHtml("contextPath",  contextPath));      
      out.print(getHtml("requestURL",  requestURLStr));      
      out.print(getHtml("urlUser",  getUrlUser(out, requestURLStr, contextPath, userName)));      
      out.print("</body></html>");
  }
  
  private String getUrlUser(ServletOutputStream out, String requestUrlStr, String contextPath, String userName) throws IOException {
    String result = "urlUser nicht ermittelbar";
    if(contextPath != null && requestUrlStr != null && userName != null) {
      int start = requestUrlStr.indexOf(contextPath);
      out.print("<p>start: " + start + "</p>");
      out.print("<p>userName.length: " + userName.length() + "</p>");
      out.print("<p>contextPath.length(): " + contextPath.length() + "</p>");
      out.print("<p>requestUrlStr.length(): " + requestUrlStr.length() + "</p>");
      start += contextPath.length();
      start++;
      out.print("<p>start: " + start + "</p>");
      int end = start + userName.length();
      try {
        result = requestUrlStr.substring(start, end);
      } catch(Exception ex) {
        result = ex.getMessage();
      }
    }
    return result;
  }
  
  private String getHtml(String label, String theVal) {
    StringBuffer buf = new StringBuffer();
    buf.append("<p>");
    buf.append(label);
    buf.append(": ");
    if(theVal == null) {
      buf.append("null");
    } else {
      buf.append(theVal);
    }
    buf.append("</p>");
    return buf.toString();
  }
 
  private String getUserName(HttpServletRequest hr) {
    String userName = null;
    Object p = hr.getUserPrincipal();
    if (p instanceof Principal) {
      userName = ((Principal) p).getName();
    }
    return userName;
  }
 
  /**
   * Returns a short description of the servlet.
   *
   * @return a String containing servlet description
   */
  @Override
  public String getServletInfo() {
    return "Short description";
  }// </editor-fold>
 
}