手机
当前位置:查字典教程网 >编程开发 >Java >java基于包结构的请求路由实现实例分享
java基于包结构的请求路由实现实例分享
摘要:WebFilter.java复制代码代码如下:packagecom.hongyuan.route;importjava.io.File;im...

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;

}

}

【java基于包结构的请求路由实现实例分享】相关文章:

java中通用的线程池实例代码

java调用未知类的指定方法简单实例

java网络编程中向指定URL发送GET POST请求示例

java循环练习的简单代码实例

java开发之MD5加密算法的实现

java反射实现javabean转json实例代码

java list与数组之间的转换详细解析

Java泛型的简单实例

java定时任务的实现方法

java 二维数组矩阵乘法的实现方法

精品推荐
分类导航