Chapter 5 server Side Scripting Date: 01 October 2017
Outlines Server Side Scripting Java Technology Part I: Variables, constants, expression Part II: Functions & control structures Part III: Object Class Object, Class, Inheritance, Polymorphism, Abstraction, Encapsulation
SERVER SIDE SCRIPTING
Client-Server Side
Life Cycle of JSP
JAVA TECHNOLOGY
Variables, constants & expression public class FirstJava { public static void main(String[] args) { system.out.println(“My First JAVA Project"); }
What happens at compile time? JAVA Code Byte Code Compile Simple.java Simple.class
Variables in JAVA Types of variables Local variable Instance variable Static variable
Variables in JAVA class A{ int data=50; //instance variable static int m=100; //static variable void method(){ int n=90; //local variable } } //end of class
Data Types in JAVA Data Type Default Value Default Size boolean false 1 bit char ‘u\0000’ 2 byte byte 1 byte short int 4 byte long 0L 8 byte float 0.0f double 0.0d
Data Types in JAVA public class B { public static void main(String[] args){ int a=10; int b=10; int c=a+b; System.out.println(c); }
Operator in JAVA Operator in java is symbols to perform operation such as +, -, *, /, etc.
Operator in JAVA (Assignment) class OperatorExample{ public static void main(String[] args){ int a=10; int b=5; System.out.println(a+b); System.out.println(a-b); System.out.println(a*b); System.out.println(a/b); System.out.println(a%b); } }
Operator in JAVA (Relational) class OperatorExample { public static void main(String args[]) { int a=10; int b=5; int c=20; System.out.println(a<b&&a++<c);//false && true = false System.out.println(a);//10 because second condition is not checked System.out.println(a<b&a++<c);//false && true = false System.out.println(a);//11 because second condition is checked }
Part II: Function & Control Structure If statement If-else statement Nested if statement If-else-if ladder
If Statement Example:- public class Example1 { public static void main(String[] args) { int age=20; if(age>18){ System.out.print("Age is greater than 18"); } }
If-Else Statement Example:- public class Example2 { public static void main(String[] args) { int number=13; if(number%2==0){ System.out.println("even number"); } else { System.out.println("odd number"); } } }
Nested If Statement Example: public class Example3 { public static void main(String[] args) { int number=13; if(number%2==0){ System.out.println("even number"); } else if { System.out.println("odd number"); } else { System.out.println(“Error!!!”); } }
If-Else-If Statement Example:- public class Example4 { public static void main(String[] args) { int number=85; if(number>=80 && number<=100) { System.out.println(“Grade A"); } else if (number>=79 && number<=60) { System.out.println(“Grade B"); } else { System.out.println(“Error!!!”); } }
Part II: Function & Control Structure Switch For Loop While Loop Do While
Switch Example:- public class SwitchExample { public static void main(String[] args) { int number=20; switch(number){ case 10: System.out.println("10");break; case 20: System.out.println("20");break; case 30: System.out.println("30");break; default:System.out.println("Not in 10, 20 or 30"); } } }
For Loop Example:- public class LoopExample { public static void main(String[] args) { for(int i=1;i<=10;i++){ System.out.println(i); } } }
While Loop Example:- public class WhileExample { public static void main(String[] args) { int i=1; while(i<=10){ System.out.println(i); i++; } } }
Do-While Example public class DoWhileExample { public static void main(String[] args) { int i=1; do { System.out.println(i); i++; } while(i<=10); }
Part III: Object Class Object Class Inheritance Polymorphism Abstraction Encapsulation
Naming Conventions Naming conventios Name Convention class name Should start with uppercase letter and be a noun e.g. String, Color, Button, System, Thread etc. interface name Should start with uppercase letter and be an adjective e.g. Runnable, Remote, ActionListener etc. method name Should start with lowercase letter and be a verb e.g. actionPerformed(), main(), print(), println() etc. variable name Should start with lowercase letter e.g. firstName, orderNumber etc. package name Should be in lowercase letter e.g. java, lang, sql, util etc. constants name Should be in uppercase letter. e.g. RED, YELLOW, MAX_PRIORITY etc.
Object & Class Example 1:- class Student{ int id;//field or data member or instance variable String name; public static void main(String args[]) { Student s1=new Student();//creating an object of Student System.out.println(s1.id);//accessing member through reference variable System.out.println(s1.name); } }
Object & Class Example 2:- class Student{ int id; String name; } } class TestStudent3{ public static void main(String args[]){ Student s1=new Student(); // create objects Student s2=new Student(); // create objects s1.id=101; s1.name=“Lukaku"; s2.id=102; s2.name=“Lukamu"; System.out.println(s1.id+" "+s1.name); System.out.println(s2.id+" "+s2.name); }
Object & Class Example 3:- class Employee{ int id; String name; float salary; void insert(int i, String n, float s) { id=i; name=n; salary=s; } void display(){System.out.println(id+" "+name+" "+salary);} } public class TestEmployee { public static void main(String[] args) { Employee e1=new Employee(); e1.insert(101,"ajeet",45000); e1.display(); Example 3:-
To be continue.....