1 package com.quadcap.http.server22; 2 3 40 41 import java.io.IOException ; 42 43 import java.util.Enumeration ; 44 import java.util.Properties ; 45 46 import javax.servlet.Servlet ; 47 import javax.servlet.ServletConfig ; 48 import javax.servlet.ServletContext ; 49 import javax.servlet.ServletException ; 50 import javax.servlet.ServletRequest ; 51 import javax.servlet.ServletResponse ; 52 import javax.servlet.SingleThreadModel ; 53 54 import com.quadcap.util.Debug; 55 56 63 public class WebServlet implements ServletConfig { 64 WebApplication app; 65 Servlet servlet = null; 66 boolean singleThreadModel = false; 67 String servletName; 68 String servletClass; 69 String jspFile; 70 int loadOnStartup = -1; 71 Properties initParams = new Properties (); 72 73 public WebServlet() { 74 } 75 76 public String toString() { 77 return servletName; 78 } 79 80 public void setServletName(String name) { 81 this.servletName = name; 82 } 83 84 public String getServletName() { 85 return servletName; 86 } 87 88 public void setServletClass(String name) { 89 this.servletClass = name; 90 } 91 92 public String getServletClass() { 93 return servletClass; 94 } 95 96 public void setJspFile(String name) { 97 this.jspFile = name; 98 } 99 100 public String getJspFile() { 101 return jspFile; 102 } 103 104 public void setLoadOnStartup(int loadOnStartup) { 105 this.loadOnStartup = loadOnStartup; 106 } 107 108 public int getLoadOnStartup() { 109 return loadOnStartup; 110 } 111 112 public void addInitParam(String prop, String val) { 113 initParams.setProperty(prop, val); 114 } 115 116 public Enumeration getInitParameterNames() { 117 return initParams.keys(); 118 } 119 120 public String getInitParameter(String name) { 121 return initParams.getProperty(name); 122 } 123 124 public void setWebApplication(WebApplication app) { 125 this.app = app; 126 } 127 128 public WebApplication getWebApplication() { 129 return app; 130 } 131 132 public ServletContext getServletContext() { 133 return app; 134 } 135 136 140 144 final void init() throws ServletException { 145 synchronized (this) { 146 if (servlet == null) { 147 try { 148 Class c = app.getClassLoader().loadClass(servletClass); 149 Servlet s = (Servlet )c.newInstance(); 150 singleThreadModel = (s instanceof SingleThreadModel ); 151 s.init(this); 152 servlet = s; 153 } catch (ServletException e) { 154 throw e; 155 } catch (Throwable t) { 156 Debug.print(t); 157 throw new ServletException (t.toString()); 158 } 159 } 160 } 161 } 162 163 public void service(ServletRequest req, ServletResponse res) 164 throws ServletException , IOException 165 { 166 if (servlet == null) init(); 167 if (singleThreadModel) { 168 synchronized (servlet) { 169 servlet.service(req, res); 170 } 171 } else { 172 servlet.service(req, res); 173 } 174 } 175 } 176 | Popular Tags |