java中默认的一个web项目中会有两个xml文件,一个是sys-config.xml,位于src跟目录下;一个是web层的WEB-INF下的web.xml。
两个配置文件有什么区别?
故名思议:sys-config.xml用于配置系统中的基本参数,通常将数据库访问这些数据放入到这里。另外,还可以在src目录下,建立一个新的xml文件,用于javaBean的反射。如,新建一个beanConfig.xml文件配合,工厂+配置文件+反射的设计模式解耦。
web.xml文件则是记录一些application、session、filter、servlet、error等这类的配置。下面是两类配置文件的示例:
sys-config.xml:
<?xml version="1.0" encoding="UTF-8"?>
<config>
<db-info>
<driver-name>oracle.jdbc.driver.OracleDriver</driver-name>
<url>jdbc:oracle:thin:@localhost:1521:bjpowern</url>
<user-name>drp1</user-name>
<password>drp1</password>
</db-info>
</config>
beanConfig.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans>
<service-class>
<service id="com.bjpowernode.drp.basedata.manager.ItemManager" class="com.bjpowernode.drp.basedata.manager.ItemManagerImpl"/>
</service-class>
<dao-class>
<dao id="com.bjpowernode.drp.basedata.dao.ItemDao" class="com.bjpowernode.drp.basedata.dao.ItemDao4OracleImpl" />
</dao-class>
</beans>
web.xml:
所有的节点都在这里配置:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
.........
</web-app>
1、配置servlet:因为涉及到jsp的跳转访问,所以有<servlet-mapping>节点
<servlet>
<servlet-name>ShowItemUploadServlet</servlet-name>
<servlet-class>com.bjpowernode.drp.basedata.web.ShowItemUploadServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ShowItemUploadServlet</servlet-name>
<url-pattern>/servlet/item/ShowItemUploadServlet</url-pattern>
</servlet-mapping>
所有的<servlet>节点的作用是将servlet-name和sevlet-class包装关联;<servlet-mapping>节点的作用是将<servlet-name>和<url-pattern>关联,一个完整的servlet是通过<servlet-name>将<sevlet-class>和<url-pattern>映射。
<url-pattern>可以设置虚拟路径,比如这里的/servlet/item就是为了给系统划分模块,人为添加的,访问的时候需要与之匹配。
另外,我们可以在servlet中配置一些参数,在该servlet初始化的时候,来访问这些数据。如,可以这样配置一个带有默认page-size的关于分页的servlet。这个参数的作用范围只限于该servlet。
<servlet>
<servlet-name>SearchItemServlet</servlet-name>
<servlet-class>com.bjpowernode.drp.basedata.web.SearchItemServlet</servlet-class>
<init-param>
<param-name>page-size</param-name>
<param-value>3</param-value>
</init-param>
</servlet>
在SearchItemServlet.java中,可以这样来访问:
int pageSize = Integer.parseInt(this.getServletConfig().getInitParameter("page-size"));
在tomcat启动的时候,能否控制servlet的初始化顺序呢?答案是:通过<load-on-startup>节点。
<servlet>
<servlet-name>FileUploadServlet</servlet-name>
<servlet-class>com.bjpowernode.drp.basedata.web.FileUploadServlet</servlet-class>
<load-on-startup>10</load-on-startup>
</servlet>
这里须是正整数,数值越小,启动顺序越早。
2、filter过滤器的配置:
一个系统可以有多个filter,他是一个链,按照顺序执行,最后到你请求的资源servlet或jsp。如,配置系统中所有的jsp和servlet统一的编码格式:
<filter>
<filter-name>CharsetEncodingFilter</filter-name>
<filter-class>com.bjpowernode.drp.util.filter.CharsetEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>GBK</param-value>
</init-param>
</filter>
<!-- 所有的jsp的编码格式 -->
<filter-mapping>
<filter-name>CharsetEncodingFilter</filter-name>
<url-pattern>*.jsp</url-pattern>
</filter-mapping>
<!-- 所有servlet的编码格式 -->
<filter-mapping>
<filter-name>CharsetEncodingFilter</filter-name>
<url-pattern>/servlet/*</url-pattern>
</filter-mapping>
CharsetEncodingFilter类对编码格式的实现:
/**
* 采用Filter统一处理字符集
* @author Administrator
*
*/
public class CharsetEncodingFilter implements Filter {
private String endcoding;
public void destroy() {
}
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
//System.out.println("CharsetEncodingFilter--->>>begin");
//设置字符集
request.setCharacterEncoding(endcoding);
//继续执行,把请求传递给下一个链
chain.doFilter(request, response);
//System.out.println("CharsetEncodingFilter--->>>end");
}
public void init(FilterConfig filterConfig) throws ServletException {
this.endcoding = filterConfig.getInitParameter("encoding");
System.out.println("CharsetEncodingFilter.init()-->> endcoding=" + endcoding);
}
}
3、application变量<context-param>的配置:
<context-param>
<param-name>page-size</param-name>
<param-value>2</param-value>
</context-param>
这里配置的变量是全局变量所有的jsp和servlet都可以访问,相当于application级别。
对全局变量的访问:
//从application范围内取得page-size,application指的是ServletContext对象
int pageSize = Integer.parseInt(this.getServletContext().getInitParameter("page-size"));
针对全局变量的访问和servlet级别的变量的访问方法是一样的。
4、session的配置:
<session-config>
<session-timeout>60</session-timeout>
</session-config>
这里配置session的声明周期为60秒。
5、关于自定义错误类型或404、500错误的配置:
无论是自定义错误还是404/500这些常见的错误,在tomcat看来都需要在<error-page>中配置,但是自定义错误配置在<exception-type>节点中,404配置在<error-code>节点中。他们都有一个<location>节点,说明错误页指向。
<error-page>
<exception-type>com.bjpowernode.drp.util.ApplicationException</exception-type>
<location>/error.jsp</location>
</error-page>
<error-page>
<error-code>404</error-code>
<location>/http_error.jsp</location>
</error-page>
<error-page>
<error-code>500</error-code>
<location>/http_error.jsp</location>
</error-page>
关于自定义异常
类
ApplicationException的实现,只需要继承一下RuntimeException即可,然后在使用的时候,直接多种重载都可以直接使用。
ApplicationException定义:
public class ApplicationException extends RuntimeException {
public ApplicationException() {
}
public ApplicationException(String message) {
super(message);
}
public ApplicationException(Throwable cause) {
super(cause);
}
public ApplicationException(String message, Throwable cause) {
super(message, cause);
}
}
使用:
throw new ApplicationException("删除物料失败!");
总结:
这里区分了一下java项目中不同类型的xml文件的用途区别。然后重点介绍了web.xml文件中servlet、filter、error、session、application的配置和初始话顺序、参数、声明周期以及对filter、自定义异常的实现。
另外,java解析xml文件,尤其是对xml文件的读取是一项重要的基本功,有现成的若干组件,上篇博客中介绍了使用dom4j解析的实现,这里不再赘述。