java基于包結(jié)構(gòu)的請(qǐng)求路由實(shí)現(xiàn)實(shí)例分享
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);
}
//獲取請(qǐng)求的類全限定名
String className=BASE_PACKAGE+requestPath.replaceAll("/", ".")+CLASS_FLAG;
//獲取請(qǐng)求方法名稱
String methodName=request.getParameter("method");
if(methodName==null||"".equals(methodName.trim())){
methodName="index";
}
try {
//獲取處理類并響應(yīng)請(qǐng)求
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);
}
}
//判斷是否存在請(qǐng)求的資源
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;
}
}
- Android 中HttpURLConnection與HttpClient使用的簡(jiǎn)單實(shí)例
- Android HttpURLConnection.getResponseCode()錯(cuò)誤解決方法
- java實(shí)現(xiàn)http請(qǐng)求工具類示例
- java發(fā)送get請(qǐng)求和post請(qǐng)求示例
- java發(fā)送url請(qǐng)求獲取返回值的二種方法
- java使用httpclient模擬post請(qǐng)求和get請(qǐng)求示例
- java模擬post請(qǐng)求登錄貓撲示例分享
- JAVA發(fā)送HTTP請(qǐng)求,返回HTTP響應(yīng)內(nèi)容,應(yīng)用及實(shí)例代碼
- java使用httpclient發(fā)送post請(qǐng)求示例
- java網(wǎng)絡(luò)編程中向指定URL發(fā)送GET POST請(qǐng)求示例
- java后臺(tái)調(diào)用HttpURLConnection類模擬瀏覽器請(qǐng)求實(shí)例(可用于接口調(diào)用)
相關(guān)文章
Java異常中toString()和getMessage()區(qū)別
在java異常體系中,要打印異常信息,可以通過(guò):e.getMessage() 、 e.toString() e.printStackTrace() 等方法打印,本文主要介紹了Java異常中toString()和getMessage()區(qū)別,具有一定的參考價(jià)值,感興趣的可以了解一下2024-01-01基于spring security實(shí)現(xiàn)登錄注銷功能過(guò)程解析
這篇文章主要介紹了基于spring security實(shí)現(xiàn)登錄注銷功能過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-01-01使用springboot單元測(cè)試對(duì)weblistener的加載測(cè)試
這篇文章主要介紹了使用springboot單元測(cè)試對(duì)weblistener的加載測(cè)試,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-10-10Java詳細(xì)分析連接數(shù)據(jù)庫(kù)的流程
Java數(shù)據(jù)庫(kù)連接,JDBC是Java語(yǔ)言中用來(lái)規(guī)范客戶端程序如何來(lái)訪問(wèn)數(shù)據(jù)庫(kù)的應(yīng)用程序接口,提供了諸如查詢和更新數(shù)據(jù)庫(kù)中數(shù)據(jù)的方法。JDBC也是Sun Microsystems的商標(biāo)。我們通常說(shuō)的JDBC是面向關(guān)系型數(shù)據(jù)庫(kù)的2022-05-05圖解Eclipse j2ee開(kāi)發(fā)環(huán)境的搭建過(guò)程
這篇文章以圖文結(jié)合的方式介紹了Eclipse j2ee開(kāi)發(fā)環(huán)境的搭建過(guò)程,內(nèi)容很詳細(xì),每一個(gè)步驟都有對(duì)應(yīng)的操作截圖,需要的朋友可以參考下2015-08-08Java使用Sharding-JDBC分庫(kù)分表進(jìn)行操作
Sharding-JDBC 是無(wú)侵入式的 MySQL 分庫(kù)分表操作工具,本文主要介紹了Java使用Sharding-JDBC分庫(kù)分表進(jìn)行操作,感興趣的可以了解一下2021-08-08maven中心倉(cāng)庫(kù)OSSRH使用簡(jiǎn)介(推薦)
這篇文章主要介紹了maven中心倉(cāng)庫(kù)OSSRH使用簡(jiǎn)介,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-04-04