Wednesday, April 6, 2011

Chapter 31: Introduction to JSP

JSP and Servlets have greatly simplified the life of a J2EE programmer who builds Java based Enterprise Applications for a living. As part of the series of articles on the SCWCD Certification, we have already covered the Servlets in great detail. Next in line is the Java Server Pages Technology also called the JSP Technology

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.

JSP competes directly with ASP. In fact, you would be forgiven if you thought it was a copy. Sun took the same approach to JSP as it did with Java. Sun borrowed the syntax of its best competitor (ASP for JSP and C++ for Java) tweaked it a little, but built everything under the hood from scratch. Java syntax comes from C++, but it works on all platforms with no portability issues like C++ for the developer. Similarly, the JSP structure comes from ASP. The look and feel, and even some syntax is the same. However, ASP primarily uses Microsoft's versatile VBScript, while JSP uses the more powerful and portable Java and JavaScript.

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.

JSP Syntax:

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

No comments:

Post a Comment

© 2013 by www.inheritingjava.blogspot.com. All rights reserved. No part of this blog or its contents may be reproduced or transmitted in any form or by any means, electronic, mechanical, photocopying, recording, or otherwise, without prior written permission of the Author.

ShareThis

Google+ Followers

Followers