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 GET 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("Testausgabe"); super.doGet(request, response); out.print(""); */ String userName = getUserName(request); String requestURLStr = request.getRequestURL().toString(); String contextPath = request.getContextPath(); ServletOutputStream out = response.getOutputStream(); out.print("Testausgabe"); 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(""); } 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("

start: " + start + "

"); out.print("

userName.length: " + userName.length() + "

"); out.print("

contextPath.length(): " + contextPath.length() + "

"); out.print("

requestUrlStr.length(): " + requestUrlStr.length() + "

"); start += contextPath.length(); start++; out.print("

start: " + start + "

"); 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("

"); buf.append(label); buf.append(": "); if(theVal == null) { buf.append("null"); } else { buf.append(theVal); } buf.append("

"); 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"; }// }