javaserver pages syntax harry richard erwin, phd cse301/cit304

25
JavaServer Pages Syntax Harry Richard Erwin, PhD CSE301/CIT304

Upload: aileen-phillips

Post on 26-Dec-2015

220 views

Category:

Documents


0 download

TRANSCRIPT

JavaServer Pages Syntax

Harry Richard Erwin, PhD

CSE301/CIT304

Outline

• Resources

• Java Basics

• Operators in Java

• Java Syntax

• Method Calls

• Wrapping it up

Resources

• Arnold and Gosling, 1998, Java Programming Language, second edition, Addison-Wesley.

• Farley, Crawford, and Flanagan, 2002, Java Enterprise in a Nutshell, second edition, O’Reilly, ISBN: 0-596-00152-5. For programmers.

• Bergsten, 2002, JavaServer Pages, second edition, O’Reilly, ISBN: 0-596-00317-X. Covers JSP 1.2, for both programmers and web page authors. Has lots of worked examples. Recommended.

Scope

• This presentation is not intended to make you a Java programmer.

• If you will be doing a good deal of JavaServer Pages scripting, you should learn Java.

• Remember, however, scripting is often unnecessary.

Java Basics

• Unicode

• Identifiers and Names

• Primitive Types

• Reference Types

Unicode

• Java is written in the Unicode character set. Java programs can also be written in ASCII or Latin-1 characters, which most text editors and web page development programs support.

• Unicode characters are stored in 16 bits and can represent almost all written languages.– A– å– π

• Unicode can be used anywhere.

Identifiers

• These begin with a letter, underscore (_), or currency symbol (€$¥£).

• Contain any number of letters, numbers, underscores, and currency symbols.

• Avoid the currency symbols as compilers use them.

• Remember Unicode. The following are legal identifiers:– π– Ö

Scope of Names

• The scope of the declaration of a name is the region of the program within which the entity can be referred to using a simple name.

• Class names must be imported (or declared) to be in scope.

• Members (attributes and methods) have class or interface scope.

• Method parameters have method scope.

• Local variables have block scope.

• When a name is hidden by another name, you must provide a full name, not a simple name.

Primitive Data Types

• boolean (true or false)• char (16-bit)• short• byte (8-bit)• int• long• float• double

‘Horses for Courses’

• Primitive types lack methods and cannot be stored in collections that expect some sort of object.

• Each primitive type has a corresponding class (with useful methods) that provides instances that can be stored in a collection.

• Boolean—boolean• Character—char• Byte—byte• Short—short• Integer—int• Long—long• Float—float• Double—double

Type Conversions

• Integer to floating point are automatic.

• char to/from integer and floating point are supported

• Widening conversions (e.g., short to long) are safe.

• Narrowing conversions are dangerous. They lose information.

Pointers

• There are no programmer-accessible pointers in Java. Yeah!

• Classes and arrays in Java are called reference types. Java manages the underlying pointers.

• There is no way to convert from a reference to a primitive object.

• You cannot use a reference to access a private member attribute.

Classes and Arrays

• These are called reference types• Created using the new operator

– new type(args); // an object– new type[dim]; // 1-D array– new type[dim1][dim2] etc; //larger arrays

• A class or array object is null (does not exist) until it is initialized.

• Writing new classes is Java programming. Plan on using existing Java classes instead.

Creating an Object

• ClassName obj;– Declares that the name obj refers to an obj of class

ClassName in the current scope.– Does not create that object. Until the object is created,

obj refers to null (e.g., nothing).– To create the object, execute the following:

• obj = new ClassName(argument list);– Note that the constructor for ClassName may be

overloaded.– ‘argument list’ is a list of arguments.

Using an Object or Class

• To refer to the member fields and methods of an object, use the following syntax:– obj.field;– obj.method(args);

• To refer to class level (‘static’) fields and methods, use the following syntax:– ClassName.field;– ClassName.method(args);

String

• Contains Unicode text• Constant—unlike C and C++ strings.• A class, not a primitive type• Literals consist of anything between a pair of

double quotes. To include a double quote in a string, use \”

• Supports operator overloading (+, +=). This is the only place in Java where operator overloading takes place.

Copying and Comparing Reference Types

• The pointer is copied, not the data. Hence the same data are used.

• Comparisons (e.g., ==, !=, <, >, etc.) of most reference types are done by comparing pointers. Hence two different objects containing the same field values are not equal.

• Comparisons of primitive types and Strings are based on value.

Java Operators

• Generally as in C or C++.

• Typecasting is handled by (type)var;

Java Operator List

• +=, ++, -=, --, etc., as in C and C++

• != not-equals-test• == equals-test• <, >, <=, >=• [num] for array access• ! means ‘not’• = assignment

• +, - , *, /, % arithmetic• && logical and• || logical or• ?: conditional• . used for member

access• () used in function or

method calls

Java Syntax

• Variables may be declared anywhere. The variable name is in scope in the local block from the point of declaration.

• Reference types are set to null (non-existent) until they are given a value. Primitive types have a default value, typically zero or false.

Statements

• Generally as in C/C++• Statements are terminated by ‘;’• Blocks are delimited by { }, may substitute

for individual statements.• Statements that you are unlikely to use:

– synchronized (threads)– throw (an exception)– try/catch/finally (to handle exceptions)

Common Statements

• import brings a Java class into scope• if (logical test) then statement; else statement;• switch is used to handle multiple cases• for(init;logical test;final) statement; is a for loop• do statement while(expression); is a do loop• while (logical test) statement; is a while loop• break; is used to exit a loop immediately• continue; goes to the end of the loop• return retval; exits a function or method

Method Calls

• Look like functions, but are always associated with a class or object.

• Defined by a signature:– Method name– Arguments– Return type (differs from C++, may be void)– Checked exceptions thrown (not of much concern here)– Method modifiers: public, static, abstract, final, native,

private, protected, synchronized. (you usually use public methods)

Using JSP Scripting

• You don’t need to write classes or methods.• If you need to use a temporary variable, declare it

in a <%…%> element.• If you need to display a variable, use a <%=…%>

element.• If you need to use Java syntax (if then else, loops,

switch perhaps, use <%…%> elements to hide it from the HTML.

• If you need to call Java classes, you will need to import them.

Conclusions• Java is available if you need it, particularly for

something that an action element (see previous lecture) can’t handle or prototyping.

• Use it like any scripting language. You won’t usually need to write classes or methods.

• Use the block structure to control the HTML generation and any logic.

• Variables give you flexibility in displaying information.• JDBC can be used like ODBC.• That’s all you need folks!