程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> java基於包構造的要求路由完成實例分享

java基於包構造的要求路由完成實例分享

編輯:關於JAVA

java基於包構造的要求路由完成實例分享。本站提示廣大學習愛好者:(java基於包構造的要求路由完成實例分享)文章只能為提供參考,不一定能成為您想要的結果。以下是java基於包構造的要求路由完成實例分享正文


WebFilter.java

package com.hongyuan.route;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

public class WebFilter implements Filter {

 public static final String BASE_PACKAGE="com.hongyuan";
 public static final String CLASS_FLAG="Action";
 @Override
 public void destroy() {}

 @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);
  }

 }
 //斷定能否存在要求的資本
 private boolean hasResouce(ServletRequest request) {
  String realPath=request.getServletContext().getRealPath(((HttpServletRequest)request).getServletPath());
  File resouce=new File(realPath);
  if(resouce.exists()){
   return true;
  }else{
   return false;
  }
 }
 //初始化高低文
 private Object initContext(Class clazz, ServletRequest request,
   ServletResponse response) throws Exception{
  Object instance=clazz.newInstance();
  clazz.getMethod("setRequest",new Class[]{ServletRequest.class}).invoke(instance, new Object[]{request});
  clazz.getMethod("setResponse", new Class[]{ServletResponse.class}).invoke(instance, new Object[]{response});
  return instance;
 }

 @Override
 public void init(FilterConfig arg0) throws ServletException {}

}


BaseAction.java

package com.hongyuan.route;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

public abstract class BaseAction {
 protected ServletRequest request=null;
 protected ServletResponse response=null;

 public ServletRequest getRequest() {
  return request;
 }
 public void setRequest(ServletRequest request) {
  this.request = request;
 }
 public ServletResponse getResponse() {
  return response;
 }
 public void setResponse(ServletResponse response) {
  this.response = response;
 }

}

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