1 22 package org.jboss.test.web.servlets.ssl; 23 24 import java.io.IOException ; 25 import java.io.PrintWriter ; 26 import java.security.Principal ; 27 import javax.servlet.ServletConfig ; 28 import javax.servlet.ServletException ; 29 import javax.servlet.http.HttpServlet ; 30 import javax.servlet.http.HttpServletRequest ; 31 import javax.servlet.http.HttpServletResponse ; 32 import javax.servlet.http.HttpSession ; 33 34 40 public class SecureServlet extends HttpServlet 41 { 42 private String expectedPrincipalName = null; 43 44 public void init(ServletConfig config) 45 throws ServletException 46 { 47 super.init(config); 48 expectedPrincipalName = config.getInitParameter("expectedPrincipalName"); 49 } 50 51 protected void processRequest(HttpServletRequest request, HttpServletResponse response) 52 throws ServletException , IOException 53 { 54 if( request.isSecure() == false ) 56 throw new ServletException ("Expected a secure connection"); 57 58 Principal user = request.getUserPrincipal(); 60 if( expectedPrincipalName != null ) 61 { 62 63 } 64 65 HttpSession session = request.getSession(false); 66 response.setContentType("text/html"); 67 PrintWriter out = response.getWriter(); 68 out.println("<html>"); 69 out.println("<head><title>SecureServlet</title></head>"); 70 out.println("<h1>SecureServlet Accessed</h1>"); 71 out.println("<body>"); 72 out.println("You have accessed this servlet as user:"+user); 73 if( session != null ) 74 out.println("<br>The session id is: "+session.getId()); 75 else 76 out.println("<br>There is no session"); 77 out.println("</body></html>"); 78 out.close(); 79 } 80 81 protected void doGet(HttpServletRequest request, HttpServletResponse response) 82 throws ServletException , IOException 83 { 84 processRequest(request, response); 85 } 86 87 protected void doPost(HttpServletRequest request, HttpServletResponse response) 88 throws ServletException , IOException 89 { 90 processRequest(request, response); 91 } 92 93 } 94 | Popular Tags |