利用java反射機(jī)制實(shí)現(xiàn)自動(dòng)調(diào)用類的簡(jiǎn)單方法
1. 新建TestServlet類
package com.yanek.test;
import java.io.IOException;
import java.lang.reflect.Method;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class TestServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 獲取類的全路徑以及名稱
String className = request.getParameter("className");
// 獲取方法名
String methodName = request.getParameter("method");
try {
// 獲取class文件
Class<?> t_class = Class.forName(className);
// 獲取該類所需求的方法
Method method = t_class.getDeclaredMethod(methodName,
HttpServletRequest.class, HttpServletResponse.class);
method.invoke(t_class.newInstance(), request, response);// 方法的實(shí)現(xiàn)
} catch (Exception e) {
e.printStackTrace();
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
2. 建立需要自動(dòng)調(diào)用的類
package com.yanek.test;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
System.out.println("hello world !");
}
public void test(HttpServletRequest request, HttpServletResponse response) {
System.out.println("hello");
System.out.println(request.getParameter("username"));
}
}
3. web.xml配置
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <servlet> <description>Test</description> <display-name>Test</display-name> <servlet-name>Test</servlet-name> <servlet-class>com.yanek.test.TestServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>Test</servlet-name> <url-pattern>/Test</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <login-config> <auth-method>BASIC</auth-method> </login-config> </web-app>
4. 啟動(dòng)服務(wù)器訪問:
http://127.0.0.1:8081/TestPrj/Test?className=com.yanek.test.Test&method=test&username=aspboy
控制臺(tái)輸出:
hello
aspboy
說明: 類com.yanek.test.Test類的 方法 public void test(HttpServletRequest request, HttpServletResponse response) 被執(zhí)行了.
反射機(jī)制是java中的重要功能,在框架設(shè)計(jì)中大量使用.
測(cè)試環(huán)境: tomcat6.0
以上這篇利用java反射機(jī)制實(shí)現(xiàn)自動(dòng)調(diào)用類的簡(jiǎn)單方法就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Spring Boot熱加載jar實(shí)現(xiàn)動(dòng)態(tài)插件的思路
本文主要介紹在 Spring Boot 工程中熱加載 jar 包并注冊(cè)成為 Bean 對(duì)象的一種實(shí)現(xiàn)思路,在動(dòng)態(tài)擴(kuò)展功能的同時(shí)支持在插件中注入主程序的 Bean 實(shí)現(xiàn)功能更強(qiáng)大的插件2021-10-10
springcloud整合seata的實(shí)現(xiàn)代碼
這篇文章主要介紹了springcloud整合seata的實(shí)現(xiàn)方法,整合步驟通過引入spring-cloud-starter-alibaba-seata?jar包,文中結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-05-05
Java數(shù)組擴(kuò)容實(shí)現(xiàn)方法解析
這篇文章主要介紹了Java數(shù)組擴(kuò)容實(shí)現(xiàn)方法解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-11-11
Jmeter如何將每次測(cè)試的結(jié)果保存到文件中
這篇文章主要介紹了Jmeter如何將每次測(cè)試的結(jié)果保存到文件中的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-03-03
java使用ArrayList遍歷及效率比較實(shí)例分析
這篇文章主要介紹了java使用ArrayList遍歷及效率比較,實(shí)例分析了ArrayList遍歷的方法與使用技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-07-07

