Sunday, July 13, 2008

Different between servlet and JSP

I have done a little experiment about jsp and servlet. In the first time, I thought both files will be compiled and run in the same way. But in fact, both files are treated different by Tomcat. I have never come across on this little experiment before.

I did 3 experiments. In the first experiment, I went through the following steps:

  1. Create jsp a very simple jsp file. It only contains the html tag.
  2. Startup tomcat.
The above experiment come with the result that there is a file created by Tomcat. The filename is tldCache.ser. I am not really sure on this file, but I found some source saying that this file is used by Tomcat as a cache for tld tag defined in jsp. Thus, Tomcat doesnt read or scan the tag every time the request is meant to the jsp file.

The second experiment, I went through the following steps:
  1. Create a simple jsp file which contains html tag and several java operation (which is looping something). The filename is test.jsp
  2. startup Tomcat.
The result of this experiment is Tomcat produces java file and class file. The files are located in /work/. The files which are produced by Tomcat, are test_jsp.java and test_jsp.class. The thing that I can get from the second experiment is, Tomcat produces java file which extends org.apache.jasper.runtime.HttpJspBase. It is completely different from what I had thought before.

In the third experiment, I went through the following steps:
  1. Create a very simple java class extends HttpServlet.
  2. Compile it to be ja va class.
  3. Create directory /work//WEB-INF/classes.
  4. Create mapping file.
  5. Startup Tomcat.
In the last experiment, I didnt see any files created by Tomcat. Which means that Tomcat will use it in memory directly without creating any files.

My bad that I didnt come across this experiment before. I have gone through the things that is too far such as playing around with spring and webwork but never think on this basic thing. I hope my experiments will be useful for all of the visitor.

No comments: