Servlet-模块化开发web fragment

Servlet-模块化开发web fragment

[email protected]

2015年9月17日

 

1 目标:在不影响web.xml整体结构的情况下,独立开发web模块,提高复用性、降低耦合。

2 原理:容器识别web-fragment.xml配置文件,将其与整体应用合并。

3 流程:在web.xml中启用模块化(与注记相同)。开发web fragment project。打包。应用到目webapp。

注意:web-fragment.xml会自动生成display-name标签,则标签要删除,否则无法使用多个模块。

catalina.log中的错误提示:Thedisplay name was defined in multiple fragments with different values

参考:http://chainhou.iteye.com/blog/1986090

4 方法:

5 开发web fragment project:

5.1 新建webfragment project应用。

注意:如果指定了目标应用程序,则Eclipse会自动打包、应用。

5.2 配置web-fragment.xml。

删除display-name标签。

增加相关的标签。(也可以使用注记)。

模块名称:<name>

模块加载顺序:<ordering><before><after>

5.3 开发相应的servlet。

开发模块化的功能类。

6 打包:export 为jar。

7 应用:将jar复制到web-inf的lib目录。

8 示例:创建两个独立的监听器和一个过滤器

//第一个监听模块: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请求

 

你可能感兴趣的:(java,Web,servlet,javaee,Fragment,模块化开发)