泛域名解析

web.xml配置


<!-- webSite dns 泛域名解析 -->
<filter>
<filter-name>webDnsFilter</filter-name>
<filter-class>
net.hlj.schoolWebsite.Filter.WebDnsFilter
</filter-class>
<init-param>
<param-name>error</param-name>
<param-value>/error.jsp</param-value>
</init-param>
<init-param>
<param-name>ignorePages</param-name>
<param-value>
/error.jsp,/include/ajax_action.jsp,/download
</param-value>
</init-param>
<init-param>
<param-name>ignoreExtensions</param-name>
<param-value>
jpeg,gif,css,jpg,js,htm,html,swf,png
</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>webDnsFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- webSite dns -->



实现类:


package net.hlj.schoolWebsite.Filter;

import java.io.IOException;
import java.util.Vector;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import net.hlj.util.*;
/**
* 备注:对网站域名做泛解析
* @author:未知
* @date:2012-01-30
*/
public class WebDnsFilter implements Filter {
Vector<String> nonfilter = new Vector<String>();

private static String contextPath = null;

private static boolean hasContextPath = false;

private String forwardPage;

/*
* (non-Javadoc)
*
* @see javax.servlet.Filter#init(javax.servlet.FilterConfig)
*/
public void init(FilterConfig config) throws ServletException {
forwardPage = config.getInitParameter("error");
String[] notFilter = config.getInitParameter("ignorePages").split(",");
for (int i = 0; i < notFilter.length; i++) {
nonfilter.add(notFilter[i]);
}
String[] notFilterExt = config.getInitParameter("ignoreExtensions").split(",");
for (int i = 0; i < notFilterExt.length; i++) {
nonfilter.add(notFilterExt[i]);
}
nonfilter.add(forwardPage);
}

/*
* (non-Javadoc)
*
* @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest,
* javax.servlet.ServletResponse, javax.servlet.FilterChain)
*/
public void doFilter(ServletRequest srequest, ServletResponse sresponse,
FilterChain chain) throws IOException, ServletException {

HttpServletRequest request = (HttpServletRequest) srequest;

String uri = request.getRequestURI();
//System.out.println(uri);
String dns = "";
String forwordPage="";
//String pageName="";
String ext="";
String basePath = request.getServerName();
if (basePath != null) {
dns = basePath.substring(0, basePath.indexOf("."));
}
if (hasContextPath(request))
uri = uri.substring(uri.indexOf("/", 1));
//System.out.println(uri);
ext=uri.substring(uri.lastIndexOf(".")+1);
//pageName=uri.substring(uri.lastIndexOf(".")+1);
if (infiltermap(uri)&&infiltermap(ext)) {
if (dns != null && !dns.equals("www")&&!UtilTool.ipvalid(basePath)) {//ip留测试用
// if(uri.equals("/showNews.jsp"))
// {
// forwordPage="/schoolWebsite/webSite/showNews.jsp";
// }
// else if(uri.equals("/showInfo.jsp"))
// {
// forwordPage="/schoolWebsite/webSite/showInfo.jsp";
// }
// else
// {
// forwordPage="/schoolWebsite/webSite/index.jsp";
// }

RequestDispatcher rd = request
.getRequestDispatcher(forwordPage);
rd.forward(srequest, sresponse);
return;
}
}
chain.doFilter(srequest, sresponse);
}

public boolean hasContextPath(HttpServletRequest request) {
if (contextPath == null) {
contextPath = request.getContextPath();
hasContextPath = contextPath.length() > 0;
}
return hasContextPath;
}

private boolean infiltermap(String uri) {
return !nonfilter.contains(uri);
}

/*
* (non-Javadoc)
*
* @see javax.servlet.Filter#destroy()
*/
public void destroy() {

}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值