Today, Budi walks us through a refresher and brief overview of server JSP programming. Today's portion covers JavaServer Pages (JSP), with a thorough overview of JavaBeans and Tags. This excerpt comes from chapter one of JavaServer Faces Programming, by Budi Kurniawan (McGraw-Hill/Osborne, ISBN 0-07-222983-7, 2004).
Sun’s solution to this problem is JSP. According to Sun’s Web site, “JSP technology is an extension of the servlet technology created to support authoring of HTML and XML pages.” Combining fixed or static template data with dynamic content is easier with JSP. JSP makes development more rapid than using servlets alone because it allows HTML tags to intersperse with Java code. No compilation is necessary. The first time a JSP page is invoked, the servlet/JSP container compiles it automatically. The MyLongServlet in the previous example can be rewritten in a JSP page like this:
However, note that JSP did not make servlets obsolete. In fact, JSP pages and servlets coexist in many JavaWeb applications. And, bear in mind that JSP is an extension of servlets.
Inside the JSP container is a special servlet called the page compiler. The servlet container is configured to forward to this page compiler all HTTP requests with URLs that match the .jsp file extension. This page compiler turns a servlet container into a JSP container. When a JSP page is first called, the page compiler parses and compiles the JSP page into a servlet class. If the compilation is successful, the JSP servlet class is loaded into memory.
On subsequent calls, the servlet class for that JSP page is already in memory; however, it could have been updated. Therefore, the page compiler servlet will always compare the timestamp of the JSP servlet with the JSP page. If the JSP page is more current, recompilation is necessary.With this process, once deployed, JSP pages go through the time-consuming compilation process only once.
You may be thinking that after the deployment, the first user requests for a JSP page will experience unusually slow response, due to the time spent compiling the .jsp file into a JSP servlet. That scenario was anticipated, and to avoid this unpleasant situation, a mechanism in JSP allows the JSP pages to be precompiled before any user request for them is received. Alternatively, you can automatically load a JSP page by using the <load-on-startup> element under the <servlet> element in the deployment descriptor.
Remember: This is part two of the first chapter of JavaServer Faces Programming, by Budi Kurniawan (McGraw-Hill/Osborne, ISBN 0-07-222983). Stay tuned for more chapters of developer books from McGraw-Hill/Osborne. Buy this book!