In this chapter and the subsequent many chapters, we will cover the JSP Technology in great detail.
So, lets get started!!!
Introduction to Java Server Pages:
Java Server Pages have become an integral part of any J2EE application. They are used extensively because they can combine the features of HTML and Java. The difficulty level of JSP is half-way between HTML and pure Java. For simple tasks like displaying the current date, you write a normal HTML page and add only a small amount of Java as a scriptlet. For big tasks like processing a shopping cart, you use JSP as the mediator between the Web form and a component (Ex: Servlet) that has all the processing logic.
CGI, Perl, Active Server Pages etc were all the predecessors of Java Server Pages. Am not saying that the JSP Technology was built based on these technologies but it is safe to say that, the JSP Technology was created to overcome many of the shortcomings in the above mentioned technologies. Though the ASP technology is a web server scripting champion and is used very widely, the only problem is the Runs only in Windows Attitude of the technology. Unlike ASP, JSP has equivalent if not better features and can run in any environment making it an invaluable tool for enterprise application developers who don't want to be tied to the limitation of the system running only in Windows.
Not only does JSP run on all major platforms, but the JavaBeans used by these JSPs run on all major platforms as well.
While it helps to know JSP's history, I won't bore you anymore with it. You have learnt as much as you need to know about the history of Java Server Pages and it is time to move on to the Exam Objectives…
How Does JSP Work?
If you remember the initial chapters in this series, you would remember the fact that JSP Pages get converted or rather Translated into Servlets before execution. Kudos to you if you remembered it. The JSP container parses the JSP source and converts the entire JSP page (HTML becomes strings and Java source code embedded in a JSP page gets copied into methods) into a Java servlet class. The HTML text is converted into a bunch of out.println statements in the order encountered. Finally, the container compiles this class into Java bytecodes. This Servlet is invoked by the Server in order to finish displaying the contents on the web browser.
I feel that it is always useful to take a look at the simple syntactical constructs of a technology before we dig deep into it. Well, whether you agree or disagree, below is a synopsis of the JSP syntax. You can use it to get a feel of what we are going to learn in the next few chapters…
|Syntax||What it Represents||Example|
|< ! -- comment -- >||HTML Comment||< ! -- This HTML comment is passed through to the client -- >|
|<%-- comment --%>||JSP Comment||<%-- This comment is ignored by the server --%>|
|<%@ page [key]="[value]" %>||Page Directive||<%@ page import="java.util.*" %>|
|<%! Declaration %>||Declaration||<%! String name = new String ("Rocky"); %>|
|<%= expression %>||Expression||Your shopping cart total is: <%= shoppingCart.getTotal() %>.|
|<% code %>||Scriptlet|| <% String password =request.getParameter("password");
if ( password == null)
Password is required, thank you.
<% } %>
|<%@ include file="file" %>||Static include, parsed at compile-time||<%@ include file="welcome.jsp>|
|< jsp : include page="file" / >||Dynamic include, request-time and not parsed||< jsp : include page="welcome.html" / >|
Previous Chapter: Chapter 30 - Servlet API
Next Chapter: Chapter 32 - Opening & Closing JSP Tags