程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> java支持自定義擴展名或空擴展名的Dispatcher

java支持自定義擴展名或空擴展名的Dispatcher

編輯:關於JAVA
 

支持自定義類似於”.do”, “.php”, “.aspx”這樣的擴展名, 或者不使用擴展名. 在不使用擴展名的情況下, 所有被識別為action的鏈接, 最後一個foward slash後不能再出現”.”號, 以免和常用的mime types混淆.

public class Dispatcher implements Filter {

private static Logger logger = Logger.getLogger(Dispatcher.class);
private String attribute = null;
private FilterConfig filterConfig = null;
private static String ext =SimpleConfig.getConfig("extension");

// Initialise the dispatcher filter rule
private static String regex;
static
{
if (ext==null || ext.length() == 0 || ext.length() == 1)
{
ext = "";
regex = ".*?/[^\\.]*?$";
}
else
{
if (ext.charAt(0)!='.') {ext = "."+ext;}
regex = ".*?\\."+ ext.substring(1) + "$";
}
}

public void init(FilterConfig filterConfig) throws ServletException
{
this.filterConfig = filterConfig;
this.attribute = filterConfig.getInitParameter("attribute");
}

public void destroy()
{
this.attribute = null;
this.filterConfig = null;
}

public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException
{
HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse) res;
String servletPath = request.getServletPath();

if (servletPath.matches(regex))
{
long startTime = System.currentTimeMillis();

String name = servletPath.substring(1, servletPath.length() - ext.length()).replaceAll("/", ".");

ActionContext context = SimpleConfig.getActionContext(name);

if (context != null)
{
ActionContainer container = new ActionContainer(context, request, response);
container.process();
}
long stopTime = System.currentTimeMillis();
logger.info( "Dispatcher: " + servletPath + ": " + (stopTime - startTime) + " milliseconds");
}
else
{
chain.doFilter(req, res);
}
}

}
 

 
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved