KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > SampleServlet2


1 /*
2  * Copyright 2000-2001,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.FileNotFoundException JavaDoc;
19 import java.io.FileInputStream JavaDoc;
20
21 import java.util.Properties JavaDoc;
22 import java.util.Vector JavaDoc;
23
24 import javax.servlet.ServletConfig JavaDoc;
25 import javax.servlet.http.HttpServletRequest JavaDoc;
26 import javax.servlet.http.HttpServletResponse JavaDoc;
27
28 import org.apache.velocity.Template;
29 import org.apache.velocity.context.Context;
30 import org.apache.velocity.servlet.VelocityServlet;
31 import org.apache.velocity.app.Velocity;
32 import org.apache.velocity.exception.ResourceNotFoundException;
33 import org.apache.velocity.exception.ParseErrorException;
34
35 /**
36  * Sample of how to use the VelocityServlet. This example
37  * is intended to show how to use an external properties
38  * file. Note that :
39  * <ul>
40  * <li> It assumes that the path to the velocity log
41  * is relative to the webapp root
42  * <li> If specified, it assumes that the path for the
43  * file resource loader is single, and relative to
44  * the webapp root
45  * </ul>
46  *
47  * @author Dave Bryson
48  * @author <a HREF="mailto:geirm@optonline.net">Geir Magnusson Jr.</a>
49  * @version $Id: SampleServlet2.java,v 1.3.8.1 2004/03/04 00:18:29 geirm Exp $
50  */

51 public class SampleServlet2 extends VelocityServlet
52 {
53
54     /**
55      * A fancier version of loadConfiguration(), this will
56      * set the log file to be off of the webapp root, and
57      * will do the same with the file loader paths
58      */

59    protected Properties JavaDoc loadConfiguration(ServletConfig JavaDoc config )
60         throws IOException JavaDoc, FileNotFoundException JavaDoc
61     {
62         /*
63          * get our properties file and load it
64          */

65
66         String JavaDoc propsFile = config.getInitParameter(INIT_PROPS_KEY);
67         
68         Properties JavaDoc p = new Properties JavaDoc();
69         
70         if ( propsFile != null )
71         {
72             String JavaDoc realPath = getServletContext().getRealPath(propsFile);
73         
74             if ( realPath != null )
75             {
76                 propsFile = realPath;
77             }
78
79             p.load( new FileInputStream JavaDoc(propsFile) );
80         }
81
82         /*
83          * first, normalize our velocity log file to be in the
84          * webapp
85          */

86
87         String JavaDoc log = p.getProperty( Velocity.RUNTIME_LOG);
88
89         if (log != null )
90         {
91             log = getServletContext().getRealPath( log );
92             
93             if (log != null)
94             {
95                 p.setProperty( Velocity.RUNTIME_LOG, log );
96             }
97         }
98
99        
100         /*
101          * now, if there is a file loader resource path, treat it the
102          * same way.
103          */

104
105         String JavaDoc path = p.getProperty( Velocity.FILE_RESOURCE_LOADER_PATH );
106
107         if ( path != null)
108         {
109             path = getServletContext().getRealPath( path );
110
111             if ( path != null)
112             {
113                 p.setProperty( Velocity.FILE_RESOURCE_LOADER_PATH, path );
114             }
115         }
116
117         return p;
118     }
119  
120     /**
121      * <p>
122      * main routine to handle a request. Called by
123      * VelocityServlet, your responsibility as programmer
124      * is to simply return a valid Template
125      * </p>
126      *
127      * @param ctx a Velocity Context object to be filled with
128      * data. Will be used for rendering this
129      * template
130      * @return Template to be used for request
131      */

132     public Template handleRequest( HttpServletRequest JavaDoc request,
133     HttpServletResponse JavaDoc response, Context ctx )
134     {
135         /*
136          * set up some data to put into the context
137          */

138
139         String JavaDoc p1 = "Bob";
140         String JavaDoc p2 = "Harold";
141         
142         Vector JavaDoc personList = new Vector JavaDoc();
143         personList.addElement( p1 );
144         personList.addElement( p2 );
145
146         /*
147          * Add the list to the context.
148          * This is how it's passed to the template.
149          */

150
151         ctx.put("theList", personList );
152         
153         /*
154          * get the template. There are three possible
155          * exceptions. Good to know what happened.
156          */

157
158         Template outty = null;
159         
160         try
161         {
162             outty = getTemplate("sample.vm");
163         }
164         catch( ParseErrorException pee )
165         {
166             System.out.println("SampleServlet : parse error for template " + pee);
167         }
168         catch( ResourceNotFoundException rnfe )
169         {
170             System.out.println("SampleServlet : template not found " + rnfe);
171         }
172         catch( Exception JavaDoc e )
173         {
174             System.out.println("Error " + e);
175         }
176         return outty;
177     }
178 }
179
180
181
182
183
Popular Tags