From 22f5de9ee605ec60eefd36d1117b854e375a2d8e Mon Sep 17 00:00:00 2001
From: Ulrich <undisclosed>
Date: Fri, 03 Mar 2017 15:51:53 +0000
Subject: [PATCH] ViewServlet gebaut

---
 src/de/uhilger/wbx/web/ViewServlet.java |  105 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 105 insertions(+), 0 deletions(-)

diff --git a/src/de/uhilger/wbx/web/ViewServlet.java b/src/de/uhilger/wbx/web/ViewServlet.java
new file mode 100644
index 0000000..aa0d1f1
--- /dev/null
+++ b/src/de/uhilger/wbx/web/ViewServlet.java
@@ -0,0 +1,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>
+
+}

--
Gitblit v1.9.3