Showing posts with label jsps. Show all posts
Showing posts with label jsps. Show all posts

Tuesday, April 12, 2011

Quick Recap: Chapters 31 to 37

Let us quickly go through what we learnt in the previous few chapters about the JSP Technology.

Introduction to JSPs

• JSPs are an integral part of any J2EE Application and are used extensively
• CGI, Perl and Active Server Pages (ASP) are all predecessors of the JSP Technology
• A JSP page gets converted/translated into a Servlet before it is executed
• You can have the following in a JSP Page
     o HTML Code
     o Java Code
     o XML Code
     o JSP Scriptlets

JSP Tags:

• JSP Tags begin with a “<” symbol and end with a “>” symbol
• There are many directives that can be used in a JSP Page
• The include directive is a placeholder that lets you include the text in another file
• The page directive gives directions to the servlet engine about the general setup of the page
• The scriptlet is a way to include Java code directly in a JSP page
• JSP Tags can be used in two ways. One is the regular JSP Tag construct and the other is the XML Equivalent.

The Page Directive:

• The Page Directive is one of the important components of any JSP Page. It can help us define page specific properties like Buffer size or location of an error page etc
• In JSP, you do not have to use the page directive for the default import list of java.lang.*, javax.servlet.*, javax.servlet.jsp.*, and javax.servlet.http.*. These are automatically available to you
• The page directive can be used to define an error page to which the control will be re-directed in case there is an issue with the current page
• The page directive also defines if the current page is an error page that would be invoked by any other JSP page

JSP Lifecycle:

• Just like Servlets, JSPs too have a life cycle
• The following are the life cycle stages of a JSP Page
     o Page translation
     o Page compilation
     o Load class
     o Create instance
     o Call jspinit
     o Call _jspService
     o Call jspDestroy


JSP Implicit Objects:

In any JSP Page, there are a bunch of implicit objects that are available for the programmer to use. It contains a variety of information that can be used to display stuff on the page. The following Implicit Objects are available for the programmer inside the JSP Page
• request
• response
• out
• session
• config
• application
• page
• pageContext
• exception

Key Terms Learnt in the previous Chapters:
• JSP
• servlet
• directive
• expression
• scriptlet
• implicit object
• JSP container
• JSP page lifecycle
• tag library
• Web application

Previous Chapter: Chapter 37 - JSP Scriptlets

Next Chapter: Self Test - Chapters 31 to 37

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

Monday, February 28, 2011

Chapter 1: Servlet & JSP History

Here we are, studying for the SCWCD exam. I would like to congratulate you again for this decision of yours because of which you are going to get yourself SCWCD certified. It's a big step and am gonna be with you step by step to help you get this certification.

Well, it wouldn't be much fun preparing for a certification on JSPs and Servlets without knowing their history. Would it?

This chapter is going to be a history lesson taking you to the humble beginnings of these two wonderful technologies that have made our lives so much more easier & powerful.

How it all Began – Internet

Early in the 1950’s computer scientists in USA were working their backsides off in order to compete with Soviet Unions (The late USSR) advancements in superpower computing. They formed the Advanced Research Projects Agency (ARPA) in the year 1957. In those they still had powerful computers, but they weren’t able to talk or communicate with one another. In 1966 Lawrence G. Roberts (From MIT) proposed the first computer network which was named the ARPANET. The US Department of Defense (DoD) funded the venture and it took them 3 years to implement the network. The ARPANET team rewarded the DoD by establishing the Network Control Protocol (NCP), the first host to host protocol, which made possible for the university and the research center PC’s to communicate with one another.

With the success of the NCP, telco major AT&T installed the first cross country link between UCLA and BBN. It was a humble beginning and by 1973 hundreds of computers were talking to one another.

The real big breakthrough came in the year 1982 when the TCP/IP standard was established by Vint Cerf and Bob Kahn. Based on this development, the Domain Name System was established and by 1984 there were over 1000 hosts registered.

This was the backbone of the current day Internet. It was called NSFNET originally and with multiplying hosts it was becoming difficult to manage. By 1991 there were over a hundred thousand hosts and the system was getting out of control. There was nobody incharge and there was utter chaos all around.

In 1991, Tim Berners Lee created hyperlinks. He invented the whole protocol that made links communicate with one another and the World Wide Web was born. Telnet, email and many other services started using the networks.

In 1993, Marc Anderson and his friends wanted to see what was on the Internet, so they developed a new program called the NCSA Mosaic at the University of Illinois based on Berners Lee’s ideas. (NCSA stands for National Center for Supercomputing Applications)

Mosaic was the catalyst that caused the internet to explode. Nearly 200 million hosts were in use by the end of the decade and more than 1 billion users were using it.

This was not the end of it. Mobile phones, PDAs, GPS, Cars etc started connecting to the internet and the number of users began growing beyond numbers that we can write down or calculate.

It all started with basic HTML pages and hungry scientists created more and more advanced technologies whose powers were unbelievable. JSPs and Servlets just changed the landscape catastrophically and here we are, studying them to become better J2EE web programmers!!!

History of JSP & Servlets

The Internet's original purpose was to access and copy files from one computer to another. While TCP/IP provided a highway layer, the File Transfer Protocol (FTP) specification provided a standard way to exchange those files. It defined a way of shuttling them back and forth, but said nothing about looking at the content. HyperText Markup Language (HTML) allowed us to see the documents on the Internet. FTP can transmit HTML files just as easily as HTTP can. But we use Hypertext Transfer Protocol (HTTP) to act as an FTP specifically for HTML documents because it is a stateless protocol which makes having many short connections more efficient.
HTTP is the plumbing that connects the various computers. Now it is time to discuss about the fluid that flows through it “JSP & Servlets”

Note: JSP & Servlets arent the only technologies that are used in J2EE applications. Struts, Hibernate, Springs etc are other technologies that are used in J2EE Web applications. But, don't worry about them because they arent in the exam.

Using HTTP and HTML people were able to view/browse files and contents on a remote server. This is very useful, but people wanted live data. This is where the CGI (Common Gateway Interface) specification helped us. It helped us connect to databases and display stuff on the fly. The CGI specification was a major breakthrough in Web Application Development. The CGI standards made sure that the same CGI program worked on different Web servers.

CGI became the bread and butter of web developers. It was the most common means of displaying dynamic content on the internet. Though it was good, it wasn't good enough. It was not able to handle the performance requirements of the bursting Internet users. It was literally too much for it.

If you are asking me why CGI couldn't handle the load, the answer is simple. CGI spawned a separate process for every request that it receives. This design was able to sustain during off-peak hours but ate off server resources during peak loads which was eventually too much for it.
With growing numbers of users of web applications, scalability became a key consideration which wasn't CGI’s Middle Name and hence people started exploring other options.

Many CGI derivatives came up as server-side programming solutions that implement business logic, including ASP, ColdFusion, PHP, and Perl. Java surpassed them all due to portability and its object oriented programming design.

Alas, he we are, learning JSPs and Servlets that are the children of the Java Family which make our lives all the more easier in the world of Web Development.

Java was conceptualized in 1991 but it wasn't in the internet programming world until 1997. Servlets were the alternative to CGI and were released in 1997. Unlike CGI, which starts a process for each request, Servlets just spawn a new thread. Servlets had a better or rather efficient architecture which was able to handle the loads of the internet.

Though Servlets were awesome when compared to CGI, they still had some issues when it came to displaying dynamic content on a web page. Thankfully, Sun released the JSP (Java Server Pages) specifications in 1998, which solved all our UI woes. JSPs enabled programmers to display dynamic HTML content that could also use Java features. The combination of JSPs and Servlets was just what the Doctor Prescribed and it just revolutionized the Web Programming industry.
That's it for the history lesson. Now we are all set to dive deep into the world of magical Servlets and JSPs.

Previous Chapter: Introduction to the SCWCD Exam

Next Chapter: Web Servers & Servlet Containers
© 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