Apache. Tomcat calls the Class reflection mechanism of Servlet principles, which is explained by the orc Class,

Source: Internet
Author: User

Apache. Tomcat calls the Class reflection mechanism of Servlet principles, which is explained by the orc Class,

There is a beast

package com.swift.servlet;public class OrcDemo {private int hp;private int mp;private int atk;public int getHp() {return hp;}public void setHp(int hp) {this.hp = hp;}public int getMp() {return mp;}public void setMp(int mp) {this.mp = mp;}public int getAtk() {return atk;}public void setAtk(int atk) {this.atk = atk;}public OrcDemo() {}public OrcDemo(int hp, int mp, int atk) {super();this.hp = hp;this.mp = mp;this.atk = atk;}public void orcInfo() {System.out.println("hp"+hp+"mp"+mp+"atk"+atk);}public static void main(String[] args) {OrcDemo orc=new OrcDemo(3000,2000,500);orc.orcInfo();}}
How to use the original Orcs object:
Public static void main (String [] args) {OrcDemo orc = new OrcDemo (3000,2000, 500); orc. orcInfo ();}
Use Class reflection to get all the contents of the orcs object:

Class c = Class. forName ("OrcDemo ");
OrcDemo od = (OrcDemo) c. newInstance ();
Od. orcInfo ();

Apache. tomcat is the Servlet called using this method.

In the web. xml configuration file
<Servlet>
<Servlet-name> ServletDemo </servlet-name>
<Servlet-class> com. swift. servlet. TestServlet </servlet-class>
</Servlet>
<Servlet-mapping>
<Servlet-name> ServletDemo <servlet-name>
<Url-pattern>/test </url-pattern>
</Servlet-mapping>
Use the com. swift. servlet. TestServlet in <servlet-class> com. swift. servlet. TestServlet </servlet-class> above for use.
Apply the reflected code just written
Class c = Class. forName ("OrcDemo ");
OrcDemo od = (OrcDemo) c. newInstance ();
Od. orcInfo ();
Change
Class c = Class. forName ("com. swift. servlet. testServlet "); // ServletDemo sd = (ServletDemo) c. newInstance ();/* tomcat uses the reflection method only because it does not know the ServletDemo class. here we can use its parent class to perform polymorphism */HttpServlet hs = c. newInstance (); hs. doGet ();

  

 
 

Related Article

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.