JSP and ClassLoader

Posted on 2002-06-21
Last Modified: 2010-04-01
Explain how class loaders work with JSP, and how is that different than servlets.

Can you have a JSP application that is nearly static, with many thousands of megabytes of JSP pages?

How come it is possible to replace a JSP page with a fresher version, and the servlet behind the JSP page gets reloaded. But it doesn't work the same way with a servlet. In the case of a JSP, only the one JSP page is recompiled, and users do not loose their sessions. In the case of servlets, if it works at all, replacing the servlet will restart the entire web application.
Question by:mjzalewski
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +1
LVL 19

Expert Comment

ID: 7099398
This is due to implementation of various application servers.

App Servers will do "page translation".  When a web container recieives a request for JSP for the first time or when restarted (some application servers allow you to precompile JSPs).  Most app servers out there will perform "page translation" every time the JSP is modified.

Some application servers load servlets into memory when they start due to performance reasons.  Some app servers auto-restart when a servlet class file is replaced and others require a manual restart.  You can setup your session appropriately you will not lose sessions btw restarts.

for any replacements of JARs or WARs.  A restart is required.

I am not sure if this specifically answers your questions but hopefully has shed some light on things.

LVL 19

Expert Comment

ID: 7099405
I ran into this info about Tomcat:

"Turn on Servlet Reloading To tell Tomcat to check the modification dates of the class files of requested servlets and reload ones that have changed since they were loaded into the server's memory. This degrades performance in deployment situations, so is turned off by default. However, if you fail to turn it on for your development server, you'll have to restart the server every time you recompile a servlet that has already been loaded into the server's memory. To turn on servlet reloading, edit install_dir/conf/server.xml and add a DefaultContext subelement to the main Service element and supply true for the reloadable attribute. The easiest way to do this is to find the following comment: <!-- Define properties for each web application. This is only needed if you want to set non-default properties, or have web application document roots in places other than the virtual host's appBase directory. --> and insert the following line just below it: <DefaultContext reloadable="true"/> Be sure to make a backup copy of server.xml before making the above change."

This is supposedly true for WARs and JARs too.


Author Comment

ID: 7099493
OK let me rephrase the question.

How is it possible to have a large number of JSP pages (hundreds of megabytes) in a web application? Why doesn't the JVM run out of memory, because each JSP page must be compiled into a servlet, and servlets don't normally unload once started.

My second, probably related question is:

When you replace a .jsp file with a newer version, the application server recompiles the .jsp and runs it as a new servlet. You can also replace a servlet .class file, or a .jar file used by the servlet, and the new version of the servlet can be reloaded automatically. But when this happens, *everything* is reloaded, including all actively running web sessions, and all the other servlets in the web application. Why is it different?

Part of the answer to the first question might be because JSP pages compile into servlets that read the static parts from a file. So the servlet .class file is actually much smaller than the original JSP page.

But I am almost certain that is not the whole anser. Something different happens when a JSP page reloads. Each JSP page can be reloaded without affecting the other ones. The same is not true of a servlet. Changing the .class file for a servlet causes the entire web application to reload.

It must be that there is a seperate class loader for JSP than for servlets. Probably a seperate instance of a class loader for each JSP page. That way, the Class object for the JSP servlet can be released from memory when the JSP servlet is no longer in use.

And if this analysis is true, there is no limit on the number of JSP pages you can have in an application. So if this is true, the answer to my question is:

You can have as many JSP pages as you want, because the inactive JSP Class objects will be unloaded. The Application server can unload JSP servlet Class objects because each one is loaded with a seperate instance of a ClassLoader.

And the answer to my second question might be:

JSP pages can be replaced in a running application because each JSP has a different instance of a ClassLoader. When the application server needs to make a new JSP servlet, it makes a new ClassLoader and releases the old one. Servlets are loaded with a single instance of a ClassLoader. So the only way to reload a servlet is to release the ClassLoader, which causes all of the other servlet classes to be reloaded.

Not sure if any of the above is correct, or even if it makes sense.
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 92

Expert Comment

ID: 7099744
LVL 27

Expert Comment

ID: 7099754
LVL 92

Accepted Solution

objects earned 50 total points
ID: 7099756

Author Comment

ID: 7108131
The section on USING CLASS LOADERS FOR HOT DEPLOYMENT was approximately what I was looking for. I guess I was looking for a more complete explanation of how JSP pages in particular use class loaders.

But this information allows me to connect the dots. Thanks for the reference!
LVL 92

Expert Comment

ID: 7109529
No worries.
I'd guess the details of how it's handled for jsp may depend on which container you are using.
Brainbench MVP for Java 1

Featured Post

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
maven project in eclipse 11 60
J2SE 5.0 metadata 1 67
TreeSet comparator example 7 105
login form jsp example 2 55
In order to fulfill our mission of inspiring learning in the technology community, Experts Exchange is launching a Course of the Month program. Premium and Team Account members will have access to one course per month as a part of their membership, …
We have put together a white paper that aims to explain how MSPs can both improve their offering and ease the pain of after-hours service by: -Suggesting changes to workflow -Indicating how to rework policy to suit your team -Providing ConnectW…
In a recent question ( here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question