您的位置:首页 > 编程语言 > Java开发

java基于包结构的请求路由实现实例分享

2013-12-31 09:30 671 查看

 @Override
 public void doFilter(ServletRequest request, ServletResponse response,FilterChain chain) throws IOException, ServletException {

  if(!hasResouce(request)){

   String requestPath=((HttpServletRequest)request).getServletPath();
   if(requestPath.indexOf(".")>=0){
    requestPath=requestPath.substring(0, requestPath.indexOf("."));
   }
   if(requestPath.endsWith("/")){
    requestPath=requestPath.substring(0, requestPath.length()-1);
   }
   //获取请求的类全限定名
   String className=BASE_PACKAGE+requestPath.replaceAll("/", ".")+CLASS_FLAG;
   //获取请求方法名称
   String methodName=request.getParameter("method");
   if(methodName==null||"".equals(methodName.trim())){
    methodName="index";
   }
   try {
    //获取处理类并响应请求
    Class clazz=Class.forName(className);
    Object instance=this.initContext(clazz,request,response);
    Method method=clazz.getMethod(methodName, new Class[]{});
    Object result=method.invoke(instance, new Object[]{});
    response.getWriter().print(result);
   } catch (Exception e) {
    e.printStackTrace(response.getWriter());
   }
  }else{
   chain.doFilter(request, response);
  }

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  请求路由