Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Tomcat parses the principles of XML and reflection object creation

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

Here's how Tomcat parses XML and how to create objects by reflection through the example code. The specific code is as follows:

Import java.lang.reflect.InvocationTargetException;import java.lang.reflect.Method;import java.util.List;import org.dom4j.Document;import org.dom4j.DocumentException;import org.dom4j.Element;import org.dom4j.io.SAXReader;public class ServerReadXML1 {public static void main (String [] args) throws DocumentException, ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, SecurityException, IllegalArgumentException, InvocationTargetException {/ / now if you enter a url-pattern String urlPattern of Servlet in the browser = "/ first" / / get the class name String className = getClassByUrl (urlPattern) according to the urlPattern; / / get the Class object Class clazz = Class.forName (className) based on the full class name; / / create the specified object Object obj = clazz.newInstance () by reflecting the clazz object; / / get the service method Method method = clazz.getDeclaredMethod ("service"); / / get the permission method.setAccessible (true) / / execute service method method.invoke (obj);} private static String getClassByUrl (String urlPattern) throws DocumentException {/ / 1. Create a SAXReader object SAXReader reader = new SAXReader (); / / 2. Read the file Document document = reader.read (ServerReadXML1.class.getClassLoader (). GetResourceAsStream ("web.xml")); / / 3. Get root node Element rootElement = document.getRootElement (); / / System.out.println (rootElement.getName ()); / / 4. Get the child node List servletList = rootElement.elements () under the root node; / / record the content of the same servlet-name tag as urlPattern String servletName = ""; / / record the content of servlet-class in the servlet tag / / servletClassName, that is, the full class name of Servlet String servletClassName = "; / / 5. Traverse the child node for (Element servletElement: servletList) {/ / System.out.println (servletElement.getName ()); / / determine if it is a servlet-mapping tag, execute the code if ("servlet-mapping" .equals (servletElement.getName () {/ / get the url-pattern tag object Element url = servletElement.element ("url-pattern") / / determine whether the content of the tag and the entered urlPattern value are the same if (urlPattern.equals (url.getText () {/ / record the content of the servlet-name tag that is the same as urlPattern / / if it is the same, record ServletName / / get the content of servelt-name in servlet-mapping servletName = servletElement.element ("servlet-name"). GetText () } / / iterate through for (Element servletElement: servletList) {/ / determine if it is a servlet tag Execute this code if ("servlet" .equals (servletElement.getName () {/ / determine whether the value of the servletName obtained in the last traversal is the same as the content of the servlet-name in this traversal (servletName.equals (servletElement.element ("servlet-name"). GetText ()) {/ / if the same record servletClassName servletClassName = servletElement.element ("servlet-class"). GetText () } / / return the full class name of Servlet servletClassName return servletClassName;}}

1. Four ways to obtain Class of reflection

Test public void test1 () throws ClassNotFoundException {/ / 1. Class name. Class Class clazz = String.class; System.out.println (clazz); / / 2. Object .getClass () Class clazz1 = "abc" .getClass (); System.out.println (clazz1); / / 3.Class.forName (); Class clazz2 = Class.forName ("java.lang.String"); System.out.println (clazz2); / / 4.ClassLoader .loadClass ("full class name") Class clazz3 = ReflectTest1.class.getClassLoader (). LoadClass ("java.lang.String"); System.out.println (clazz3);}

two。 Common ways for reflection to use attributes

@ Test public void test2 () throws ClassNotFoundException, NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {/ / get the Class object can get its internal properties Class clazz = Class.forName ("com.atguigu.bean.User"); User user = new User (); / / the getField of the class represented by the Field object can only get the public property Field field = clazz.getField ("email"); System.out.println (field) / / it is not recommended to use Field field2 = clazz.getDeclaredField ("id"); System.out.println (field2); field2.setAccessible (true); field2.setInt (user, 1001); System.out.println (user);}

3. Common methods of using reflection

@ Test public void test3 () throws ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, SecurityException, IllegalArgumentException, InvocationTargetException {Class clazz = Class.forName ("com.atguigu.bean.User"); / / create the object Object obj = clazz.newInstance () by reflection; / / now you want to set the name value String fileName = "name" / / create a method name String methodName = "set" + fileName.substring (0,1). ToUpperCase () / N + fileName.substring (1). ToLowerCase (); / / ame / / get the public method Method method = clazz.getMethod (methodName, String.class) based on the method name; / / execute the specified method method.invoke (obj, "yangjian"); System.out.println (obj);}

Summary

The above is the Tomcat parsing XML and reflection creation object principle introduced by the editor to you. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support to the website!

If you think this article is helpful to you, you are welcome to reprint it, please indicate the source, thank you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report