Class loading has something to do with program running. You might as well test it.
Difficulty: Intermediate
Independent Process Chapter
Suppose there are the following class files:
// Main.java
package com.github.mccxj.test;
public class Main {
public static void main(String[] args){
new TestServlet().test();
}
}
// TestServlet.Java
package com.github.mccxj.test;
public class TestServlet {
public void test() {
InputStream is = TestServlet.class.getClassLoader().getResourceAsStream("config.properties");
if(is == null){
throw new RuntimeException("couId not found config.properties");
}
}
}
Assuming that the directory structure is like this, the following representation of jar is the content inside the jar package:
test -lib -test.jar -com/github/mccxj/test/Main.class -main.jar -com/github/mccxj/test/TestServlet.class -config.properties
Excuse me?
- Is there an error in executing java-cp main.jar; lib/test.jar com.github.mccxj.test.Main?
- What is the result of executing java-cp main.jar-Djava.ext.dirs=. / lib com.github.mccxj.test.Main?
Continue to adjust the catalogue as follows:
test -lib -test.jar -com/github/mccxj/test/Main.class -main.jar -com/github/mccxj/test/TestServlet.class -config.properties
Ask again
- What is the result of executing java -Djava.ext.dirs=./lib com.github.mccxj.test.Main?
Continue to adjust the code of the TestServlet:
// TestServlet.Java
package com.github.mccxj.test;
public class TestServlet {
public void test() {
- InputStream is = TestServlet.class.getClassLoader().getResourceAsStream("config.properties");
+ InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream("config.properties");
if(is == null){
throw new RuntimeException("couId not found config.properties");
}
}
}
Restore the directory structure to:
test -lib -test.jar -com/github/mccxj/test/Main.class -main.jar -com/github/mccxj/test/TestServlet.class -config.properties
Excuse me?
- Is there an error in executing java-cp main.jar; lib/test.jar com.github.mccxj.test.Main?
- What is the result of executing java-cp main.jar-Djava.ext.dirs=. / lib com.github.mccxj.test.Main?
Finally, the results of adjusting the catalogue are as follows:
test -lib -test.jar -com/github/mccxj/test/Main.class -main.jar -com/github/mccxj/test/TestServlet.class -config.properties
- What is the result of executing java -Djava.ext.dirs=./lib com.github.mccxj.test.Main?
Web Application Server Chapter
The following example is tomcat.
Suppose you have the following Servlet file and package it into test.jar:
// TestServlet.java
package com.github.mccxj.test;
public class TestServlet extends HttpServlet {
private static Atomiclnteger al = new AtomicInteger();
private Atomiclnteger a2 = new AtomicInteger();
@Override
public void service(ServletRequest arg0, ServletResponse arg1) throws Servlet Exception, IOException {
System.out.printIn(String.valueOf(al.incrementAndGet()));
System.out.printIn(String.valueOf(a2.incrementAndGet()));
}
}
And deploy two applications appa, appb, add the following in their WEB_INF/web.xml
<servlet>
<servlet-name>test</servlet-name>
<display-name>test servlet</display-name>
<servlet-class>com.huawei.test.TestServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>test</servlet-name>
<url-pattern>/test</url-pattern>
</servlet-mapping>
You should have heard of the notion that Servlet s are singletons, or that Web application servers have mechanisms for sharing classes. Then, please:
- Now throw test.jar into the WEB_INF/lib directories of appa and appb, start tomcat, visit / appa/test twice, then visit / appb/test, what will be output at this time?
- Continue to remove test.jar, add it only to TOMCAT_HOME/lib directory, start tomcat, visit / appa/test twice, then visit / appb/test, what will be output at this time?
- Finally, copy test.jar to the WEB_INF/lib directory of appa, start tomcat, visit / appa/test twice, then visit / appb/test. What will be output at this time?