Use the java reflection mechanism to automatically call the Class Method

Source: Internet
Author: User

1. Create a 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 {
// Obtain the full path and name of the class
String className = request. getParameter ("className ");
// Obtain the method name
String methodName = request. getParameter ("method ");
Try {
// Obtain the class file
Class <?> T_class = Class. forName (className );
// Obtain the required methods for this class
Method method = t_class.getDeclaredMethod (methodName,
HttpServletRequest. class, HttpServletResponse. class );
Method. invoke (t_class.newInstance (), request, response); // method implementation
} Catch (Exception e ){
E. printStackTrace ();
}
}

Public void doPost (HttpServletRequest request, HttpServletResponse response)
Throws ServletException, IOException {
DoGet (request, response );
}

}

 

2. Create a class 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/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

 

Note: The public void test (HttpServletRequest request, HttpServletResponse response) method of the com. yanek. Test. test class is executed.

Reflection mechanism is an important function in java and is widely used in framework design.

Test environment: tomcat6.0

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.