BCIS 3680 Exam 2
Implementing Interface Syntax Example
- A class can implement 1 or more interfaces - An interface can be implemented by any # of class <access modifier> class ClassName [extends SuperClassName] implements InterfaceName1, [interfaceName2, ...]
Superclasses and Subclasses
- A superclass can have many subclasses - A subclass can can be the superclass of other subclasses - A subclass can inherit directly from only one Superclass - All classes in Java inherit from the Object class
Defining Interface Syntax Example
- Access modifier has to be public or none [public] interface InterfaceName { [public abstract] returnType1 methodName1 ([parameterList1]); [public abstract] returnType2 methodName2 ([parameterList2}); }
Interface
- An interface ONLY contains abstract methods - If a subclass wants to be a class that can be instantiated then it must implement all abstract methods in the abstract superclass
Dynamic Web Pages
- Certain contents do not exist before the page is requested - content is created upon request and related to what the user inputs Ex: current time on the web server - use JSP, server-side scripting language
Web Application
- Collection of files required to support a particular feature or task you want to publish to the outside world. - The set of files are stored in a folder, called "deployment folder"(possible subfolders)
To make it all connect
- Each alias you need to add a new <context> tag under <host> tag - Then define what the user sees(alias) as the path attribute of <context> tag - Set the value of docBase attribute to <context> tag to the deployment folder location.
Defining an Alias
- Edit the server.xml file, located in C:\Tomcat\conf\
HTML Elements(tag)
- Element(tag) consists of an opening tag(<h1>) and a closing tag (</h1>) - Defines either the structural role or formatting instruction for the portion of the text between the open/close tags Ex: <h1> for level-1 heading <b> for boldface
Static Variables(Data)
- Once it is defined in the class it comes into being immediately. (No instantiation) - Can be accessed from outside the class by ClassName.staticVarName - Values stored are not tied to any instance of the class
Static Methods(Action)
- Once static methods is defined in class it comes into being immediately(No instantiation) - Can be accessed outside the class by following ClassName.staticMethod()
Make an Abstract Class
- One abstract method=class is abstract - You cannot instantiate an object of an abstract class, BUT you can declare an object of that class Ex: accessModifier abstract class ClassName {//class body...} public abstract class student {...}
Rules for overriding
- Return type must be the same for both the overriding and overridden methods - Parameter list the exact same - Static methods CANNOT be overridden
Constructors
- Special method that is run when an object is created. Used to set initial(default) values for instance vars. - You can recognize a constructor because its declaration uses the same name as the class and it has no return type. public class Point { public int x = 0; public int y = 0; //constructor public Point(int a, int b) { x = a; y = b; } }
Instance Variables(Data)
- Store data specific to a particular object - Does not exist until an object of that class is created - Same class, same instance variables - Values stored in variables are unique to object
Abstract Method
- Subclass is responsible for fleshing out how the method works. The subclass implements(not overrides) the abstract method
Processing input with dynamic pages
- To process user input passed in a <form>, we need to write Java code - The dynamic contents are generated by Java code written as JSP elements - Goal of JSP is to generate the dynamic data and insert them into correct spots in static html
HTML
- Two main components of HTML page: Head and Body - Defines how text should be displayed on Web pages by using tags. - NOT case sensitive
Instance Methods(Action)
- Used to define actions that an object of the class is capable of performing - Has to have an object of that class to be called - Action works on data unique to object: When called on alice object, showStudentId() displays value 1010 When called on bob object, showStudentId() displays 1011
1) Request Object
- contains info about the request made by the client to the web server - Extract useful info
2) Response Object
- controls output the Web server sends to the client
Creating an Interface
- created by grouping these abstract methods in a special class - often contains names like -able or -er/-or
3) Out Object
- generates text that is being sent to the client - instance of javax.servlet.jsp.JspWriter - has println() and print() method - in every JSP page
Defining an Abstract Method
- include abstract keyword EX: accessModifier abstract returnType methodName( [parameter list] ); - No code/curly brackets
Exiting Tomcat
-in cmd - press ctrl+c - type "y"
Starting Tomcat
-open cmd -catalina run should see "INFO:Server startup in ### ms"
JavaServer Pages (JSP)
.jsp, not .htm/.html - JavaScript is client-side script - JSP is server-side script
Server-side scripting
1) Server processes user input, creates dynamic contents based on input, and writes the HTML code 2) Server sends code back to client 3) Clients browser displays the html code from server
Context for Web Applications: To make it all connect
<Host name .......> <Context path="/steinj03" docBase="D:\Whatup\HW\#2" /> - XML is CASE SENSITIVE
HTML Element Example (Hyperlinks)
<a href="http://sup.com"> Click Here </a> Each attribute is a name=value pair
HTML Form
<form> has 3 keyattributes 1) Name: Required if you have multiple forms on same page 2) Action: Specify the destination file where the form is processed 3) Method: Specify how to send form to Web server, either POST or GET
HTML Elements
<head> header <title> title of page <table> defines a table <tr> table row <th> column headings(bold) <td> Table data(cell) <ul> unordered list <li> List item(bullet) <p> paragraph <h#> Headings(# of 1-7) <hr> Horizontal bar <br> Line break
Checkbox
<input type="checkbox" name="java">Java</input>
Radio Button
<input type="radio" name="gender" value="M">Male</input>
Submit Button
<input type="submit" name="Submit" value="Submit thru Secure Server">
Textbox
<input type="text" name="firstName" />
Inheritance
A group of objects that share certain common traits while each group has some uniqueness of its own, then we may create a class hierarchy. Ex: Different types of vehicles
Setter Methods (Mutator)
A void method that performs an action(set value of an instance variable) - Takes a parameter Ex: public void setvarName (dataType paramName) Method Body: varName = paramName; public void setNumber (int number) { Number = number; }
Adding new members
Add new methods and/or modify methods defined in the superclass
What two things are required to create an object?
An object is created from a Class. 1) Declare an object reference for the new object, ClassName objRef; Ex: Student cobStudent; 2)Use the new keyword to actually create the object, objRef = new ClassName([arguments]); Ex: cobStudent = new Student ("ITDS", Undergrad");
Running JSP
Before JSP is run, behind the scenes the server converts JSP file into a servlet
JSP Declaration
Can use the declaration to define methods inside a JSP file Now JSP expressions can refer to variable names AND make method calls <%! %>
Create an Object
ClassName objRef = new ClassName ([arguments]); Ex: Student cobStudent = new Student ("ITDS", "Undergrad");
Subclasses
Common variables and methods defined in the superclass. Each subclass has its own unique traits as well as the shared ones
Superclass Constructors
Constructor in superclass initializes variables that are defined in the superclass(Constructors aren't inherited) - But a subclass may have its own Const.
Alias for Web App.
Create an alias(or context, or virtual directory) for the folder, so you don't show the path name to the deployment folder
Input Field
Defined using the <input> tag
Class
Defines a number of properties(variables) and actions/behaviors(methods) variables - store data methods - specify action
JSP Elements
Expression Scriptlet Declaration Directive
Superclass
General class that defines traits common to a set of related classes(subclasses)
Implementing an Interface
If a class implements an interface, it fleshes out the behaviors by implementing abstract methods contained in the interface Ex: A dog, a cat , and a parrot are very different species, but they all can be a "pet". How each can be a "pet" differs
Interface Example
If the interface PETABLE is implemented by DOG, then all the abstract methods must be implemented
Web Root
If you enter only the domain name you will get the default page in the root directory, and not anything further in the directory.
GET
Information is appended and it consists of a question mark, followed by name-value pairs in the format of fieldName=fieldValue
Folders for deployment
Inside the deployment folder, you create a subfolder called WEB-INF
Overloading vs Overriding
Look in PPT page 13 for diagram
How a Subclass modify a Superclass-defined method
May add to the behaviors(or change them) in the superclass version Called Overriding
Overloading
Method name stays the same, the parameter list changes 1) doSomething(int numberOne, double numberTwo) 2) doSomething(int numberOne)
JSP Scriptlet
More powerful than expressions. Create objects, do calculation, manipulate strings, run loops, etc... Must use println() or print() method to generate HTML code <% %>
POST
Nothing will be appended to the URL
Abstract Method
Place a method header in the superclass to define the behavior as a method at the superclass level, even though no code is possible
Polymorphism
Reference variable can refer to and be used to store objects of types different from its own, as long as those types are subclasses of its type Ex: Superclass - Student Subclass - Undergrad and Graduate So, Student s1 = new Student(); Student s2 = new Undergrad(); are legal
JSP Expression
Takes on value during web app execution, based on user input, date/time, etc... Incomplete Java statement Write dynamic portions only. Intermixed with HTML code <%= %>
JSP Directive
The page directive has an import attribute that is equivalent to the import statement in Java The include directive can be used to attach the same Header and Footer to a # of pages <%@ %>
this
The this keyword is used to refer to the object itself Public void setUnitPrice (double unit Price) { this.unitPrice = unitPrice; }
Overriding
The version in Subclass overrides the one defined in the superclass
Implicit Objects
Three objects created automatically so you don't have to worry about instantiating objects
Deployment Folder
Users don't see. Where web application files are actually located at
Getter Methods (Accessor)
Value-returning method that returns a value(the value of an instance variable) - No parameters Ex: public <dataTypeOfVar> get VarName() Method Body: return varName public int getNumber() { return Number; }
Client-side scripting
Want to validate the input using javascript You pass the info to the server
Alias Context
What a user will type or see in the URL
What is Method Overriding?
When a method in a subclass has the same return type and signature as a method in its superclass(identical headers), Then the method in subclass overwrites the method in superclass If overridden, method in subclass is run If not overridden, method in superclass is run
URL (Uniform Resource Locator)
http = protocol www.123.unt.edu = Server Address /itds = directory /index.php = file name http://www.123.unt.edu/itds /index.php
To use an Object
objectName.variableName; Ex: cobStudent.dept = "Marketing"; ObjectName.methodName ([arguments]); Ex: cobStudent.regClass("BCIS3680");
How to access a variable or method in the Superclass?
variable - super.variableName method - super.methodName([args]) constructor - super([args])