利用java反射機制實現(xià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);// 方法的實現(xiàn)
} catch (Exception e) {
e.printStackTrace();
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
2. 建立需要自動調用的類
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. 啟動服務器訪問:
http://127.0.0.1:8081/TestPrj/Test?className=com.yanek.test.Test&method=test&username=aspboy
控制臺輸出:
hello
aspboy
說明: 類com.yanek.test.Test類的 方法 public void test(HttpServletRequest request, HttpServletResponse response) 被執(zhí)行了.
反射機制是java中的重要功能,在框架設計中大量使用.
測試環(huán)境: tomcat6.0
以上這篇利用java反射機制實現(xiàn)自動調用類的簡單方法就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
Springcloud實現(xiàn)服務多版本控制的示例代碼
這篇文章主要介紹了Springcloud實現(xiàn)服務多版本控制的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-05-05
詳解spring boot整合JMS(ActiveMQ實現(xiàn))
本篇文章主要介紹了詳解spring boot整合JMS(ActiveMQ實現(xiàn)),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-10-10
Spring Boot集成Shiro并利用MongoDB做Session存儲的方法詳解
這篇文章主要給大家介紹了關于Spring Boot集成Shiro并利用MongoDB做Session存儲的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友下面來一起看看吧。2017-12-12
Java利用trueLicense實現(xiàn)項目離線證書授權操作步驟
文章介紹了如何使用trueLicense實現(xiàn)離線授權控制,包括生成公私鑰、創(chuàng)建證書校驗模塊、生成證書模塊和測試模塊,通過這種方式,可以控制用戶使用的項目模塊、授權周期、使用的設備和服務器,感興趣的朋友跟隨小編一起看看吧2024-11-11
Java使用Thread創(chuàng)建多線程并啟動操作示例
這篇文章主要介紹了Java使用Thread創(chuàng)建多線程并啟動操作,結合實例形式分析了Java基于Thread類的多線程定義與啟動簡單操作技巧,需要的朋友可以參考下2018-06-06

