View Javadoc

1   /*
2    * Created on 2004/10/23
3    */
4   package org.ieee.shinobu.demo.velocity.steve;
5   
6   import java.io.FileNotFoundException;
7   import java.io.IOException;
8   import java.util.Properties;
9   
10  import javax.servlet.ServletConfig;
11  import javax.servlet.http.HttpServletRequest;
12  import javax.servlet.http.HttpServletResponse;
13  
14  import org.apache.velocity.Template;
15  import org.apache.velocity.context.Context;
16  import org.apache.velocity.exception.ParseErrorException;
17  import org.apache.velocity.exception.ResourceNotFoundException;
18  import org.apache.velocity.servlet.VelocityServlet;
19  
20  /***
21   * @author shinobu
22   */
23  public class SteveVelocityServlet extends VelocityServlet {
24  
25  	private static final String ERR_HEADER = "ERR_HEADER";
26  
27  	protected Properties loadConfiguration(ServletConfig objConfig)
28  			throws IOException, FileNotFoundException {
29  		Properties objProps = new Properties();
30  		String sPath = objConfig.getServletContext().getRealPath("/");
31  		if (sPath == null) {
32  			System.out.println(ERR_HEADER
33  					+ "Unable to get the current webapp root");
34  			sPath = "/";
35  		}
36  		objProps.setProperty("file.resource.loader.path", sPath + "/velocity/steve/templates");
37  //		objProps.setProperty("file.resource.loader.cache", "false");
38  //
39  //		objProps.setProperty("file.resource.loader.modificationCheckInterval",
40  //				"5");
41  		objProps.setProperty("runtime.log", sPath + "/velocity/steve/log/corrman.log");
42  		return objProps;
43  	}
44  
45  	public Template handleRequest(HttpServletRequest objRequest,
46  			HttpServletResponse objResponse, Context objContext) {
47  
48  		objContext.put("Title", "Hello Steve");
49  
50  		Template objOutput = null;
51  		try {
52  			objOutput = getTemplate("test.vm");
53  		} catch (ParseErrorException objParseErr) {
54  			System.out.println(ERR_HEADER + "Parse error for template "
55  					+ objParseErr.getMessage());
56  		} catch (ResourceNotFoundException objResErr) {
57  			System.out.println(ERR_HEADER + "Template not found "
58  					+ objResErr.getMessage());
59  		} catch (Exception e) {
60  			System.out.println(ERR_HEADER + e.getMessage());
61  		}
62  		return objOutput;
63  	}
64  }