web.xml配置详解之listener

本文详细介绍了web.xml中listener配置的作用及其使用方法。通过具体的代码示例,展示了如何利用WebServicePublishListener来发布WebService,包括初始化和销毁的方法实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

web.xml配置详解之listener

  • 定义
<listener>
    <listener-class>nc.xyzq.listener.WebServicePublishListener</listener-class>
</listener> 
  • 作用

        该元素用来注册一个监听器类。可以收到事件什么时候发生以及用什么作为响应的通知。事件监听程序在建立、修改和删除会话或servlet环境时得到通知。常与context-param联合使用。

  • listen-class标签

         listen-class 指定监听类,该类继承ServletContextListener 包含初始化方法contextInitialized(ServletContextEvent event) 和

销毁方法contextDestoryed(ServletContextEvent event);

  • 示例

         初始化日志配置文件

package nc.xyzq.listener;

import java.net.InetAddress;
import java.net.UnknownHostException;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
import javax.xml.ws.Endpoint;
import nc.xyzq.uuib.service.impl.WebServiceImpl;
 
 /**
  * 用于发布WebService的监听器
  */
 //使用Servlet.提供的@WebListener注解将实现了ServletContextListener接口的WebServicePublishListener类标注为一个Listener
 @WebListener
 public class WebServicePublishListener implements ServletContextListener {
 
     @Override
     public void contextDestroyed(ServletContextEvent sce) {
       System.gc();
     }
 
     @Override
     public void contextInitialized(ServletContextEvent sce) {
        //WebService的发布地址
        String ip ="";
        try {
            ip = InetAddress.getLocalHost().getHostAddress();
        } catch (UnknownHostException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
         System.out.println("本机的IP = " + ip);
         String address = "http://"+ip+":8080/rzzxservices/WebService";
         //发布WebService,WebServiceImpl类是WebServie接口的具体实现类
         Endpoint.publish(address , new WebServiceImpl());
         System.out.println("使用WebServicePublishListener发布webservice成功!");
     }  
 }
 

 

转载于:https://www.cnblogs.com/lizm166/p/7919388.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值