J2EE Part I ( JSP/servlet )


Full Description
We will mainly cover JSP/servlet/JDBC in this course. Students will develop their
own applications using server-side Java.

Prerequisite:

Strong understanding of the Java Programming Language and basic HTML, familiarity with networking concepts, basic database concepts and Object-Oriented methodologies.

Software/Hardware requirements : PC with 486 or better processor. Internet connection through modem with reasonable speed/modem.

Access to an web server that can run JSP/servlet code. Access to JSP compiler and servletrunner are needed. Access to an IDE like Visual Café will be very helpful.

Access to a DBMS, the student wants to connect to can be useful but not necessary.


Outcomes:
On completion of this course, the student will be able to write basic JSP/servlet code independently. When complemented with the web-client( HTML page ) the student wrote, there will be a small but complete application developed all by the student.

Lesson1 :

Object Orientation


What is Object Orientation ?

In short, it is a way of viewing/modeling a real-world system as a system comprising of objects interacting with each other.

What is an object ?

An object has attributes and behavior, thus object resemble real-world entity.

Example : A person is an object who has specific attributes that describes the object. These attributes can be name, age, height, weight etc. A person can perform activities, some are common activities that almost all persons can do, like walking, talking etc.
Some are specific activities like painting, singing, programming etc. that only some can do.

In some cases one object may inherit attributes & behavior from another object just like children do from parents.

What are classes ?

Classes are templates for creating objects. They are used for creating objects like we use cookie cutters for creating cookies.

Example : A person class can be used many person objects and each object will have common characteristics of person.

What is Object-Oriented Programming ?

In pure Object-Oriented programming all data variables and functions/methods belong to some object, they are never used outside of an object and/or class. Data members of an object hold the information about object’s attribute( e.g. height, weight in person example ) and/or information about object’s current state( age ). Function/method members represent object’s behavior or the actions an object can perform( e.g. walk in the person example ). How this action is performed is internal to an object. Other objects can only ask an object to perform certain actions without knowing or telling it how to do it.

Class variables and members functions belong to a class and are usually shared by all the objects created from that class.

Java Basics

Here we will quickly recap some basic & useful features( in connection with learning JSP ) of the Java Programming Language. Detailed dicussion of the language is out of the scope of this particular course.

Java Data Types

Java supports following primitive( also used in procedural languages ) data types in addition to classes.
1. byte( signed 8 bit )
2. short( signed 16 bit )
3. char( unsigned 16 bit ) Unicode
4. int( signed 32 bit )
5. long( 64 bit )
6. float( 32 bit )
7. double(signed 64 bit)
8. All classes are inherited from a single class. This is called single-rooted hierarchy.
9. In special cases classes can contain data members only.
10. In special cases classes can contain member methods only.

Lexical Structure

Same as C or C++ or Javascript.
White space is ignored.
Two ways of commenting:
// Comment
/* Comment */
Escape sequences: \b - Backspace \t - Tab \n - Newline\f - Formfeed \r - Carriage return \” - Double quote\’ - Single quote \\ - Backslash

Reserved words & keywords

Java has a set of literals reserved as keywords or reserved words. Those words can not be used for naming variables or classes.

Declaring & initializing variables

For the most part it is the same as C or Javascript for primitive variables.

Operators & flow control

The operators work the same as C for primitive variables, same is true for operator precedence. Operators work a little differently with objects or classes but a detailed discussion of it is out of scope of discussion here. A very common usage of an operator in connection with classes is the use of that of the + operator with string classes. It concatenates 2 strings. Example:

String1 + String2 = String1String2

Reference variables

Reference variables refer to Object. It can be thought as pointer to an Object. However unlike C language working explicitly or manipulating pointers are NOT allowed in Java.
Same thing applies to classes.

Defining Classes

Classes will have methods & variables. Classes are usually grouped in packages. All the Java code should be inside a class & all classes should belong to a package. Four kinds of visibility modifiers are described in connection with classes below.

Public : A public class, method or variable can be accessed from anywhere.

Protected : A protected variable/method can be used only by classes in the same package or in a derived class in the same or a different package.

Package : Also called default visibility. If no modifier is used, a class, variable or method
can be used from anywhere within the same package.

Private : A variable or a method declared private can only be accessed from inside the same class, they belong to.

Instantiating objects

Objects can be created from a class using the new operator. Unlike C++, there is no delete operator in Java. Classes can not be deleted explicitly from inside of a program.
Java’s garbage collector will delete an object automatically when it detects that no reference is being made to it anymore. Example :

String String1 = new String();

Exception Handling

Java has exception & Error classes for handling programming errors. They are both derived from the class Throwable. An exception or error is said to be thrown where it occurs and is said to be caught where it is handled to continue execution. A programmer usually considers and deals with Exceptions, subclasses of Error usually handles fatal errors where the program needs to terminate.

A try statement is used to enclose a block of code where an exception may occur. A catch is used to say what needs to done if such an error occurs. Try & catch always need to be used in pair. Optionally a finally clause is used to follow a try-catch pair. It is executed regardless of an Exception occuring or not occuring.

Networking Concepts

Detailed discussion of the networking protocols are out of scope of this course. We want to quickly recap the basic features of the HTTP protocols used the delivery of the web pages, which mails consists of HTML.

It is very important to note that HTTP is a stateless protocol for communication between a client & a server. Here each request is an independent entity, a new connection needs to be opened for each request, unlike TCP/IP where a connection can be maintained for the entire life of a client session.

HTTP works this way :

A client opens a connection with the server( web server in this case ).

A client sends request to the web server requesting some kind of service. The request contains the HTTP request header which defines the type & length of the request data.

The web server will service the request. Then the server will send the response back.
The response will also contain a response header which will describe the type & length of the response data.

The connection gets closed.

Given that HTTP is stateless, connection can not be preserved between requests. If sharing of information is needed between sessions some specific techniques need to be used to overcome the statelessness of HTTP. A common example is using Cookies.

Assignment

Create an HTML page which includes a form. We should be needing to do some processing on the server on submission of the form. We will use it later to this processing on the server using a servlet.
























Assessment:
There will be quizzes at the end of each week. 50% of the final grade will be based on the final assignment where the students will build a small application.


Week 1
We will review all the basic concepts needed for this course : OO, networking, database
and Java.

We will also start building our HTML pages that we will use for adding our JSP/servlet code to.

Week 2
We will start learning what are JSP/servlets. Why do we need them ? How do they compare with other technologies that we can use for similar purposes ( CGI etc. ). We will discuss servlet life cycle, servlet engines and JSP compilers. We will also discuss when we prefer JSP and when we prefer servlets.
Week 3
We will write our first JSP, add it to the HTML page we already have, compile it, run it and debug it if needed.

We will discuss tag libraries, how to create custom tag libraries.

Week 4
We will write our first servlet code. Use it with our HTML front page, run it, test it and debug it if needed.

We will discuss configs and contexts in connection with servlets.

Week 5
We will briefly discuss database connectivity with Java. We will discuss driver types, loading, connecting , result set, prepared statements, callable statements and also how to use JDBC with servlets.

Contact Hours: 20