Modular development using web-fragment.xml in java

Source: Internet
Author: User

Development based on eclipse

1. Create a web-fragment project
File-> New-> Other...-> Web Fragment Project-> Next-> add the name of the Project to be created-> Next-> Finish

2. Compile file content in web-fragment.xml

The code is as follows: Copy code

<? Xml version = "1.0" encoding = "UTF-8"?>
<Web-fragment id = "WebFragment_ID" version = "3.0" xmlns = "http://java.sun.com/xml/ns/javaee" xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xsi: schemaLocation = "http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-fragment_3_0.xsd">
<! -- Module name -->
<Display-name> testt </display-name>
<! -- Module ID -->
<Name> testt </name>
<! -- Here is a request listener I configured -->
<Listener>
<Listener-class> org. Rudiment. Listener. MyRequestListener </listener-class>
</Listener>
<! -- Configure the startup sequence of this module -->
<Ordering>
<Before> <others/> </before>
</Ordering>
</Web-fragment>


3. Create a package org. Rudiment. Listener under src.
4. Create a Listener class (MyRequestListener) under the org. Rudiment. Listener package)

The code is as follows: Copy code

Package org. Rudiment. Listener;

Import javax. servlet. ServletRequestAttributeEvent;
Import javax. servlet. ServletRequestAttributeListener;
Import javax. servlet. ServletRequestEvent;
Import javax. servlet. ServletRequestListener;
Import javax. servlet. http. HttpServletRequest;

Public class MyRequestListener implements ServletRequestListener, ServletRequestAttributeListener
{

@ Override
Public void attributeAdded (ServletRequestAttributeEvent arg0 ){
System. out. println ("attribute in the Request range:" + arg0.getName () + "added, value:" + arg0.getValue ());
    }

@ Override
Public void attributeRemoved (ServletRequestAttributeEvent arg0 ){
System. out. println ("attribute in the Request range:" + arg0.getName () + "deleted, value:" + arg0.getValue ());
    }

@ Override
Public void attributeReplaced (ServletRequestAttributeEvent arg0 ){
System. out. println ("attribute in the Request range:" + arg0.getName () + "value updated to:" + arg0.getValue ());
    }

@ Override
Public void requestDestroyed (ServletRequestEvent arg0 ){
HttpServletRequest request = (HttpServletRequest) arg0.getServletRequest ();
System. out. println ("The request is about to end ");
    }

@ Override
Public void requestInitialized (ServletRequestEvent arg0 ){
HttpServletRequest request = (HttpServletRequest) arg0.getServletRequest ();
System. out. println ("The request is about to begin. The request comes from" + request. getHeader ("Referer") + "send" + request. getRequestURI ());
    }

}


 

5. Place the cursor over the project name, right-click export-> Java-> Jar-> keep default Next
6. put our exported jar in the web WEB-INF/lib/where I need to add the module. At this time, restart Tomcat and then access the page and find that, the tomcat background displays the information printed by the listener.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.