1 16 17 import java.io.IOException ; 18 import java.io.FileNotFoundException ; 19 import java.io.FileInputStream ; 20 21 import java.util.Properties ; 22 import java.util.Vector ; 23 24 import javax.servlet.ServletConfig ; 25 import javax.servlet.http.HttpServletRequest ; 26 import javax.servlet.http.HttpServletResponse ; 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 51 public class SampleServlet2 extends VelocityServlet 52 { 53 54 59 protected Properties loadConfiguration(ServletConfig config ) 60 throws IOException , FileNotFoundException 61 { 62 65 66 String propsFile = config.getInitParameter(INIT_PROPS_KEY); 67 68 Properties p = new Properties (); 69 70 if ( propsFile != null ) 71 { 72 String realPath = getServletContext().getRealPath(propsFile); 73 74 if ( realPath != null ) 75 { 76 propsFile = realPath; 77 } 78 79 p.load( new FileInputStream (propsFile) ); 80 } 81 82 86 87 String 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 104 105 String 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 132 public Template handleRequest( HttpServletRequest request, 133 HttpServletResponse response, Context ctx ) 134 { 135 138 139 String p1 = "Bob"; 140 String p2 = "Harold"; 141 142 Vector personList = new Vector (); 143 personList.addElement( p1 ); 144 personList.addElement( p2 ); 145 146 150 151 ctx.put("theList", personList ); 152 153 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 e ) 173 { 174 System.out.println("Error " + e); 175 } 176 return outty; 177 } 178 } 179 180 181 182 183 | Popular Tags |