KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > JXPathServletContextsTestServlet


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 import java.io.IOException JavaDoc;
18 import java.io.PrintWriter JavaDoc;
19
20 import javax.servlet.ServletContext JavaDoc;
21 import javax.servlet.ServletException JavaDoc;
22 import javax.servlet.http.HttpServlet JavaDoc;
23 import javax.servlet.http.HttpServletRequest JavaDoc;
24 import javax.servlet.http.HttpServletResponse JavaDoc;
25 import javax.servlet.http.HttpSession JavaDoc;
26
27 import org.apache.commons.jxpath.JXPathContext;
28 import org.apache.commons.jxpath.servlet.JXPathServletContexts;
29
30 /**
31  * Invoke like this: http://localhost:8080/jxpath?parm=OK
32  *
33  * @version $Revision: 1.1 $ $Date: 2004/05/08 15:10:49 $
34  */

35 public class JXPathServletContextsTestServlet extends HttpServlet JavaDoc {
36
37     public void doGet(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
38     throws IOException JavaDoc, ServletException JavaDoc
39     {
40         ServletContext JavaDoc 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 JavaDoc session = request.getSession();
51         Integer JavaDoc count = (Integer JavaDoc) session.getAttribute("count");
52         if (count == null) {
53             count = new Integer JavaDoc(0);
54         }
55         else {
56             count = new Integer JavaDoc(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 JavaDoc 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 JavaDoc out,
126             String JavaDoc title,
127             Object JavaDoc actual,
128             Object JavaDoc 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