Source Code Cross Referenced for SampleServlet.java in  » Testing » jakarta-cactus » org » apache » cactus » sample » servlet » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Testing » jakarta cactus » org.apache.cactus.sample.servlet 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /* 
002:         * ========================================================================
003:         * 
004:         * Copyright 2001-2004 The Apache Software Foundation.
005:         *
006:         * Licensed under the Apache License, Version 2.0 (the "License");
007:         * you may not use this file except in compliance with the License.
008:         * You may obtain a copy of the License at
009:         * 
010:         *   http://www.apache.org/licenses/LICENSE-2.0
011:         * 
012:         * Unless required by applicable law or agreed to in writing, software
013:         * distributed under the License is distributed on an "AS IS" BASIS,
014:         * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015:         * See the License for the specific language governing permissions and
016:         * limitations under the License.
017:         * 
018:         * ========================================================================
019:         */
020:        package org.apache.cactus.sample.servlet;
021:
022:        import java.io.IOException;
023:        import java.io.PrintWriter;
024:
025:        import java.util.Hashtable;
026:
027:        import javax.servlet.RequestDispatcher;
028:        import javax.servlet.ServletConfig;
029:        import javax.servlet.ServletException;
030:        import javax.servlet.http.Cookie;
031:        import javax.servlet.http.HttpServlet;
032:        import javax.servlet.http.HttpServletRequest;
033:        import javax.servlet.http.HttpServletResponse;
034:        import javax.servlet.http.HttpSession;
035:
036:        /**
037:         * Sample servlet that implement some very simple business logic. The goal is
038:         * to provide functional tests for Cactus, so we focus on providing as many
039:         * different test cases as possible rather than implementing a meaningful
040:         * business logic.
041:         *
042:         * @version $Id: SampleServlet.java 238835 2004-03-06 20:59:41Z vmassol $
043:         */
044:        public class SampleServlet extends HttpServlet {
045:            /**
046:             * Entry point for the servlet when a GET request is received. This will
047:             * be used to verify that we can test for the servlet output stream in
048:             * Cactus test cases.
049:             *
050:             * @param theRequest the HTTP request
051:             * @param theResponse the HTTP response
052:             * 
053:             * @exception IOException on failure
054:             */
055:            public void doGet(HttpServletRequest theRequest,
056:                    HttpServletResponse theResponse) throws IOException {
057:                PrintWriter pw = theResponse.getWriter();
058:
059:                theResponse.setContentType("text/html");
060:
061:                pw.print("<html><head/><body>");
062:                pw.print("A GET request");
063:                pw.print("</body></html>");
064:            }
065:
066:            /**
067:             * Return the method used to send data from the client (POST or GET). This
068:             * will be used to verify that we can simulate POST or GET methods from
069:             * Cactus. This simulates a method which would test the method to
070:             * implement it's business logic.
071:             *
072:             * @param theRequest the HTTP request
073:             * @return the method used to post data
074:             */
075:            public String checkMethod(HttpServletRequest theRequest) {
076:                return theRequest.getMethod();
077:            }
078:
079:            /**
080:             * Set some variable in the HTTP session. It verifies that a session object
081:             * has automatically been created by Cactus prior to calling this method.
082:             *
083:             * @param theRequest the HTTP request
084:             */
085:            public void setSessionVariable(HttpServletRequest theRequest) {
086:                HttpSession session = theRequest.getSession(false);
087:
088:                session.setAttribute("name_setSessionVariable",
089:                        "value_setSessionVariable");
090:            }
091:
092:            /**
093:             * Set some attribute in the request.
094:             *
095:             * @param theRequest the HTTP request
096:             */
097:            public void setRequestAttribute(HttpServletRequest theRequest) {
098:                theRequest.setAttribute("name_setRequestAttribute",
099:                        "value_setRequestAttribute");
100:            }
101:
102:            /**
103:             * Get some parameters from the HTTP request.
104:             *
105:             * @param theRequest the HTTP request
106:             * @return a hashtable containing some parameters
107:             */
108:            public Hashtable getRequestParameters(HttpServletRequest theRequest) {
109:                Hashtable params = new Hashtable();
110:
111:                params.put("param1", theRequest.getParameter("param1"));
112:                params.put("param2", theRequest.getParameter("param2"));
113:
114:                return params;
115:            }
116:
117:            /**
118:             * Get a header from the request.
119:             *
120:             * @return a test request header
121:             * @param theRequest the HTTP request
122:             */
123:            public String getRequestHeader(HttpServletRequest theRequest) {
124:                return theRequest.getHeader("testheader");
125:            }
126:
127:            /**
128:             * @return the cookies sent in the HTTP request
129:             *
130:             * @param theRequest the HTTP request
131:             */
132:            public Hashtable getRequestCookies(HttpServletRequest theRequest) {
133:                Hashtable allCookies = new Hashtable();
134:
135:                Cookie[] cookies = theRequest.getCookies();
136:
137:                if (cookies != null) {
138:                    for (int i = 0; i < cookies.length; i++) {
139:                        Cookie cookie = cookies[i];
140:
141:                        allCookies.put(cookie.getName(), cookie.getValue());
142:                    }
143:                }
144:
145:                return allCookies;
146:            }
147:
148:            /**
149:             * Set a header in the HTTP response. This is to verify that Cactus tests
150:             * can assert the returned headers.
151:             *
152:             * @param theResponse the HTTP response
153:             */
154:            public void setResponseHeader(HttpServletResponse theResponse) {
155:                theResponse.setHeader("responseheader",
156:                        "this is a response header");
157:            }
158:
159:            /**
160:             * Set a cookie for sending back to the client. This is to verify that
161:             * it is possible with Cactus to assert the cookies returned to the client
162:             *
163:             * @param theResponse the HTTP response
164:             */
165:            public void setResponseCookie(HttpServletResponse theResponse) {
166:                Cookie cookie = new Cookie("responsecookie",
167:                        "this is a response cookie");
168:
169:                cookie.setDomain("jakarta.apache.org");
170:                theResponse.addCookie(cookie);
171:            }
172:
173:            /**
174:             * Use a <code>RequestDispatcher</code> to forward to a JSP page. This is
175:             * to verify that Cactus supports asserting the result, even in the case
176:             * of forwarding to another page.
177:             *
178:             * @param theRequest the HTTP request
179:             * @param theResponse the HTTP response
180:             * @param theConfig the servlet config object
181:             * 
182:             * @exception IOException on failure
183:             * @exception ServletException on failure
184:             */
185:            public void doForward(HttpServletRequest theRequest,
186:                    HttpServletResponse theResponse, ServletConfig theConfig)
187:                    throws IOException, ServletException {
188:                RequestDispatcher rd = theConfig.getServletContext()
189:                        .getRequestDispatcher("/test/test.jsp");
190:
191:                rd.forward(theRequest, theResponse);
192:            }
193:
194:            /**
195:             * Use a <code>RequestDispatcher</code> to include a JSP page. This is
196:             * to verify that Cactus supports asserting the result, even in the case
197:             * of including another page.
198:             *
199:             * @param theRequest the HTTP request
200:             * @param theResponse the HTTP response
201:             * @param theConfig the servlet config object
202:             * 
203:             * @exception IOException on failure
204:             * @exception ServletException on failure
205:             */
206:            public void doInclude(HttpServletRequest theRequest,
207:                    HttpServletResponse theResponse, ServletConfig theConfig)
208:                    throws IOException, ServletException {
209:                RequestDispatcher rd = theConfig.getServletContext()
210:                        .getRequestDispatcher("/test/test.jsp");
211:
212:                rd.include(theRequest, theResponse);
213:            }
214:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.