Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does jsp work?

Tags:

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.

like image 913
peter Avatar asked May 15 '12 19:05

peter


People also ask

How does JSP work with HTML?

A simple JSP page (. jsp) consists of HTML markup embedded with JSP tags. When the file is processed on the server, the HTML is rendered as the application view, a web page. The embedded JSP tags will be used to call server-side code and data.

How is a JSP file executed?

The JSP container, therefore, is executed by a servlet container. (Servlet containers are summarized in "Servlet Containers".) A JSP container may be incorporated into a Web server if the Web server is written in Java, or the container may be otherwise associated with and used by the Web server.

How does JSP interact with Java?

JSP pages use XML tags and scriptlets written in the Java programming language to encapsulate the logic that generates the content for the page. It passes any formatting (HTML or XML) tags directly back to the response page. In this way, JSP pages separate the page logic from its design and display.

How does JSP life cycle work?

Instantiation(Object of the generated Servlet is created) Initialization(jspInit() method is invoked by the container) Request processing(_jspService()is invoked by the container) JSP Cleanup (jspDestroy() method is invoked by the container)


2 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.

like image 171
gpeche Avatar answered Oct 08 '22 20:10

gpeche


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.

like image 45
Alexander Pogrebnyak Avatar answered Oct 08 '22 19:10

Alexander Pogrebnyak