I am wondering does JSP ever get compiled ? The reasons why that I am asking is because whenever I deploy my Java EE application on the web server I only see those servlet and beans class file in the WEB-INF folder as they are compiled, but not that JSP, so how does it work, and what's the logical flow and the big picture of the normal request/response cycle.
-
4*"does JSP ever get compiled ?"* Yes, when first accessed. – Andrew Thompson May 15 '12 at 19:23
-
4@BhavikAmbani, please do not post comments to the question asking people to read your answer. That's entirely unnecessary. – Kirk Woll May 15 '12 at 19:27
-
1The jsp wiki here on SO will give you a good idea of the lifecycle of the JSP http://stackoverflow.com/tags/jsp/info – Sean May 15 '12 at 19:30
-
1You're basically asking a question which is already answered in chapter 1 every self-respected book/tutorial about JSP. I recommend you to go get one so that you at least show research effort :) – BalusC May 15 '12 at 19:50
4 Answers
Basically:
In your servlet container, the JSP servlet is mapped to any URL that ends in
.jsp
(usually)When one of those
.jsp
URLs is requested, the request goes to the JSP servlet. Then, this servlet checks if the JSP is already compiled.If the JSP is not compiled yet, the JSP servlet translates the JSP to some Java source code implementing the
Servlet
interface. Then it compiles this Java source code to a.class
file. This.class
file usually is located somewhere in the servlet container's work directory for the application.Once the JSP servlet has compiled the servlet class from the JSP source code, it just forwards the request to this servlet class.
The thing is, unless you specifically precompile your JSP, all this happens at runtime, and hidden in the servlet container's work directory, so it is "invisible". Also have in mind that this is what happens "conceptually", several optimizations are possible in this workflow.

- 21,974
- 5
- 38
- 51
-
Seems like someone is engaging in a fusilade of revenge D/V'ing on all the answers right now. Anyway, +1, this is a nice, complete answer. – Kirk Woll May 15 '12 at 19:44
-
-
2The generated java file should be visible under `work\Catalina\localhost\ProjectName\jspFile.java` – Aniket Thakur Apr 03 '15 at 12:49
Yes, they are compiled!
Older compilers even produced java and class files.
Looks like with newer compilers ( at least starting with Sun JDK 6 update 30 ), they can do all the byte-code generation in-memory, so you do not see any traces in your application work
or temp
directories.

- 44,836
- 10
- 105
- 121
-
1Not sure why, but I find your last statement dubious. Do you have any references for this? Are you sure that you're not confusing with the fact that the average IDE takes over the server's work/temp folders somewhere in the depths of the workspace when developing and running JSP inside an IDE? – BalusC May 15 '12 at 19:39
-
@BalusC. No, because I was surprised not to find `.class` files in my `work` directory in production with tomcat 7.0.25. Doing some debugging session with Jasper confirmed the last statement. – Alexander Pogrebnyak May 15 '12 at 19:53
-
Interesting. But Tomcat doesn't use the JDK to compile JSPs. Tomcat 7 uses Eclipse JDT for this. Tomcat 6 and older uses Ant JSPC for this. – BalusC May 15 '12 at 20:01
JSPs compile to Servlets. They just have a special compiler that is typically embedded in the container.
From good ole wikipedia:
Architecturally, JSP may be viewed as a high-level abstraction of Java servlets. JSPs are translated into servlets at runtime; each JSP's servlet is cached and re-used until the original JSP is modified.
At run time, the JSP code is interpreted by the
JSP compiler, which parses out all the special features in the JSP code and translates them to Java
code. The Java class created from each JSP implements Servlet
. Then, the Java code goes through
the same cycle it normally does. Still at run time, it is compiled into bytecode and then into machine
code. Finally, the JSP-turned-Servlet responds to requests like any other Servlet.
If you look at the *_jsp.java file, what you should find is a class that extends org.apache.jasper.runtime.HttpJspBase
. This
abstract class extends HttpServlet
. HttpJspBase
provides some base
functionality that will be used by all JSPs that Tomcat compiles, and when your JSP is executed,
ultimately the service method on that Servlet is executed, which eventually executes the
_jspService method.
If you inspect the _jspService method, you’ll find a series of method calls writing your HTML to
the output stream. This code should look very familiar to you because it’s not that different from
the Java code that you replaced with this JSP.
Of course, the JSP Servlet class does not look the same on every web container. The org.apache.jasper classes, for example, are Tomcat-specific classes. Your JSP compiles differently on each different web container you run it on. The important point is that there is a standard specification for the behavior and syntax of JSPs, and as long as the web containers you use are compliant with the specification, your JSPs should run the same on all of them, even if the Java code they get translated into looks completely different.
Taken (and paraphrased) from Professional Java for Web Applications by Nicholas S Williams

- 6,025
- 5
- 31
- 88