ADVANCED JAVA
MODULE 4-- Part 2
Java Server Pages (JSP)
VENKATESH BHAT
Senior Associate Professor
CSE Department
AIET, Moodbidri
2.
SYLLABUS – MODULE4 – Servlets and JSP
Servlets:
• Background; The life cycle of a servlet;
• A simple servlet;
• the servlet API;
• The javax.servlet package Reading servlet parameter;
• The javax.servlet.http package;
• Handling HTTP Requests and Responses;
• Using Cookies;
• Session Tracking,
Java Server Pages (JSP):
• Introduction, JSP tags, Variables and Objects, Methods, Control statements, Loops, Request
String, Parsing other information, User sessions, Cookies, Session Objects
Textbook 1: Chapter 31 (Servlets)
Textbook 2: Chapter 11 (JSP)
3.
Java Server Pages(JSP)
• JSP is a server-side programming technology that enables the creation of
dynamic, platform-independent method for building Web-based applications.
• JSP have access to the entire family of Java APIs, including the JDBC API to access
enterprise databases.
• JSP is a technology for developing Webpages that supports dynamic content. This
helps developers insert java code in HTML pages by making use of special JSP
tags, most of which start with <% and end with %>.
• A JSP component is a type of Java servlet that is designed to fulfill the role of a
user interface for a Java web application. Web developers write JSPs as text files
that combine HTML or XHTML code, XML elements, and embedded JSP actions
and commands.
• Using JSP, we can collect input from users through Webpage forms, present
records from a database or another source, and create Web pages dynamically.
4.
JSP Lifecycle
• JSPlife cycle is defined as the process from its creation till the destruction. This is
similar to a servlet life cycle with an additional step which is required to compile a JSP
into servlet.
• There are three methods that are automatically called when a JSP is requested and
when the JSP terminates normally. These are jspInt(), jspDestroy(), and
service() method.
• The jspInt() method is identical to the init() method in a Java servlet and in
an applet. The jspInt() method is called first when the JSP is requested and is
used to initialize objects and variables that are used throughout the life of the JSP.
• The jspDestroy() method is identical to the destroy() method in a Java
servlet. This method is automatically called when the JSP terminates normally.
• The service() method is automatically called and retrieves a connection to HTTP.
5.
SYLLABUS – MODULE4 – Servlets and JSP
Servlets:
• Background; The life cycle of a servlet;
• A simple servlet;
• the servlet API;
• The javax.servlet package Reading servlet parameter;
• The javax.servlet.http package;
• Handling HTTP Requests and Responses;
• Using Cookies;
• Session Tracking,
Java Server Pages (JSP):
• Introduction, JSP tags, Variables and Objects, Methods, Control statements, Loops, Request
String, Parsing other information, User sessions, Cookies, Session Objects
Textbook 1: Chapter 31 (Servlets)
Textbook 2: Chapter 11 (JSP)
6.
JSP Tags
• AJSP program consists a combination of HTML tags and JSP tags.
• JSP tags define Java code that is to be executed before the output of the JSP
program is sent to the browser.
• A JSP tag begins with <%, which is followed by java code and ends with %>.
• XML version of the JSP Tags are formatted as <jsp:TagID></jsp:TagID>
• Example:
<jsp:expression>
expression
</jsp:expression>
7.
Steps involved inthe process
when the JSP program is called by the browser
• Tomcat reads the JSP program
• Resolves JSP Tags
• Sends the HTML Tags and related information to the browser.
• Java code associated with JSP program is executed when
encountered by TomCat and send the result to the browser.
Browser knows how to display the display the result because
JSP tags enclosed within an open and closed HTML Tag.
8.
Five Types ofJSP Tags
• Comment tag: Text comments are enclosed within <%-- and --%>
• Declaration statement tags: <%! declaration; [ declaration; ]+ ... %>
• Example:
<%! int i = 0; %>
<%! int a, b, c; %>
<%! Circle a = new Circle(2.0); %>
• Directive tags: <%@ directive attribute="value" %>
• Three types: import, include and taglib
• Examples:
• <%@ page import = “import java.sql.*”; %>
• Defines page-dependent attributes, such as scripting language,
error page, and buffering requirements.
• <%@ include file = “keoghbooks.html” %>
• <%@include file="includes/header.jsp" %>
• <%@include file="includes/footer.jsp" %>
• Includes a file during the translation phase.
• <%@ taglib url = “myTags,tld.sql.*” %>
• Declares a tag library, containing custom actions, used in the
page
• Expression tags: <%= expression %>
• Example: <p>Today's date: <%= (new java.util.Date()).toLocaleString()%></p>
• Scriptlet tags: used to execute java source code in JSP.
9.
SYLLABUS – MODULE4 – Servlets and JSP
Servlets:
• Background; The life cycle of a servlet;
• A simple servlet;
• the servlet API;
• The javax.servlet package Reading servlet parameter;
• The javax.servlet.http package;
• Handling HTTP Requests and Responses;
• Using Cookies;
• Session Tracking,
Java Server Pages (JSP):
• Introduction, JSP tags, Variables and Objects, Methods, Control statements, Loops, Request
String, Parsing other information, User sessions, Cookies, Session Objects
Textbook 1: Chapter 31 (Servlets)
Textbook 2: Chapter 11 (JSP)
10.
Variables and Objects
•We can declare java variables and objects that are used in a JSP Program.
• Example 1: Declaring a Single Variable
• Example 2: Declaring Multiple Variables within single JSP Tag
• Example 3: Declaring the Objects and Arrays within the single JSP Tag
11.
Example 1: Declaringa Single Variable
<HTML>
<HEAD>
<TITLE> JDP Pramming </TITLE>
</HEAD>
<BODY>
<%! int age = 29; %>
<P> Your Age in the next year is: <%= age %> </P>
</BODY>
</HTML>
Any Java Declaration statement can be used in a JSP tag.
12.
Example 2: DeclaringMultiple Variables within single JSP Tag
<HTML>
<HEAD>
<TITLE> JSP PROGRAMMING </TITLE>
</HEAD>
<BODY>
<%!
int age = 40;
float salary;
int empNumber
%>
</BODY>
</HTML>
13.
Example 3: Declaringthe Objects and Arrays within the single JSP Tag
<HTML>
<HEAD>
<Title>JSP PROGRAMMING</TITLE>
</HEAD>
<BODY>
<%! String Name;
String [] Telephone = {“2940583921”, “7958473281”, “9848586932”};
String Company = new String();
Vector Assignment = new Vector();
int [] Gade = {100, 83. 93}
%>
</BODY>
</HTML>
14.
SYLLABUS – MODULE4 – Servlets and JSP
Servlets:
• Background; The life cycle of a servlet;
• A simple servlet;
• the servlet API;
• The javax.servlet package Reading servlet parameter;
• The javax.servlet.http package;
• Handling HTTP Requests and Responses;
• Using Cookies;
• Session Tracking,
Java Server Pages (JSP):
• Introduction, JSP tags, Variables and Objects, Methods, Control statements, Loops, Request
String, Parsing other information, User sessions, Cookies, Session Objects
Textbook 1: Chapter 31 (Servlets)
Textbook 2: Chapter 11 (JSP)
15.
Methods
• For MethodDefinition, use the same tag as in the variable declaration <%! … %>
• For Method call, use the same tag is in the expression <%= … %>
• Example:
<HTML>
<HEAD>
<TITLE> JSP Method Declaration </TITLE>
</HEAD>
<BODY>
<%! int sum(int a, int b)
{
return a + b;
}
%>
<P> SUM IS: <%= sum(10, 30) %> </P>
</BODY>
</HTML>
16.
Overloaded Methods
<HTML>
<HEAD>
<TITLE> JSPMethod Declaration </TITLE>
</HEAD>
<BODY>
<%! int sum(int a, int b)
{
return a + b;
}
int sum(int a)
{
return a + 10;
}
%>
<P> SUM IS: <%= sum(10, 30) %> </P>
<P> SUM IS: <%= sum(10) %> </P>
</BODY>
</HTML>
17.
SYLLABUS – MODULE4 – Servlets and JSP
Servlets:
• Background; The life cycle of a servlet;
• A simple servlet;
• the servlet API;
• The javax.servlet package Reading servlet parameter;
• The javax.servlet.http package;
• Handling HTTP Requests and Responses;
• Using Cookies;
• Session Tracking,
Java Server Pages (JSP):
• Introduction, JSP tags, Variables and Objects, Methods, Control statements, Loops, Request
String, Parsing other information, User sessions, Cookies, Session Objects
Textbook 1: Chapter 31 (Servlets)
Textbook 2: Chapter 11 (JSP)
18.
Control Statements
•It isused to change the flow if JSP Program
•Two Types of Control Statements:
•If statement
•Switch statement
19.
If Statement inJSP
• If Statement evaluates a condition statement to determine if one or
more lines of code are to be executed or to be skipped.
• Example:
<HTML>
<HEAD>
<TITLE> JSP if Statement </TITLE>
</HEAD>
<BODY>
<%! int average = 70; %>
<% if(agerage > 50 { %>
<P> YOU PASSED </P>
<% } else { %>
<P> Better Luck Next Time </P>
<% } %>
</BODY>
</HTML>
20.
switch Statement inJSP
• Switch Statement compares the a value with one or more other values
associated with a case statement.
• The code segment associated with matching case statement is executed.
Code segments associated with other case values are ignored.
• Example:
<HTML>
<HEAD>
<TITLE> JSP switch Statement </TITLE>
</HEAD>
<BODY>
<%! int day = 1; %>
<% switch (age) {
case 1: %>
<P> It is Sunday </P>
<% break;
case 2: %>
<P> It is Monday </P>
<% break; %>
</BODY>
</HTML>
21.
switch Statement inJSP
• Second style of the same Example:
<HTML>
<HEAD>
<TITLE> JSP switch Statement </TITLE>
</HEAD>
<BODY>
<%! int day = 1; %>
<% switch (age) {
case 1:
System.out.println(“It is Sunday”);
break;
case 2:
System.out.println(“It is Monday”);
break; %>
</BODY>
</HTML>
22.
SYLLABUS – MODULE4 – Servlets and JSP
Servlets:
• Background; The life cycle of a servlet;
• A simple servlet;
• the servlet API;
• The javax.servlet package Reading servlet parameter;
• The javax.servlet.http package;
• Handling HTTP Requests and Responses;
• Using Cookies;
• Session Tracking,
Java Server Pages (JSP):
• Introduction, JSP tags, Variables and Objects, Methods, Control statements, Loops, Request
String, Parsing other information, User sessions, Cookies, Session Objects
Textbook 1: Chapter 31 (Servlets)
Textbook 2: Chapter 11 (JSP)
23.
Loops in JSP
•Wecan use three basic types of looping blocks in Java:
for, while, and do…while blocks in your JSP programming.
•For Loop Example :
<HTML>
<HEAD>
<TITLE> JSP for Statement </TITLE>
</HEAD>
<BODY>
<%! int i; %>
<%
for(i=0; i<10; i++) {
system.out.println(“i = ” + i);
} %>
</BODY>
</HTML>
24.
SYLLABUS – MODULE4 – Servlets and JSP
Servlets:
• Background; The life cycle of a servlet;
• A simple servlet;
• the servlet API;
• The javax.servlet package Reading servlet parameter;
• The javax.servlet.http package;
• Handling HTTP Requests and Responses;
• Using Cookies;
• Session Tracking,
Java Server Pages (JSP):
• Introduction, JSP tags, Variables and Objects, Methods, Control statements, Loops,
Request String, Parsing other information, User sessions, Cookies, Session Objects
Textbook 1: Chapter 31 (Servlets)
Textbook 2: Chapter 11 (JSP)
25.
Requesting String inJSP
• Assume that query string in the URL is as shown below:
http: //www.aiet.com/jsp/myprogram.jsp?fname = “Venkatesh”&lname = “Bhat”
• To extract fname and lname, the JSP code is:
<%! String myFirstName = request.getParameter(fname);
String myLastName = request.getParameter(lname);
%>
• Other Methods
• getParameterValues() method is used to return multiple values.
• getParameterNames() method returns an enumeration of String
objects that contains the field names in the request string. Using
enumeration extracting methods, we can copy the field names to variables
within our program.
26.
SYLLABUS – MODULE4 – Servlets and JSP
Servlets:
• Background; The life cycle of a servlet;
• A simple servlet;
• the servlet API;
• The javax.servlet package Reading servlet parameter;
• The javax.servlet.http package;
• Handling HTTP Requests and Responses;
• Using Cookies;
• Session Tracking,
Java Server Pages (JSP):
• Introduction, JSP tags, Variables and Objects, Methods, Control statements, Loops,
Request String, Parsing other information, User sessions, Cookies, Session Objects
Textbook 1: Chapter 31 (Servlets)
Textbook 2: Chapter 11 (JSP)