This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Tomcat and the fly likes Tomcat 7 handling of JSP Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Products » Tomcat
Bookmark "Tomcat 7 handling of JSP" Watch "Tomcat 7 handling of JSP" New topic
Author

Tomcat 7 handling of JSP

Johnnie Smith
Greenhorn

Joined: May 02, 2012
Posts: 22
Hello I have a query regarding the JSP life cycle.

I want to know what happens when a request for a JSP, say HelloWorld.jsp, is submitted to the server. Does the server look for the HelloWorld.jsp file first to see whether it is actually present ?

Or does it look for the HelloWorld_jsp.class file first and then check whether a newer version of the HelloWorld.jsp exists?

Book by Mike McGrath (JSP in Easy Steps) says that it first looks for the JSP source file i.e. HelloWorld.jsp. If it is not found it will return an error response saying that the requested resource is not found.

But book by Phil Hanna (JSP Complete Reference) does not say anything about looking for the source file first. It merely says that the server looks for the

class file (i.e. servlet with the name HelloWorld_jsp.class). If it is not found, iIt will translate the JSP into servlet, compile the servlet and so on.

I require the answer for an academic reason. Thanks in advance.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60752
    
  65

Please read this article.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Johnnie Smith
Greenhorn

Joined: May 02, 2012
Posts: 22
Dear Sir, Thank you for the reply. I have gone through the article. But it still does nto answer my question. In the article it is written:

"The JSP Engine receives the request for the JSP page and first checks to see if the JSP has been referenced before."

When it says 'referenced' does it mean that the JSP engine searches for the class file first?

OR

Does the JSP engine look for the source JSP file to determine whether it is present on the server, in the first place?

I am asking this question because the two books mentioned by me says 2 different things as mentioned in my previous post.


Thank You.

Stefan Evans
Bartender

Joined: Jul 06, 2005
Posts: 1016
After a cursory search, the Servlet/JSP Specifications don't seem to have anything to say on the matter.
I don't think there is any one answer to this question: it is an implementation detail on the server.
Some servers might implement these checks, others might not.

Tomcats implementation is to map all *.jsp requests to a JSP servlet. That servlet would have the logic about checking whether a JSP exists or not.
If you pre-compile your JSPs, and add entries to the web.xml file as a result, then it just goes directly to the compiled servlet from the JSP, and doesn't even touch the JSP Servlet.

So in the case of pre-compiled JSPs you can get a response back even if the JSP file no longer exists.
For any more details I think you would have to dive into the source code.

Johnnie Smith
Greenhorn

Joined: May 02, 2012
Posts: 22
Thank You Mr. Evans.

I too had gone through the JSP/Servlet specifications as well as Apache documentation and did not manage to find anything there.

Yes. I will have to go though the source code of Tomcat.

Regards,
John.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60752
    
  65

Johnnie Smith wrote:Does the JSP engine look for the source JSP file to determine whether it is present on the server, in the first place?

If it did not, how could it check to see if it's been updated or not?
Johnnie Smith
Greenhorn

Joined: May 02, 2012
Posts: 22
Bear Read my query carefully.

When it says 'referenced' does it mean that the JSP engine searches for the class file first?

OR

Does the JSP engine look for the source JSP file to determine whether it is present on the server, in the first place?

I want to know what occurs first. Search of the class file or determination of whether the JSP source file is present on the server, in the first place.

I know that it will look for the JSP source to check its timestamp with the timestamp of the class file.

But if it looks for the JSP source in the first place and finds that it is not there then it wont have to look for the class file at all.

Currently no documentation is available (even on Tomcat site) of what should occur first. So I will have to through the code of Tomcat server itself.

Dont be in a hurry to answer the question. Do read it carefully and follow the discussion.

John.






Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60752
    
  65

I read it very carefully. And my answer remains.

But if you want to know exactly how Tomcat does it, you'll need to look at the source code.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15951
    
  19

Take this as a general guideline. As Bear said, the Tomcat code itself is the final authority, although reading the manual doesn't hurt.

For performance reasons, Tomcat is going to attempt to locate the corresponding class for a JSP and execute it. That saves the overhead of compiling and conforms to the general idea of caching JSP code. If the class file isn't in the cache, then Tomcat will invoke the JSP compiler to digest the JSP into java code (as a servlet), compile that code into a class, which is then placed into the JSP class cache.

If you change the JSP source, Tomcat will detect the change and invalidate (delete) the class file. To the best of my knowledge, the JSP recompilation doesn't occur until the next time the JSP is actually requested (using the caching scheme mentioned above), but the difference between immediate recompilation is in transitory performance, not functionality, so only worry about it if you absolutely must. There may be a Tomcat configuration option to control this.

Changing the Java file that's generated from the JSP source and then compiled to produce the JSP class voids the warranty. Don't even think about it. Always make your changes to the JSP source, never to the generated Java or class files.


Note that JSP source updates will only be detected and applied if the webapp has Tomcat's WAR change scanner switched on. It's switched on by default, but if someone switches it off, changes will not be automatically detected.

In current releases of Tomcat, the JSP class cache is a subtree in the TOMCAT work directory, but of course this is just an implementation detail and therefore subject to change. For best results, however, you should delete this subtree when you deploy a new copy of the WAR just to make sure that no out-of-date JSP classfiles remain.


Customer surveys are for companies who didn't pay proper attention to begin with.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Tomcat 7 handling of JSP
 
Similar Threads
Websphere: Where to find to translated JSP (_jsp.java)?
JSP Without going to server
Anchor target in dispatcher forwards
Running HelloWorld JSP on weblogic8.1 ????
need help