• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How exactly JSP page converts into servlet at run time?

 
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How JSP converts into servlet at run time? Can some one explains me this?

As how scriplet part will store inside servlet? How functions will get store? How jsp's other body part will get store? And how they will work at run time?

Please explain me this, if you have idea.
 
Saloon Keeper
Posts: 27752
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's not converted, it's compiled.

In fact, in some webapp servers, like Tomcat, it's compiled twice.

Tomcat has a component called JasSPer, which is the JSP compiler. When a page request for a JSP is made, Tomcat checks its local cache (in the TOMCAT_HOME/work) directory) to see if there's an executable copy of the JSP there. If not, it runs JaSPer on the JSP source code found in the WAR to produce a temporary Java source code file.  This file defines a servlet that outputs the text parts of the JSP, invokes the infrastructure needed to deal with bean references, JSP tags and so forth, and outputs the text-only parts of the JSP. That's the first "compile" or, translation, if you prefer.

This temporary file is then compiled by the "javac" compiler to produce an ordinary Java class file for that servlet. The class file is then added to the compiled JSP cache and to the classpath of the webapp so that Tomcat can invoke it just like it would do for any other servlet.

This need for the java compiler, incidentally is why Tomcat needs to run under a JDK and not a JRE. The javac program is supplied as part of the JDK, but not as part of the corresponding JRE.

Other webapp servers may do this slightly differently, but the end result is the same.


The idea of taking source code in one language, translating it to another language, then compiling the translation into machine language is not a new idea. In fact, quite a few C and Fortran compilers actually produced assembly-language files to be compiled. And the original AT&T C++ "compiler" compiled C++ to C.
 
SunilK Chauhan
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your quick response.

So, here it compiles JSP page into servlet code finally and than after tomcat run it.

Now, is all functions and other body portion of JSP Page be the methods inside Servlet after this compilation done?

Because in servlet, as we know that there are methods ultimately and we are calling it. So, how these all JSP page functions and body portion will get store inside servlet? As a method only?
 
Tim Holloway
Saloon Keeper
Posts: 27752
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not sure if I completely understood that question.

But the servlet that gets generated when a JSP is compiled has lots of different types of code in it. Some JSP constructs are converted directly to servlet code. For example, the "c:forEach" JSTL tag. Some, such as custom tag references, may get converted into calls into the tag's implementation class. Scriptlets get copied directly (but the Bear will growl at you). The HTML parts get translated to response.print() method calls that copy out the HTML source text.

It's not easy to read, because it's not intended for humans to look at, but everything does go to its defined place, which is what is important.
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For a conceptual overview see this article. Anything beyond that is really up to the JSP container to deal with. Each JSP container will create different implementation code, so getting into the details of any single container isn't really useful general knowledge.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic