Solved

JSP and ClassLoader

Posted on 2002-06-21
8
438 Views
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.
0
Comment
Question by:mjzalewski
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 19

Expert Comment

by:cheekycj
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.

CJ
0
 
LVL 19

Expert Comment

by:cheekycj
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.

CJ
0
 
LVL 3

Author Comment

by:mjzalewski
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.
0
 
LVL 92

Expert Comment

by:objects
ID: 7099744
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 27

Expert Comment

by:rrz
ID: 7099754
0
 
LVL 92

Accepted Solution

by:
objects earned 50 total points
ID: 7099756
0
 
LVL 3

Author Comment

by:mjzalewski
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!
0
 
LVL 92

Expert Comment

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


http://www.objects.com.au/staff/mick
Brainbench MVP for Java 1
http://www.brainbench.com
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
In  today’s increasingly digital world, managed service providers (MSPs) fight for their customers’ attention, looking for ways to make them stay and purchase more services. One way to encourage that behavior is to develop a dependable brand of prod…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

705 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now