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 ();