利用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)文章
Springcloud實(shí)現(xiàn)服務(wù)多版本控制的示例代碼
這篇文章主要介紹了Springcloud實(shí)現(xiàn)服務(wù)多版本控制的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-05-05
詳解spring boot整合JMS(ActiveMQ實(shí)現(xiàn))
本篇文章主要介紹了詳解spring boot整合JMS(ActiveMQ實(shí)現(xiàn)),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-10-10
Spring?Boot快速過濾出一次請(qǐng)求的所有日志
這篇文章主要介紹了Spring?Boot快速過濾出一次請(qǐng)求的所有日志,本文講述了如何使用MDC工具來快速過濾一次請(qǐng)求的所有日志,并通過裝飾器模式使得MDC工具在異步線程里也能生效,需要的朋友可以參考下2022-11-11
java數(shù)據(jù)結(jié)構(gòu)基礎(chǔ):棧
這篇文章主要介紹了Java的數(shù)據(jù)解構(gòu)基礎(chǔ),希望對(duì)廣大的程序愛好者有所幫助,同時(shí)祝大家有一個(gè)好成績(jī),需要的朋友可以參考下,希望能給你帶來幫助2021-07-07
Spring Boot集成Shiro并利用MongoDB做Session存儲(chǔ)的方法詳解
這篇文章主要給大家介紹了關(guān)于Spring Boot集成Shiro并利用MongoDB做Session存儲(chǔ)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友下面來一起看看吧。2017-12-12
Java利用trueLicense實(shí)現(xiàn)項(xiàng)目離線證書授權(quán)操作步驟
文章介紹了如何使用trueLicense實(shí)現(xiàn)離線授權(quán)控制,包括生成公私鑰、創(chuàng)建證書校驗(yàn)?zāi)K、生成證書模塊和測(cè)試模塊,通過這種方式,可以控制用戶使用的項(xiàng)目模塊、授權(quán)周期、使用的設(shè)備和服務(wù)器,感興趣的朋友跟隨小編一起看看吧2024-11-11
Java使用Thread創(chuàng)建多線程并啟動(dòng)操作示例
這篇文章主要介紹了Java使用Thread創(chuàng)建多線程并啟動(dòng)操作,結(jié)合實(shí)例形式分析了Java基于Thread類的多線程定義與啟動(dòng)簡(jiǎn)單操作技巧,需要的朋友可以參考下2018-06-06

