用过滤器获取部署服务器的端口号
1 package com.booway.pcdm.jjpt.fliter; 2 import java.io.IOException; 3 import javax.servlet.*; 4 /** 5 * 增加获取端口过滤器 6 * @author BOOWAY 7 * 8 */ 9 public class PortFliter implements Filter 10 { 11 // 用来装载端口 12 public static String port; 13 // 静态计数器 类 加载的时候装载 14 public static int num; 15 16 @Override 17 public void destroy() 18 { 19 // System.out.println("销毁了"); 20 } 21 22 @Override 23 public void doFilter(ServletRequest request, ServletResponse response, 24 FilterChain chain) throws IOException, ServletException 25 { 26 // 获取端口号 27 int localPort = request.getLocalPort(); 28 // 说明拿到了端口 29 if (localPort!=0) 30 { 31 if (num>0) 32 { 33 // 不重复赋值 34 } 35 else 36 { 37 port=String.valueOf(localPort); 38 } 39 num++; 40 } 41 // 放行 42 chain.doFilter(request, response); 43 44 } 45 46 @Override 47 public void init(FilterConfig arg0) throws ServletException 48 { 49 // System.out.println("初始化了"); 50 } 51 52 }