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() {
}
}