1 Web.xml常用元素
2 <web-app>
3 <display-name></display-name>定义了WEB应用的名字
4 <description></description> 声明WEB应用的描述信息
5
6 <context-param></context-param> context-param元素声明应用范围内的初始化参数。
7 <filter></filter> 过滤器元素将一个名字与一个实现javax.servlet.Filter接口的类相关联。
8 <filter-mapping></filter-mapping> 一旦命名了一个过滤器,就要利用filter-mapping元素把它与一个或多个servlet或JSP页面相关联。
9 <listener></listener>servlet API的版本2.3增加了对事件监听程序的支持,事件监听程序在建立、修改和删除会话或servlet环境时得到通知。
10 Listener元素指出事件监听程序类。
11 <servlet></servlet> 在向servlet或JSP页面制定初始化参数或定制URL时,必须首先命名servlet或JSP页面。Servlet元素就是用来完成此项任务的。
12 <servlet-mapping></servlet-mapping> 服务器一般为servlet提供一个缺省的URL:http://host/webAppPrefix/servlet/ServletName。
13 但是,常常会更改这个URL,以便servlet可以访问初始化参数或更容易地处理相对URL。在更改缺省URL时,使用servlet-mapping元素。
14
15 <session-config></session-config> 如果某个会话在一定时间内未被访问,服务器可以抛弃它以节省内存。
16 可通过使用HttpSession的setMaxInactiveInterval方法明确设置单个会话对象的超时值,或者可利用session-config元素制定缺省超时值。
17
18 <mime-mapping></mime-mapping>如果Web应用具有想到特殊的文件,希望能保证给他们分配特定的MIME类型,则mime-mapping元素提供这种保证。
19 <welcome-file-list></welcome-file-list> 指示服务器在收到引用一个目录名而不是文件名的URL时,使用哪个文件。
20 <error-page></error-page> 在返回特定HTTP状态代码时,或者特定类型的异常被抛出时,能够制定将要显示的页面。
21 <taglib></taglib> 对标记库描述符文件(Tag Libraryu Descriptor file)指定别名。此功能使你能够更改TLD文件的位置,
22 而不用编辑使用这些文件的JSP页面。
23 <resource-env-ref></resource-env-ref>声明与资源相关的一个管理对象。
24 <resource-ref></resource-ref> 声明一个资源工厂使用的外部资源。
25 <security-constraint></security-constraint> 制定应该保护的URL。它与login-config元素联合使用
26 <login-config></login-config> 指定服务器应该怎样给试图访问受保护页面的用户授权。它与sercurity-constraint元素联合使用。
27 <security-role></security-role>给出安全角色的一个列表,这些角色将出现在servlet元素内的security-role-ref元素
28 的role-name子元素中。分别地声明角色可使高级IDE处理安全信息更为容易。
29 <env-entry></env-entry>声明Web应用的环境项。
30 <ejb-ref></ejb-ref>声明一个EJB的主目录的引用。
31 < ejb-local-ref></ ejb-local-ref>声明一个EJB的本地主目录的应用。
32 </web-app>
33
34
35 相应元素配置
36
37 1、Web应用图标:指出IDE和GUI工具用来表示Web应用的大图标和小图标
38 <icon>
39 <small-icon>/images/app_small.gif</small-icon>
40 <large-icon>/images/app_large.gif</large-icon>
41 </icon>
42 2、Web 应用名称:提供GUI工具可能会用来标记这个特定的Web应用的一个名称
43 <display-name>Tomcat Example</display-name>
44 3、Web 应用描述: 给出于此相关的说明性文本
45 <disciption>Tomcat Example servlets and JSP pages.</disciption>
46 4、上下文参数:声明应用范围内的初始化参数。
47 <context-param>
48 <param-name>ContextParameter</para-name>
49 <param-value>test</param-value>
50 <description>It is a test parameter.</description>
51 </context-param>
52 在servlet里面可以通过getServletContext().getInitParameter("context/param")得到
53
54 5、过滤器配置:将一个名字与一个实现javaxs.servlet.Filter接口的类相关联。
55 <filter>
56 <filter-name>setCharacterEncoding</filter-name>
57 <filter-class>com.myTest.setCharacterEncodingFilter</filter-class>
58 <init-param>
59 <param-name>encoding</param-name>
60 <param-value>GB2312</param-value>
61 </init-param>
62 </filter>
63 <filter-mapping>
64 <filter-name>setCharacterEncoding</filter-name>
65 <url-pattern>/*</url-pattern>
66 </filter-mapping>
67 6、监听器配置
68 <listener>
69 <listerner-class>listener.SessionListener</listener-class>
70 </listener>
71 7、Servlet配置
72 基本配置
73 <servlet>
74 <servlet-name>snoop</servlet-name>
75 <servlet-class>SnoopServlet</servlet-class>
76 </servlet>
77 <servlet-mapping>
78 <servlet-name>snoop</servlet-name>
79 <url-pattern>/snoop</url-pattern>
80 </servlet-mapping>
81 高级配置
82 <servlet>
83 <servlet-name>snoop</servlet-name>
84 <servlet-class>SnoopServlet</servlet-class>
85 <init-param>
86 <param-name>foo</param-name>
87 <param-value>bar</param-value>
88 </init-param>
89 <run-as>
90 <description>Security role for anonymous access</description>
91 <role-name>tomcat</role-name>
92 </run-as>
93 </servlet>
94 <servlet-mapping>
95 <servlet-name>snoop</servlet-name>
96 <url-pattern>/snoop</url-pattern>
97 </servlet-mapping>
98 元素说明
99 <servlet></servlet> 用来声明一个servlet的数据,主要有以下子元素:
100 <servlet-name></servlet-name> 指定servlet的名称
101 <servlet-class></servlet-class> 指定servlet的类名称
102 <jsp-file></jsp-file> 指定web站台中的某个JSP网页的完整路径
103 <init-param></init-param> 用来定义参数,可有多个init-param。在servlet类中通过getInitParamenter(String name)方法访问初始化参数
104 <load-on-startup></load-on-startup>指定当Web应用启动时,装载Servlet的次序。
105 当值为正数或零时:Servlet容器先加载数值小的servlet,再依次加载其他数值大的servlet.
106 当值为负或未定义:Servlet容器将在Web客户首次访问这个servlet时加载它
107 <servlet-mapping></servlet-mapping> 用来定义servlet所对应的URL,包含两个子元素
108 <servlet-name></servlet-name> 指定servlet的名称
109 <url-pattern></url-pattern> 指定servlet所对应的URL
110 8、会话超时配置(单位为分钟)
111 <session-config>
112 <session-timeout>120</session-timeout>
113 </session-config>
114 9、MIME类型配置
115 <mime-mapping>
116 <extension>htm</extension>
117 <mime-type>text/html</mime-type>
118 </mime-mapping>
119 10、指定欢迎文件页配置
120 <welcome-file-list>
121 <welcome-file>index.jsp</welcome-file>
122 <welcome-file>index.html</welcome-file>
123 <welcome-file>index.htm</welcome-file>
124 </welcome-file-list>
125 11、配置错误页面
126 一、 通过错误码来配置error-page
127 <error-page>
128 <error-code>404</error-code>
129 <location>/NotFound.jsp</location>
130 </error-page>
131 上面配置了当系统发生404错误时,跳转到错误处理页面NotFound.jsp。
132 二、通过异常的类型配置error-page
133 <error-page>
134 <exception-type>java.lang.NullException</exception-type>
135 <location>/error.jsp</location>
136 </error-page>
137 上面配置了当系统发生java.lang.NullException(即空指针异常)时,跳转到错误处理页面error.jsp
138 12、TLD配置
139 <taglib>
140 <taglib-uri>http://jakarta.apache.org/tomcat/debug-taglib</taglib-uri>
141 <taglib-location>/WEB-INF/jsp/debug-taglib.tld</taglib-location>
142 </taglib>
143 如果MyEclipse一直在报错,应该把<taglib> 放到 <jsp-config>中
144 <jsp-config>
145 <taglib>
146 <taglib-uri>http://jakarta.apache.org/tomcat/debug-taglib</taglib-uri>
147 <taglib-location>/WEB-INF/pager-taglib.tld</taglib-location>
148 </taglib>
149 </jsp-config>
150 13、资源管理对象配置
151 <resource-env-ref>
152 <resource-env-ref-name>jms/StockQueue</resource-env-ref-name>
153 </resource-env-ref>
154 14、资源工厂配置
155 <resource-ref>
156 <res-ref-name>mail/Session</res-ref-name>
157 <res-type>javax.mail.Session</res-type>
158 <res-auth>Container</res-auth>
159 </resource-ref>
160 配置数据库连接池就可在此配置:
161 <resource-ref>
162 <description>JNDI JDBC DataSource of shop</description>
163 <res-ref-name>jdbc/sample_db</res-ref-name>
164 <res-type>javax.sql.DataSource</res-type>
165 <res-auth>Container</res-auth>
166 </resource-ref>
167 15、安全限制配置
168 <security-constraint>
169 <display-name>Example Security Constraint</display-name>
170 <web-resource-collection>
171 <web-resource-name>Protected Area</web-resource-name>
172 <url-pattern>/jsp/security/protected/*</url-pattern>
173 <http-method>DELETE</http-method>
174 <http-method>GET</http-method>
175 <http-method>POST</http-method>
176 <http-method>PUT</http-method>
177 </web-resource-collection>
178 <auth-constraint>
179 <role-name>tomcat</role-name>
180 <role-name>role1</role-name>
181 </auth-constraint>
182 </security-constraint>
183 16、登陆验证配置
184 <login-config>
185 <auth-method>FORM</auth-method>
186 <realm-name>Example-Based Authentiation Area</realm-name>
187 <form-login-config>
188 <form-login-page>/jsp/security/protected/login.jsp</form-login-page>
189 <form-error-page>/jsp/security/protected/error.jsp</form-error-page>
190 </form-login-config>
191 </login-config>
192 17、安全角色:security-role元素给出安全角色的一个列表,这些角色将出现在servlet元素内的security-role-ref元素的role-name子元素中。
193 分别地声明角色可使高级IDE处理安全信息更为容易。
194 <security-role>
195 <role-name>tomcat</role-name>
196 </security-role>
197 18、Web环境参数:env-entry元素声明Web应用的环境项
198 <env-entry>
199 <env-entry-name>minExemptions</env-entry-name>
200 <env-entry-value>1</env-entry-value>
201 <env-entry-type>java.lang.Integer</env-entry-type>
202 </env-entry>
203 19、EJB 声明
204 <ejb-ref>
205 <description>Example EJB reference</decription>
206 <ejb-ref-name>ejb/Account</ejb-ref-name>
207 <ejb-ref-type>Entity</ejb-ref-type>
208 <home>com.mycompany.mypackage.AccountHome</home>
209 <remote>com.mycompany.mypackage.Account</remote>
210 </ejb-ref>
211 20、本地EJB声明
212 <ejb-local-ref>
213 <description>Example Loacal EJB reference</decription>
214 <ejb-ref-name>ejb/ProcessOrder</ejb-ref-name>
215 <ejb-ref-type>Session</ejb-ref-type>
216 <local-home>com.mycompany.mypackage.ProcessOrderHome</local-home>
217 <local>com.mycompany.mypackage.ProcessOrder</local>
218 </ejb-local-ref>
219 21、配置DWR
220 <servlet>
221 <servlet-name>dwr-invoker</servlet-name>
222 <servlet-class>uk.ltd.getahead.dwr.DWRServlet</servlet-class>
223 </servlet>
224 <servlet-mapping>
225 <servlet-name>dwr-invoker</servlet-name>
226 <url-pattern>/dwr/*</url-pattern>
227 </servlet-mapping>
228 22、配置Struts
229 <display-name>Struts Blank Application</display-name>
230 <servlet>
231 <servlet-name>action</servlet-name>
232 <servlet-class>
233 org.apache.struts.action.ActionServlet
234 </servlet-class>
235 <init-param>
236 <param-name>detail</param-name>
237 <param-value>2</param-value>
238 </init-param>
239 <init-param>
240 <param-name>debug</param-name>
241 <param-value>2</param-value>
242 </init-param>
243 <init-param>
244 <param-name>config</param-name>
245 <param-value>/WEB-INF/struts-config.xml</param-value>
246 </init-param>
247 <init-param>
248 <param-name>application</param-name>
249 <param-value>ApplicationResources</param-value>
250 </init-param>
251 <load-on-startup>2</load-on-startup>
252 </servlet>
253 <servlet-mapping>
254 <servlet-name>action</servlet-name>
255 <url-pattern>*.do</url-pattern>
256 </servlet-mapping>
257 <welcome-file-list>
258 <welcome-file>index.jsp</welcome-file>
259 </welcome-file-list>
260
261 <!-- Struts Tag Library Descriptors -->
262 <taglib>
263 <taglib-uri>struts-bean</taglib-uri>
264 <taglib-location>/WEB-INF/tld/struts-bean.tld</taglib-location>
265 </taglib>
266 <taglib>
267 <taglib-uri>struts-html</taglib-uri>
268 <taglib-location>/WEB-INF/tld/struts-html.tld</taglib-location>
269 </taglib>
270 <taglib>
271 <taglib-uri>struts-nested</taglib-uri>
272 <taglib-location>/WEB-INF/tld/struts-nested.tld</taglib-location>
273 </taglib>
274 <taglib>
275 <taglib-uri>struts-logic</taglib-uri>
276 <taglib-location>/WEB-INF/tld/struts-logic.tld</taglib-location>
277 </taglib>
278 <taglib>
279 <taglib-uri>struts-tiles</taglib-uri>
280 <taglib-location>/WEB-INF/tld/struts-tiles.tld</taglib-location>
281 </taglib>
282 23、配置Spring(基本上都是在Struts中配置的)
283
284 <!-- 指定spring配置文件位置 -->
285 <context-param>
286 <param-name>contextConfigLocation</param-name>
287 <param-value>
288 <!--加载多个spring配置文件 -->
289 /WEB-INF/applicationContext.xml, /WEB-INF/action-servlet.xml
290 </param-value>
291 </context-param>
292
293 <!-- 定义SPRING监听器,加载spring -->
294
295 <listener>
296 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
297 </listener>
298
299 <listener>
300 <listener-class>
301 org.springframework.web.context.request.RequestContextListener
302 </listener-class>
303 </listener>