Servlet-模块化开发web fragment
2015年9月17日
注意:web-fragment.xml会自动生成display-name标签,则标签要删除,否则无法使用多个模块。
catalina.log中的错误提示:Thedisplay name was defined in multiple fragments with different values
参考:http://chainhou.iteye.com/blog/1986090
注意:如果指定了目标应用程序,则Eclipse会自动打包、应用。
删除display-name标签。
增加相关的标签。(也可以使用注记)。
模块名称:<name>
模块加载顺序:<ordering><before><after>
开发模块化的功能类。
//第一个监听模块:TF1的web-fragment.xml
<?xmlversion="1.0" encoding="UTF-8"?>
<web-fragmentxmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="http://xmlns.jcp.org/xml/ns/javaee"xmlns:webfragment="http://java.sun.com/xml/ns/javaee/web-fragment_3_0.xsd"xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaeehttp://xmlns.jcp.org/xml/ns/javaee/web-fragment_3_1.xsd"id="WebFragment_ID" version="3.1">
<name>TF1</name>
<listener>
<listener-class>lee.TF1</listener-class>
</listener>
<ordering>
<before>
<others/>
</before>
</ordering>
</web-fragment>
//第一个监听模块的处理类:TF1.java
package lee;
importjavax.servlet.ServletRequestEvent;
import javax.servlet.ServletRequestListener;
importjavax.servlet.annotation.WebListener;
/**
* Application Lifecycle Listenerimplementation class TF1
*
*/
@WebListener
public class TF1 implementsServletRequestListener {
/**
* Default constructor.
*/
public TF1() {
// TODO Auto-generated constructor stub
}
/**
* @seeServletRequestListener#requestDestroyed(ServletRequestEvent)
*/
public voidrequestDestroyed(ServletRequestEvent arg0) {
// TODO Auto-generated method stub
System.out.println("1.1");
}
/**
* @seeServletRequestListener#requestInitialized(ServletRequestEvent)
*/
public voidrequestInitialized(ServletRequestEvent arg0) {
// TODO Auto-generated method stub
System.out.println("1.0");
}
}
//第二个监听模块:TF2的web-fragment.xml
<?xmlversion="1.0" encoding="UTF-8"?>
<web-fragmentxmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:webfragment="http://java.sun.com/xml/ns/javaee/web-fragment_3_0.xsd"xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaeehttp://xmlns.jcp.org/xml/ns/javaee/web-fragment_3_1.xsd"id="WebFragment_ID" version="3.1">
<name>TF2222222222</name>
<listener>
<listener-class>lee.TF2</listener-class>
</listener>
<ordering>
<after>
<name>TF1</name>
</after>
</ordering>
</web-fragment>
package lee;
//第二个监听模块处理类:TF2.java
importjavax.servlet.ServletRequestEvent;
import javax.servlet.ServletRequestListener;
importjavax.servlet.annotation.WebListener;
/**
* Application Lifecycle Listenerimplementation class TF2
*
*/
@WebListener
public class TF2 implementsServletRequestListener {
/**
* Default constructor.
*/
public TF2() {
// TODO Auto-generated constructor stub
}
/**
* @seeServletRequestListener#requestDestroyed(ServletRequestEvent)
*/
public voidrequestDestroyed(ServletRequestEvent arg0) {
// TODO Auto-generated method stub
System.out.println("2.1");
}
/**
* @seeServletRequestListener#requestInitialized(ServletRequestEvent)
*/
public voidrequestInitialized(ServletRequestEvent arg0) {
// TODO Auto-generated method stub
System.out.println("2.0");
}
}
//第三个监听模块:TF3的web-fragment.xml
<?xmlversion="1.0" encoding="UTF-8"?>
<web-fragmentxmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:webfragment="http://java.sun.com/xml/ns/javaee/web-fragment_3_0.xsd"xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaeehttp://xmlns.jcp.org/xml/ns/javaee/web-fragment_3_1.xsd"id="WebFragment_ID" version="3.1">
<name>TF3</name>
</web-fragment>
//第三个监听模块处理类:TF3.java
package lee;
import java.io.IOException;
import javax.servlet.Filter;
importjavax.servlet.FilterChain;
importjavax.servlet.FilterConfig;
importjavax.servlet.ServletException;
importjavax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
importjavax.servlet.annotation.WebFilter;
/**
* Servlet Filter implementation class TF3
*/
@WebFilter("/*")
public class TF3 implementsFilter {
/**
* Default constructor.
*/
public TF3() {
// TODO Auto-generated constructor stub
}
/**
* @see Filter#destroy()
*/
public void destroy() {
// TODO Auto-generated method stub
System.out.println("3.3");
}
/**
* @seeFilter#doFilter(ServletRequest, ServletResponse, FilterChain)
*/
public void doFilter(ServletRequest request, ServletResponseresponse, FilterChain chain) throws IOException, ServletException {
// TODO Auto-generated method stub
// place your code here
// pass the request along the filter chain
System.out.println("3.1");
chain.doFilter(request, response);
System.out.println("3.2");
}
/**
* @seeFilter#init(FilterConfig)
*/
public void init(FilterConfig fConfig) throws ServletException{
// TODO Auto-generated method stub
System.out.println("3.0");
}
}
//结果:a.jsp请求