Tuesday, February 01, 2005

Well.., sacrificing movies on last Saturday and Sunday, managed to parse web.xml and load the mapped classes. ;)

Tomcat uses some six to seven conditions to check
whether a class requested can be loaded or not.

First , it checks in the repository before checking the mappings in web.xml. Then in the Cache.

Still some more conditions apply ...

If it does not find anywhere then it throws a
ClassNotFoundException

That is why the class could be loaded (sometimes!!) even when u have deleted the class. It could be found in cache. But once you restart the server, it'll throw the exception.

But aim (as i have been telling this for long time!!) being just to understand How Tomcat Works , i came up with only two conditions. ;)

One being the repository search or just the folder in which class is present and is well-known like /WEB-INF/classes/*.

And other being mapped in web.xml.

This is because you can have a simple Servlet in classes directory (without packages!!) which is looked after by the first condition and also you can have a servlet mapped as a controller (or anything for that matter) like:

servlet-mapping servletName = "com.mycontroller.classes.MyMainServletController" uriPattern="/servlet/MyController"

If both these conditions fail, then throw the exception.

Tomcat uses neither DOM nor SAX for parsing of the web.xml file. It uses Apache Software Foundations' (ASF) Digester project.

Look for digester here:
http://jakarta.apache.org/commons/digester/

It basically maps XML to Java objects and allows certain rules to be set.

Learnt Digester (the simple reason is i didnt know either of DOM or SAX ) ;)

You might figure out that the mappings in Struts-config.xml or web.xml like
or the or the tags are all
Classes !!

Digester parses the xml file and allows you to trigger a method on finding a certain pattern.

Example:

On finding the pattern
, you can invoke a method after assigning it to a class

digester.addSetProperties("employee","Employee");
digester.addCallMethod("employee","printName");

This will call the method printName() of the class Employee on finding pattern "employee".

Very usefull indeed !! (like all the jakarta projects) ;)

But exploration of powers of Digester was limited, should utilize it to the maximum extent (but when ??!!).

Next thing to learn is Ant (Another Neat Tool). May be i should try to compile classes when the server is restarted or when the class is modified.

Surprisingly this was my original aim !!! Ya to compile and reload the classes.

We'll see what'll happen !!

Thanks for support again!