一.Servlet的配置信息
Servlet要运行,必须在web.xml中配置。主要配置信息有:
<servlet-name>:指定servlet实例的名字,可以是任何合法标识符,建议使用Servlet类名。
<servlet-class>:指定servlet类的完整名字,包括包名和类名。
<init-param>:为servlet指定初始化参数,使用ServletConfig接口中的getInitParameter方法可以获取。
<load-on-startup>:该选现配置非负整数,使得在应用加载该servlet实例即被初始化,而不是第一次访问时才初始化。数值表示初始化的顺序,而非实例的个数,servlet是单实例的,永远只有一个实例。
<url-pattern>:配置servlet的访问路径,必须以/开头,如果带扩展名,如*.do,不以/开头。
Servlet配置的实例
<servlet>
<servlet-name>LoginServlet</servlet-name>
<servlet-class>com.etc.servlet.LoginServlet</servlet-class>
<init-param>
<param-name>validate</param-name>
<param-value>true</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>LoginServlet</servlet-name>
<url-pattern>/login</url-pattern>
</servlet-mapping>
二.过滤器配置
开发过滤器后,必须在web.xml中进行配置,方能生效,主要配置信息有:<filter-name>:指定filter实例名字,可以是任意合法的标识符,建议使用filter类名。
<filter-class>:指定filter类名,必须是包括包名在内的完整类名。
<init-param>:为filter指定初始化参数,可以通过FilterConfig中的getInitParameter方法获取。
<url-pattern>:指定该过滤器过滤的url。
<dispatcher>:指定该过滤器生效的分发方式,共4种,默认是REQUEST方式。
过滤器配置实例
<filter><filter-name>LoginFilter</filter-name>
<filter-class>com.etc.filter.LoginFilter</filter-class>
<init-param>
<param-name>startTime</param-name>
<param-value>4</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>LoginFilter</filter-name>
<url-pattern>*.jsp</url-pattern>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
<dispatcher>INCLUDE</dispatcher>
<dispatcher>REQUEST</dispatcher>
</filter-mapping>
三.上下文参数
上下文参数被封装在ServletContext对象中,可以通过其中的getInitParameter方法获取。上下文参数可以在整个应用中使用。
<context-param>
<param-name>path</param-name>
<param-value>/WEB-INF/tld</param-value>
</context-param>
四.会话超时时间
容器默认的会话超时时间往往是30分钟,可以通过<session-config>配置该应用的会话超时时间,单位为分钟。<session-config>
<session-timeout>40</session-timeout>
</session-config>
五.欢迎页面列表
默认的欢迎页面是index.jsp或者index.html,可以通过<welcome-file-list>指定多个欢迎页面,容器将按照配置顺序查找。
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
<welcome-file>login.jsp</welcome-file>
<welcome-file>main.jsp</welcome-file>
</welcome-file-list>
六.错误页面配置
通过<error-page>可以指定应用中发生某种响应错误,如404错误,跳转的页面。也可以配置发生某种异常,异常没有被捕获时,跳转的页面。
<error-page>
<error-code>404</error-code>
<location>/error/404.html</location>
</error-page>
<error-page>
<exception-type>com.etc.exception.RegisterExcetpion</exception-type>
<location>/register.jsp</location>
</error-page>
七.JSP配置实例
<jsp-config>
<jsp-property-group>
<url-pattern>/admin</url-pattern>
<el-ignored>false</el-ignored>
<scripting-invalid>false</scripting-invalid>
</jsp-property-group>
<jsp-property-group>
<url-pattern>/admin</url-pattern>
<page-encoding>utf-8</page-encoding>
</jsp-property-group>
</jsp-config>
八.数据源配置
使用容器提供的连接池,可以在web.xml中配置数据源<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/meeting</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
<res-ref-name>指定的名字是在程序中查找数据源对象时使用的名字。