1. Create a new TestServlet class
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 { // Get the full path of the class and name String className = request.getParameter("className"); // Get the method name String methodName = request.getParameter("method"); try { // Get the class file Class<?> t_class = Class.forName(className); // Get the method required by this class Method method = t_class.getDeclaredMethod(methodName, HttpServletRequest.class, HttpServletResponse.class); method.invoke(t_class.newInstance(), request, response);// Implementation of the method} catch (Exception e) { e.printStackTrace(); } } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); }} 2. Create a class that needs to be called automatically
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 configuration
<?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 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. Start server access:
http://127.0.0.1:8081/TestPrj/Test?className=com.yanek.test.Test&method=test&username=aspboy
Console output:
hello
aspboy
Description: The method of class com.yanek.test.Test class public void test(HttpServletRequest request, HttpServletResponse response) is executed.
The reflection mechanism is an important function in Java and is widely used in framework design.
Test environment: tomcat6.0
The above simple method to use the Java reflection mechanism to achieve automatic calling classes is all the content I share with you. I hope you can give you a reference and I hope you can support Wulin.com more.