1 22 package org.jboss.ejb3.test.dd.web.servlets; 23 24 import java.io.IOException ; 25 import java.io.PrintWriter ; 26 import javax.servlet.ServletConfig ; 27 import javax.servlet.ServletException ; 28 import javax.servlet.http.HttpServlet ; 29 import javax.servlet.http.HttpServletRequest ; 30 import javax.servlet.http.HttpServletResponse ; 31 import javax.servlet.http.HttpSession ; 32 33 import org.jboss.ejb3.test.dd.web.util.Util; 34 35 41 public class APIServlet extends HttpServlet 42 { 43 protected void processRequest(HttpServletRequest request, HttpServletResponse response) 44 throws ServletException , IOException 45 { 46 response.setContentType("text/html"); 47 PrintWriter out = response.getWriter(); 48 out.println("<html>"); 49 out.println("<head><title>APIServlet</title></head><body><pre>"); 50 String op = request.getParameter("op"); 51 if( op.equals("testGetRealPath") ) 52 { 53 String realPath = testGetRealPath(); 54 out.println("testGetRealPath ok, realPath="+realPath+"\n"); 55 } 56 else if( op.equals("testSessionListener") ) 57 { 58 testSessionListener(request); 59 } 60 else 61 { 62 throw new ServletException ("Unknown operation called, op="+op); 63 } 64 out.println("</pre></body></html>"); 65 out.close(); 66 } 67 68 protected void doGet(HttpServletRequest request, HttpServletResponse response) 69 throws ServletException , IOException 70 { 71 processRequest(request, response); 72 } 73 74 protected void doPost(HttpServletRequest request, HttpServletResponse response) 75 throws ServletException , IOException 76 { 77 processRequest(request, response); 78 } 79 80 private String testGetRealPath() 81 throws ServletException 82 { 83 String realPath = getServletContext().getRealPath("/"); 84 if( realPath == null ) 85 throw new ServletException ("getServletContext().getRealPath(/) returned null"); 86 return realPath; 87 } 88 89 private void testSessionListener(HttpServletRequest request) 90 throws ServletException 91 { 92 HttpSession session = request.getSession(true); 94 String sessionID = session.getId(); 95 boolean created = TestSessionListener.wasCreated(sessionID); 96 if( created == false ) 97 throw new ServletException ("No session create event seen"); 98 session.invalidate(); 100 boolean destroyed = TestSessionListener.wasDestroyed(sessionID); 101 if( destroyed == false ) 102 throw new ServletException ("No session destroy event seen"); 103 } 104 } 105 | Popular Tags |