1 16 17 import java.io.IOException ; 18 import java.io.PrintWriter ; 19 20 import javax.servlet.ServletContext ; 21 import javax.servlet.ServletException ; 22 import javax.servlet.http.HttpServlet ; 23 import javax.servlet.http.HttpServletRequest ; 24 import javax.servlet.http.HttpServletResponse ; 25 import javax.servlet.http.HttpSession ; 26 27 import org.apache.commons.jxpath.JXPathContext; 28 import org.apache.commons.jxpath.servlet.JXPathServletContexts; 29 30 35 public class JXPathServletContextsTestServlet extends HttpServlet { 36 37 public void doGet(HttpServletRequest request, HttpServletResponse response) 38 throws IOException , ServletException 39 { 40 ServletContext servletContext = getServletContext(); 41 servletContext.setAttribute("app", "OK"); 42 JXPathContext appContext = JXPathServletContexts 43 .getApplicationContext(servletContext); 44 45 request.setAttribute("attr", "OK"); 46 JXPathContext reqContext = JXPathServletContexts.getRequestContext( 47 request, 48 servletContext); 49 50 HttpSession session = request.getSession(); 51 Integer count = (Integer ) session.getAttribute("count"); 52 if (count == null) { 53 count = new Integer (0); 54 } 55 else { 56 count = new Integer (count.intValue() + 1); 57 } 58 session.setAttribute("count", count); 59 60 JXPathContext sessionContext = JXPathServletContexts.getSessionContext( 61 session, 62 servletContext); 63 64 response.setContentType("text/html"); 65 PrintWriter out = response.getWriter(); 66 out.println("<html>"); 67 out.println("<head>"); 68 out.println("<title>JXPathServletContext</title>"); 69 out.println("</head>"); 70 out.println("<body>"); 71 out.println("<h1>JXPathServletContexts Servlet Context Test</h1>"); 72 assertEqual( 73 out, 74 "Application Context", 75 appContext.getValue("app"), 76 "OK"); 77 assertEqual( 78 out, 79 "Request Context Attribute", 80 reqContext.getValue("attr"), 81 "OK"); 82 assertEqual( 83 out, 84 "Request Context Attribute", 85 reqContext.getValue("attr"), 86 "OK"); 87 88 if (request.getParameter("parm") == null) { 89 out.println("<p><b>Invoke this test servlet like this: " 90 + "http://localhost:8080/jxpath-war/jxpath?parm=OK<b>"); 91 } 92 else { 93 assertEqual( 94 out, 95 "Request Context Parameter", 96 reqContext.getValue("parm"), 97 "OK"); 98 } 99 assertEqual( 100 out, 101 "Session Context Parameter (reload for actual test)", 102 sessionContext.getValue("count"), 103 count); 104 assertEqual( 105 out, 106 "Application Context via Request Context", 107 reqContext.getValue("app"), 108 "OK"); 109 assertEqual( 110 out, 111 "Session Context via Request Context", 112 reqContext.getValue("count"), 113 count); 114 assertEqual( 115 out, 116 "Application Context via Session Context", 117 sessionContext.getValue("app"), 118 "OK"); 119 120 out.println("</body>"); 121 out.println("</html>"); 122 } 123 124 private void assertEqual( 125 PrintWriter out, 126 String title, 127 Object actual, 128 Object expected) 129 { 130 if ((actual == null && expected == null) 131 || (actual != null && actual.equals(expected))) { 132 out.println("<p>" + title + ": Ok"); 133 } 134 else { 135 out.println("<p><font color=red>" + title + ": Failure</font>"); 136 } 137 } 138 } 139 140 | Popular Tags |