用過濾器獲取部署服務器的端口號java
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 }