Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "JavaServer Pages Syntax Harry Richard Erwin, PhD CSE301/CIT304."— Presentation transcript:

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

2 Outline Resources Java Basics Operators in Java Java Syntax Method Calls Wrapping it up

3 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.

4 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.

5 Java Basics Unicode Identifiers and Names Primitive Types Reference Types

6 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.

7 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: –π –Ö

8 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.

9 Primitive Data Types boolean (true or false) char (16-bit) short byte (8-bit) int long float double

10 ‘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

11 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.

12 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.

13 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.

14 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.

15 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);

16 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.

17 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.

18 Java Operators Generally as in C or C++. Typecasting is handled by (type)var;

19 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

20 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.

21 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)

22 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

23 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)

24 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.

25 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!


Download ppt "JavaServer Pages Syntax Harry Richard Erwin, PhD CSE301/CIT304."

Similar presentations


Ads by Google