Presentation is loading. Please wait.

Presentation is loading. Please wait.

JavaServer Pages: Introduction to JSP Instructor: Enoch E. Damson

Similar presentations


Presentation on theme: "JavaServer Pages: Introduction to JSP Instructor: Enoch E. Damson"— Presentation transcript:

1 JavaServer Pages: Introduction to JSP Instructor: Enoch E. Damson
The University of Akron Summit College Dept of Business Technology Computer Information Systems JavaServer Pages: Introduction to JSP Instructor: Enoch E. Damson

2 Three-tier Client/Server Architecture
Introduction to JSP

3 Client-side Scripts and Browser Dependency
Client-side scripts are executed in the client browser The browser must provide an appropriate interpreter All browsers come with built-in engines to support certain client-side scripting languages Introduction to JSP

4 JavaScript and VBScript
There are two main client-side scripting languages JavaScript VBScript JavaScript is more widely supported than VBScript Introduction to JSP

5 VBScript Example <HTML>
<HEAD><TITLE> Client-side Scripting</TITLE></HEAD> <BODY> This page was last modified on <script language="VBScript"> document.write document.lastModified </script> </BODY> </HTML> Introduction to JSP

6 JavaScript Example <HTML>
<HEAD><TITLE> Client-side Scripting </TITLE></HEAD> <BODY> This page was last modified on <script language="JavaScript"> document.write(document.lastModified) </script> </BODY> </HTML> Introduction to JSP

7 Client-side Scripting Using JavaScript
The main disadvantage of client-side scripting is that it is browser dependent Most browsers support JavaScript JavaScript tends to be the language of choice for writing client-side scripts Introduction to JSP

8 Client-side Scripting Using JavaScript
Opening script tag <script language="JavaScript"> document.write("Hello World!") </script> Closing tag Introduction to JSP

9 Events Handlers Introduction to JSP

10 An Example: Event Handler
<HTML> <HEAD><TITLE>Event handler</TITLE> </HEAD> <BODY> <a href="#" onMouseOver="alert('Mouse over')"> Please move mouse over me </a> </BODY> </HTML> Introduction to JSP

11 Server-side Scripting: JSP
Server-side scripts are executed on the Web server The server must be equipped with an engine that can interpret corresponding scripting code For example, in order to run JSP, the server must be equipped with an engine that can interpret JSP scripts Independent of Browser: Since the Web server sends the results (the execution of server-side script) of server-side scripts back to the client browser as regular HTML content, server-side scripts have nothing to do with the client browser. That means that the server-side scripts will work with any browser Introduction to JSP

12 JSP Example The following JSP script:
<%= new java.util.Date() %> Generate output to client browser similar to: Thu Jan 30 20:02:01 EST 2003 Introduction to JSP

13 JSP versus HTML JSP can perform the following tasks:
Connect to and manipulate a database. Create pages that can display things which will be of interest to a particular user. Collect data from users and return information to a visitor based on the data collected. Modify the content of a Web page, by updating a text file or the contents of a database rather than the HTML code itself. Access file systems via the Internet so that you can read, write, and update files. Utilize extensive Java Applications Programming Interface. Introduction to JSP

14 A Web Server and JSP A Web server and a JSP engine software needs to be installed on your computer to enable you to write and test JSP code Any Web server that supports JSP can be used The Apache Tomcat Web server is a good choice for JSP scripting beginners Download from: The Java Software Development Kit (Java SE) needs to be installed prior to installing the Apache Tomcat Server Download from: Introduction to JSP

15 Installing the Java Software Development Kit
It is assumed that you are using Windows 2000 Professional, Windows XP Professional, or Windows Vista Locate and download the software which is located at: Choose the “Java SE” link under “Downloads” Download the “JDK 6 update x” Follow the default instructions to install the Java JDK to your computer Introduction to JSP

16 Installing Apache Tomcat
It is assumed that you are using Windows 2000 Professional, Windows XP Professional, or Windows Vista Locate and download the software which is located at: Choose the “Tomcat 6.x” link under “Downloads” Download any of the “Binary Distributions” Preferably the “Windows Service Installer” under “Core” Follow the instructions to install the Apache Tomcat server to your computer By default, Tomcat will be installed in the directory: C:\Program Files\Apache Software Foundation\Tomcat 6.0 Introduction to JSP

17 Testing Apache Tomcat Server Installation and Configuration
Once you have installed and configured the Tomcat server and your Web application, you should check to make sure that it works correctly Start the Tomcat server if it is not started Open a browser window and enter the following URL in the address box: 8080 being the default port number used for the Apache Tomcat Server during installation Most web servers You should see the Tomcat default page displayed Introduction to JSP

18 Executing JSP Scripts By default, your JSP scripts must be saved at: C:\Program Files\Apache Software Foundation\Tomcat 6.0\webapps\ROOT (which is the default location for publishing files on the apache Tomcat server) You test your scripts by opening a browser window and entering the following URL in the address box: Introduction to JSP

19 Collecting Information
A form serves as a container that holds controls such as text fields, labels, buttons, and images Form tags: <FORM NAME="formName" ACTION="getUserName.jsp" METHOD="POST"> <!--form elements go inside of form tags --> </FORM> Introduction to JSP

20 Form Attributes Name: Action: METHOD:
is required if you want form controls processed on the client side; otherwise, it is optional Action: Specify the destination Web page where the form is processed METHOD: Specify how to send form to the Web server, either POST or GET Introduction to JSP

21 Control Element in a Form
The input fields on a form are called controls and are usually (but not always) created with the <INPUT> tag <FORM NAME="frmName" ACTION="getUserName.jsp" METHOD="POST"> Name: <INPUT TYPE = "TEXT" NAME="lastName"> </FORM> Introduction to JSP

22 Control Elements’ Attributes
TYPE: Specifies the input type NAME: Each control in a form must have a NAME. The data entered by the user is retrieved by referencing the name of its control field. Without a name, the data stored in the control field cannot be retrieved VALUE: A default value may be assigned to a control element Introduction to JSP

23 Form Elements Example <FORM NAME="frmName" ACTION="getUserName.jsp" METHOD="POST"> Your First Name:<INPUT TYPE = "TEXT" NAME="firstName"> </FORM> Introduction to JSP

24 Submitting Form for Processing
In order to process form data, you need to submit the form to the server, and then retrieve and process data on the server-side To submit a form, the submit() method of the form must be called Using Submit button Explicitly call the submit method of a form Introduction to JSP

25 Using Submit Button <FORM NAME="frmName" ACTION="getUserName.jsp" METHOD="POST"> Your First Name:<INPUT TYPE = "TEXT" NAME="firstName"><br> <INPUT TYPE=“SUBMIT” VALUE="Submit"> <INPUT TYPE=“RESET” VALUE="Reset"> </FORM> Introduction to JSP

26 GET Method Form data is appended to the end of the designated URL after a question mark The URL is the one specified as the ACTION value in a form If you do not explicitly set the METHOD attribute for a form, by default the form is sent using the GET method Introduction to JSP

27 POST Method When you use the POST method, the form element data is sent to the server as a transaction message body Unlike the GET method, the POST method does not append form element data to the URL Note that when the POST method is used, you can generate a query string by attaching a queryString to the URL directly Introduction to JSP

28 Retrieve Data Stored in a Control Element
request.getParameter(“elementName”) Example: request.getParameter(“major”); request.getParameter(“balance”); Introduction to JSP

29 Retrieve Form Data <BODY>
Hello, <%= request.getParameter("myName") %>: here is your message displayed using your preferences:<br> </BODY> Introduction to JSP

30 Output to the user <%= “Your message goes here” %>
<% out.println(“Your message goes here”); %> Unlike the first output method: <%= %>, with the out.print method, you can put many statements within a single pair of <% %> Introduction to JSP

31 Storing Form Information
There are many situations in which you will need to store form information and use it later in your JSP scripts. Like all programming languages, JSP uses variables to temporarily store information A variable is a location in computer memory where a value is stored for use by a program In JSP script, all variables must be declared before you can use them After a variable has been declared, a value can be stored in the variable, and this value can be used throughout your JSP page Introduction to JSP

32 Using Variables <% String major = request.getParameter(“major”);
out.println(major); %> <%= major %> Introduction to JSP

33 Content Comments <!– content comments -->
Sent back the client via response output stream They are not displayed on the browser The same syntax as comments in HTML: <!– content comments --> Introduction to JSP

34 Server-side Comments They are ignored at translation time
They are not sent to clients <%-- multiple line comments -- %> <% /* Multiple line comments */ %> <% //single line comment %> Introduction to JSP

35 Variables A variable is a location in the computer's memory where a data value is stored, or a location that references to another location where an actual object resides There are two types of data types in JSP: primitive data types and classes For primitive data types, the data value is stored in the location specified by the variable name; for classes, the actual data value is stored somewhere in memory and the data value is reference by using the variable name Introduction to JSP

36 Variable Declaration dataType variableName; int x, y; char a;
String s1; Introduction to JSP

37 Assignment String s1; s1= “Hi, there.”;
You can declare and initialize a variable in one step: String s2 =“Hi, there.”; Introduction to JSP

38 Naming Variables A variable name is any valid identifier
An identifier is a series of characters, consisting of letters, digits, and underscores, that does not begin with a digit. JSP script is case sensitive—uppercase and lowercase letters are different, so varname and VARNAME are different identifiers A variable name can be any length; all of the following are valid identifiers:        String s; String aLongVariableNameButStillValid; int an_integer_variable_name; Variable names cannot contain spaces or dashes Introduction to JSP

39 Primitive Data Types A data type describes the information that a variable stores. For example, int variables store integers, or whole numbers A variable’s data type also determines how many bytes of memory are required to store that variable Each data type has a range of values. Memory space is allocated to store each variable according to its data type JSP provides eight primitive data types. Six of them are numeric; one is character, used for characters in Unicode encoding; and one is Boolean, used for true/false values Introduction to JSP

40 Numerical Data Types Name Range Storage Requirement byte short int
long float double -27 to 27 -1 -215 to -231 to -263 to -3.4E38 to 3.4E38 -1.7E308 to 1.7E308 1 byte 2 bytes 4 bytes 8 bytes Introduction to JSP

41 Character Data Type The character data type is used to represent a single character. Unlike the string type, a character value is enclosed within single quotation marks. For example, consider the following code: [           char letter = 'A'; char numChar = '6'; Introduction to JSP

42 Boolean Data Type The Boolean data type has two values, true and false
It is used for logical testing using the relational operators Boolean values are integral part of control structures such as if then statements, while loops, and for loops Introduction to JSP

43 Arithmetic Operations
operator Algebraic expression JSP expression Addition + x + y Subtraction - x – y x - y Multiplication * x * y Division / x / y Modulus % x mod y x % y Introduction to JSP

44 Comparison Operators Operator Example Meaning == != > >= <
<= x == y x != y x > y x >= y x < y x <= y x is equal to y x is not equal to y x is greater than y x is greater than or equal to y x is less than y x is less than or equal to y Introduction to JSP

45 Conditional Statements
if (condition) { statement(s); }         for example: <% if (yourGrade >= 60) { out.println("passed"); %> Introduction to JSP

46 Conditional Statements
if (condition) { statement(s); }else{ }         for example: <% if (yourGrade >= 60) { out.println("passed"); out.println("failed"); } %> Introduction to JSP

47 For loop for (initialize-control-variable; condition-testing; modifying-condition){ statements; } Example: <% for(int i = 0; i<10; i++){ %> Welcome to JSP<br> <% } %> Introduction to JSP

48 While loop while (condition-testing){ statements; } Example:
int i = 0; While(i < 10){ Out.println("Welcome to JSP<br>"); i--; Introduction to JSP

49 Do loop do{ statements; } while(condition-testing); Example:
<% int counter = 1; do { %> <%= counter %> <br> <% }while (++counter <=10); %> Introduction to JSP

50 Switch Structure switch (switch-expression){ case value1: statements1;
break; case value2: statements2; case valueN: statementsN; default: statements-for-default-case; } Introduction to JSP

51 Logical Operators Operator Meaning ! && || ^ Logical NOT Logical AND
Logical OR Logical exclusive Introduction to JSP


Download ppt "JavaServer Pages: Introduction to JSP Instructor: Enoch E. Damson"

Similar presentations


Ads by Google