SlideShare a Scribd company logo
2
Most read
5
Most read
22
Most read
WEB TECHNOLOGIES JSP
Dr R Jegadeesan Prof-CSE
Jyothishmathi Institute of Technology and Science,
karimnagar
SYLLABUS
2
UNIT- IV JSP
The Anatomy of a JSP Page, JSP Processing, Declarations,
Directives, Expressions, Code Snippets, implicit objects,
Using Beans in JSP Pages, Using Cookies and session for
session tracking, connecting to database in JSP.
UNIT-IV : Introduction to JSP
Aim & Objective :
➢ Java server pages is a technology for developing web pages that
include dynamic content.
➢A JSP page can change its output based on variable items, identity of the
user, the browsers type and other information.
The Anatomy of JSP Page
3
4
UNIT-IV : Introduction to JSP
Anatomy of a JSP page
The main parts are JSP elements and template texts.
<%@ page language="java" contentType="text/html" %>
<html>
<body bgcolor="white">
<jsp:useBean id = "userInfo" class= "com.ora.jsp.beans.userInfoBean"> <jsp:setProperty name="userInfo"
property="*"/>
</jsp:useBean>
The following information was saved:
<ul>
<li> User Name: <jsp:getProperty name="userInfo" property="userName"/>
<li> Email Address:
<jsp:getProperty name="userInfo" property="emailAddr"/> </ul>
</body>
</html>
The Anatomy of JSP Page
UNIT-IV : Introduction to JSP
JSP Processing
5
The following steps explain how the web server creates the Webpage using JSP:
• As with a normal page, your browser sends an HTTP request to the web server
.
• The web server recognizes that the HTTP request is for a JSP page and forwards it to a JSP
engine. This is done by using the URL or JSP page which ends with .jsp instead of .html.
• The JSP engine loads the JSP page from disk and converts it into a servlet content. This
conversion is very simple in which all template text is converted to println( ) statements and all
JSP elements are converted to Java code. This code implements the corresponding dynamic
behavior of the page.
• The JSP engine compiles the servlet into an executable class and forwards the original request
to a servlet engine.
• A part of the web server called the servlet engine loads the Servlet class and executes it.
During execution, the servlet produces an output in HTML format. This output is further passed
on to the web server by the servlet engine inside an HTTP response
6
UNIT-IV : Introduction to JSP
JSP Declarations:
A declaration declares one or more variables or methods that you can use in Java code later in the JSP
file. You must declare the variable or method before you use it in the JSP file.
Following is the syntax of JSP Declarations :
<%! declaration; [ declaration; ]+ ... %>
You can write XML equivalent of the above syntax as follows:
<jsp:declaration> code fragment </jsp:declaration>
Following is the simple example for JSP Declarations:
<%! int i = 0; %>
<%! int a, b, c; %>
<%! Circle a = new Circle(2.0); %>
JSP Declarations
7
UNIT-IV : Introduction to JSP
JSP Directives:
JSP Directives: A JSP directive affects the overall structure of the servlet class.
It usually has the following form:
<%@ directive attribute="value" %>
There are three types of directive tags:
JSP Directives
Directive Description
<%@ page ... %> Defines page-dependent attributes, such as
scripting language, error page, and buffering
requirements
<%@ include ... %> Includes a file during the translation phase.
<%@ taglib ... %> Declares a tag library, containing custom
actions, used in the page
8
UNIT-IV : Introduction to JSP
JSP Expressions:
A JSP expression element contains a scripting language expression that is evaluated, converted to a
String, and inserted where the expression appears in the JSP file. Because the value of an expression is
converted to a String, you can use an expression within a line of text, whether or not it is tagged with HTML,
in a JSP file. The expression element can contain any expression that is valid according to the Java
Language Specification but you cannot use a semicolon to end an expression.
Following is the syntax of JSP Expression:
<%= expression %>
You can write XML equivalent of the above syntax as follows:
<jsp:expression> expression </jsp:expression>
Following is the simple example for JSP Expression:
<html>
<head>
<title>A Comment Test</title>
</head>
<body>
<p> Today's date: <%= (new java.util.Date()).toLocaleString()%> </p> </body> </html>
This would generate following result: Today's date: 11-Sep-2010 21:24:25
JSP Expressions
9
UNIT-IV : Introduction to JSP
JSP Implicit Objects
JSP Implicit Objects:
JSP supports nine automatically defined variables, which are also called implicit objects. These
variables are:
Objects Description
request This is the HttpServletRequest object associated with the request.
response This is the HttpServletResponse object associated with the response to
the client.
out This is the PrintWriter object used to send output to the client.
session This is the HttpSession object associated with the request.
application This is the ServletContext object associated with application context
config This is the ServletConfig object associated with the page.
pageContext This encapsulates use of server-specific features like higher
performance JspWriters.
page This is simply a synonym for this, and is used to call the methods
defined by the translated servlet class.
10
UNIT-IV : Introduction to JSP
JSP- Java Beans
JSP- Java Bean
A JavaBean is a specially constructed Java class written in the Java and coded according to the
JavaBeans API specifications.
Following are the unique characteristics that distinguish a JavaBean from other Java classes −
• It provides a default, no-argument constructor
.
• It should be serializable and that which can implement the Serializable interface.
• It may have a number of properties which can be read or written.
•It may have a number of "getter" and "setter" methods for the properties. JavaBeans Properties
• A JavaBean property is a named attribute that can be accessed by the user of the object.
•The attribute can be of any Java data type, including the classes that you define.
• A JavaBean property may be read, write, read only, or write only. JavaBean properties are
accessed through two methods in the JavaBean's implementation class −
11
UNIT-IV : Introduction to JSP
JSP- Java Beans
JSP- Java Bean
S.No. Method & Description
1 getPropertyName() For example, if property name is firstName, your method name
would be getFirstName() to read that property. This method is called accessor
.
2 setPropertyName() For example, if property name is firstName, your method name
would be setFirstName() to write that property. This method is called mutator
.
A read-only attribute will have only a getPropertyName() method, and a write- only attribute will
have only a setPropertyName() method.
12
UNIT-IV : Introduction to JSP
JSP- Java Beans
Example Java beans with JSP
package com.tutorialspoint;
public class StudentsBean implements java.io.Serializable
{
private String firstName = null;
private String lastName = null;
private int age = 0;
public StudentsBean()
{ }
public String getFirstName()
{
return firstName;
}
public String getLastName()
{ return lastName; }
public int getAge(){ return age; }
public void setFirstName(String firstName)
{ this.firstName = firstName; }
public void setLastName(String lastName)
{ this.lastName = lastName; }
public void setAge(Integer age){ this.age = age; }
}
13
UNIT-IV : Introduction to JSP
JSP- COOKIES
JSP- Cookies:
Cookies are text files stored on the client computer and they are kept for various information
tracking purposes. JSP transparently supports HTTP cookies using underlying servlet
technology.
There are three steps involved in identifying and returning users −
• Server script sends a set of cookies to the browser
. For example, name, age, or identification
number, etc.
•Browser stores this information on the local machine for future use.
•When the next time the browser sends any request to the web server then it sends those
cookies information to the server and server uses that
information to identify the user or may be for some other purpose as well.
• This chapter will teach you how to set or reset cookies, how to access them and how to
delete them using JSP programs.
The Anatomy of a Cookie :
•Cookies are usually set in an HTTP header (although JavaScript can also set a cookie
directly on a browser).
14
UNIT-IV Introduction to JSP
JSP- COOKIES
A JSP that sets a cookie might send headers that look something like this −
HTTP/1.1 200 OK
Date: Fri, 04 Feb 2000 21:03:38 GMT
Server: Apache/1.3.9 (UNIX) PHP/4.0b3
Set-Cookie: name = xyz;
expires = Friday, 04-Feb-07 22:03:38 GMT;
path = /; domain = tutorialspoint.com
Connection: close
Content-Type: text/html
As you can see, the Set-Cookie header contains a name value pair, a GMT date, a path and
a domain. The name and value will be URL encoded. The expires field is an instruction
to the browser to "forget" the cookie after the given time and date.
15
UNIT-IV : Introduction to JSP
JSP- Sessions
JSP- Sessions:
Session Tracking HTTP is a "stateless" protocol which means each time a client retrieves a
Webpage, the client opens a separate connection to the Web server and the server automatically
does not keep any record of previous client request. Maintaining Session Between Web Client And
Server Let us now discuss a few options to maintain the session between the Web Client and the Web
Server − Cookies A webserver can assign a unique session ID as a cookie to each web client and for
subsequent requests from the client they can be recognized using the received cookie. This may not
be an effective way as the browser at times does not support a cookie. It is not recommended to use
this procedure to maintain the sessions. Hidden Form Fields A web server can send a hidden HTML
form field along with a unique session ID as follows −
<input type = "hidden" name = "sessionid" value = "12345">
This entry means that, when the form is submitted, the specified name and value are automatically
included in the GET or the POST data. Each time the web browser sends the request back, the
session_ id value can be used to keep the track of different web browsers. This can be an effective
way of keeping track of the session but clicking on a regular (<A HREF...>) hypertext link does not
result in a form submission, so hidden form fields also cannot support general session tracking. URL
Rewriting You can append some extra data at the end of each URL. This data identifies the session;
the server can associate that session identifier with the data it has stored about that session.
16
UNIT-IV : Introduction to JSP
JSP- Sessions
JSP- Sessions:
For example,
with http://tutorialspoint.com/file.htm;sessionid=12345, the session identifier is attached as
sessionid = 12345 which can be accessed at the web server to identify the client.
URL rewriting is a better way to maintain sessions and works for the browsers when they don't
support cookies.
The drawback here is that you will have to generate every URL dynamically to assign a session ID
though page is a simple static HTML page.
The session Object Apart from the above mentioned options, JSP makes use of the servlet
provided HttpSession Interface.
This interface provides a way to identify a user across.
17
UNIT-IV : Introduction to JSP
JSP- Sessions
JSP- Sessions:
• a one page request or
•visit to a website or
• store information about that user
.
By default, JSPs have session tracking enabled and a new HttpSession object is instantiated for
each new client automatically.
Disabling session tracking requires explicitly turning it off by setting the page directive session
attribute to false as follows −
<%@ page session = "false" %>
The JSP engine exposes the HttpSession object to the JSP author through the implicit session
object.
Since session object is already provided to the JSP programmer, the programmer can
immediately begin storing and retrieving data from the object without any initialization or
getSession().
18
UNIT-IV : Introduction to JSP
JSP- Sessions
JSP- Sessions:
Session Tracking Example :
This example describes how to use the HttpSession object to find out the creation time and the
last-accessed time for a session. We would associate a new session with the request if one does
not already exist.
<%@ page import = "java.io.*,java.util.*" %>
<%
// Get session creation time.
Date createTime = new Date(session.getCreationTime()); // Get last access time of this
Webpage.
Date lastAccessTime = new Date(session.getLastAccessedTime());
String title = "Welcome Back to my website";
Integer visitCount = new Integer(0);
String visitCountKey = new String("visitCount");
String userIDKey = new String("userID");
String userID = new String("ABCD");
UNIT-IV : Introduction to JSP
// Check if this is new comer on your Webpage.
if (session.isNew() ){
title = "Welcome to my website";
session.setAttribute(userIDKey, userID);
session.setAttribute(visitCountKey, visitCount);
}
visitCount = (Integer)session.getAttribute(visitCountKey);
visitCount = visitCount + 1;
userID = (String)session.getAttribute(userIDKey);
session.setAttribute(visitCountKey, visitCount);
%>
<html>
<head>
<title>Session Tracking</title>
</head>
JSP- Sessions
19
UNIT-IV : Introduction to JSP
// Check if this is new comer on your Webpage.
if (session.isNew() ){
title = "Welcome to my website";
session.setAttribute(userIDKey, userID);
session.setAttribute(visitCountKey, visitCount);
}
visitCount = (Integer)session.getAttribute(visitCountKey);
visitCount = visitCount + 1;
userID = (String)session.getAttribute(userIDKey);
session.setAttribute(visitCountKey, visitCount);
%>
<html>
<head>
<title>Session Tracking</title>
</head>
JSP- Sessions
20
UNIT-IV : Introduction to JSP
<body>
<center>
<h1>Session Tracking</h1>
</center>
<table border = "1" align = "center">
<tr bgcolor = "#949494">
<th>Session info</th>
<th>Value</th>
</tr>
<tr> <td>id</td>
<td><% out.print( session.getId()); %></td> </tr>
<tr> <td>Creation Time</td>
<td><% out.print(createTime); %></td> </tr>
<tr> <td>Time of Last Access</td>
<td><% out.print(lastAccessTime); %></td> </tr>
<tr> <td>User ID</td>
<td><% out.print(userID); %></td></tr>
<tr> <td>Number of visits</td>
<td><% out.print(visitCount); %></td> </tr>
</table>
</body>
</html>
JSP- Sessions
21
UNIT-IV : Introduction to JSP
JSP- Sessions
22
JSP–universities & Important Questions:
1. What is JSP
2. What are advantages of JSP
3. What is the difference between include directive & jsp:include
4. What are Custom tags. Why do you need Custom tags. How do you create Custom tag
5. What is jsp:usebean. What are the scope attributes & difference between these attributes
6. What is difference between scriptlet and expression
7. What is Declaration?
Thank you
23

More Related Content

What's hot (20)

PPT
Components of client server application
Ashwin Ananthapadmanabhan
 
PPTX
Issues in knowledge representation
Sravanthi Emani
 
PPTX
Understanding Web Cache
ProdigyView
 
PPTX
Query processing in Distributed Database System
Meghaj Mallick
 
PPTX
Java Beans
Ankit Desai
 
PPTX
Concurrency Control in Distributed Database.
Meghaj Mallick
 
PPTX
Types of parsers
Sabiha M
 
PDF
Remote Method Invocation (RMI)
Peter R. Egli
 
PPT
Data models
Usman Tariq
 
PPT
Form validation client side
Mudasir Syed
 
PPTX
Combining inductive and analytical learning
swapnac12
 
PPTX
Advance Java Topics (J2EE)
slire
 
PPTX
Grasp patterns and its types
Syed Hassan Ali
 
PPS
Java rmi
kamal kotecha
 
PPT
Aspect oriented architecture
tigneb
 
PPTX
INTRODUCTION TO JSP,JSP LIFE CYCLE, ANATOMY OF JSP PAGE AND JSP PROCESSING
Aaqib Hussain
 
PPT
2.3 bayesian classification
Krish_ver2
 
Components of client server application
Ashwin Ananthapadmanabhan
 
Issues in knowledge representation
Sravanthi Emani
 
Understanding Web Cache
ProdigyView
 
Query processing in Distributed Database System
Meghaj Mallick
 
Java Beans
Ankit Desai
 
Concurrency Control in Distributed Database.
Meghaj Mallick
 
Types of parsers
Sabiha M
 
Remote Method Invocation (RMI)
Peter R. Egli
 
Data models
Usman Tariq
 
Form validation client side
Mudasir Syed
 
Combining inductive and analytical learning
swapnac12
 
Advance Java Topics (J2EE)
slire
 
Grasp patterns and its types
Syed Hassan Ali
 
Java rmi
kamal kotecha
 
Aspect oriented architecture
tigneb
 
INTRODUCTION TO JSP,JSP LIFE CYCLE, ANATOMY OF JSP PAGE AND JSP PROCESSING
Aaqib Hussain
 
2.3 bayesian classification
Krish_ver2
 

Similar to WEB TECHNOLOGIES JSP (20)

PPTX
JSP.pptx
NishaRohit6
 
PDF
15 expression-language
snopteck
 
PPSX
Java server pages
Tanmoy Barman
 
PPTX
JSP APP DEVLOPMENT.pptx Related to Android App Development
BhawnaSaini45
 
PPTX
JAVA SERVER PAGES
Kalpana T
 
PPTX
JavaScript, often abbreviated as JS, is a programming language and core techn...
MathivananP4
 
PPTX
SCWCD : Java server pages CHAP : 9
Ben Abdallah Helmi
 
DOCX
Unit 4 web technology uptu
Abhishek Kesharwani
 
DOCX
Unit 4 1 web technology uptu
Abhishek Kesharwani
 
PDF
14 mvc
snopteck
 
DOC
Spatial approximate string search Doc
Sudha Hari Tech Solution Pvt ltd
 
PPT
Introduction to the Servlet / JSP course
JavaEE Trainers
 
PPTX
Jsp basic
Jaya Kumari
 
DOCX
Java server pages
Abhishek Kesharwani
 
DOC
Jsp
Rahul Goyal
 
DOC
Jsp
Rahul Goyal
 
PPTX
Jsp and jstl
vishal choudhary
 
PPTX
Java Server Pages(jsp)
Manisha Keim
 
JSP.pptx
NishaRohit6
 
15 expression-language
snopteck
 
Java server pages
Tanmoy Barman
 
JSP APP DEVLOPMENT.pptx Related to Android App Development
BhawnaSaini45
 
JAVA SERVER PAGES
Kalpana T
 
JavaScript, often abbreviated as JS, is a programming language and core techn...
MathivananP4
 
SCWCD : Java server pages CHAP : 9
Ben Abdallah Helmi
 
Unit 4 web technology uptu
Abhishek Kesharwani
 
Unit 4 1 web technology uptu
Abhishek Kesharwani
 
14 mvc
snopteck
 
Spatial approximate string search Doc
Sudha Hari Tech Solution Pvt ltd
 
Introduction to the Servlet / JSP course
JavaEE Trainers
 
Jsp basic
Jaya Kumari
 
Java server pages
Abhishek Kesharwani
 
Jsp and jstl
vishal choudhary
 
Java Server Pages(jsp)
Manisha Keim
 
Ad

More from Jyothishmathi Institute of Technology and Science Karimnagar (20)

PDF
JAVA PROGRAMMING- GUI Programming with Swing - The Swing Buttons
Jyothishmathi Institute of Technology and Science Karimnagar
 
PDF
JAVA PROGRAMMING - The Collections Framework
Jyothishmathi Institute of Technology and Science Karimnagar
 
PDF
JAVA PROGRAMMING- Exception handling - Multithreading
Jyothishmathi Institute of Technology and Science Karimnagar
 
PDF
JAVA PROGRAMMING – Packages - Stream based I/O
Jyothishmathi Institute of Technology and Science Karimnagar
 
PDF
Java programming -Object-Oriented Thinking- Inheritance
Jyothishmathi Institute of Technology and Science Karimnagar
 
PDF
Compiler Design- Machine Independent Optimizations
Jyothishmathi Institute of Technology and Science Karimnagar
 
PDF
COMPILER DESIGN Run-Time Environments
Jyothishmathi Institute of Technology and Science Karimnagar
 
PDF
COMPILER DESIGN- Syntax Directed Translation
Jyothishmathi Institute of Technology and Science Karimnagar
 
PDF
COMPILER DESIGN- Introduction & Lexical Analysis:
Jyothishmathi Institute of Technology and Science Karimnagar
 
PPTX
CRYPTOGRAPHY AND NETWORK SECURITY- E-Mail Security
Jyothishmathi Institute of Technology and Science Karimnagar
 
PDF
CRYPTOGRAPHY AND NETWORK SECURITY- Transport-level Security
Jyothishmathi Institute of Technology and Science Karimnagar
 
PDF
CRYPTOGRAPHY & NETWORK SECURITY- Cryptographic Hash Functions
Jyothishmathi Institute of Technology and Science Karimnagar
 
PDF
CRYPTOGRAPHY & NETWOK SECURITY- Symmetric key Ciphers
Jyothishmathi Institute of Technology and Science Karimnagar
 
PDF
Computer Forensics Working with Windows and DOS Systems
Jyothishmathi Institute of Technology and Science Karimnagar
 
JAVA PROGRAMMING- GUI Programming with Swing - The Swing Buttons
Jyothishmathi Institute of Technology and Science Karimnagar
 
JAVA PROGRAMMING - The Collections Framework
Jyothishmathi Institute of Technology and Science Karimnagar
 
JAVA PROGRAMMING- Exception handling - Multithreading
Jyothishmathi Institute of Technology and Science Karimnagar
 
JAVA PROGRAMMING – Packages - Stream based I/O
Jyothishmathi Institute of Technology and Science Karimnagar
 
Java programming -Object-Oriented Thinking- Inheritance
Jyothishmathi Institute of Technology and Science Karimnagar
 
Compiler Design- Machine Independent Optimizations
Jyothishmathi Institute of Technology and Science Karimnagar
 
COMPILER DESIGN- Syntax Directed Translation
Jyothishmathi Institute of Technology and Science Karimnagar
 
COMPILER DESIGN- Introduction & Lexical Analysis:
Jyothishmathi Institute of Technology and Science Karimnagar
 
CRYPTOGRAPHY AND NETWORK SECURITY- E-Mail Security
Jyothishmathi Institute of Technology and Science Karimnagar
 
CRYPTOGRAPHY AND NETWORK SECURITY- Transport-level Security
Jyothishmathi Institute of Technology and Science Karimnagar
 
CRYPTOGRAPHY & NETWORK SECURITY- Cryptographic Hash Functions
Jyothishmathi Institute of Technology and Science Karimnagar
 
CRYPTOGRAPHY & NETWOK SECURITY- Symmetric key Ciphers
Jyothishmathi Institute of Technology and Science Karimnagar
 
Computer Forensics Working with Windows and DOS Systems
Jyothishmathi Institute of Technology and Science Karimnagar
 
Ad

Recently uploaded (20)

PDF
HR agent at Mediq: Lessons learned on Agent Builder & Maestro by Tacstone Tec...
UiPathCommunity
 
PPTX
Simplifying End-to-End Apache CloudStack Deployment with a Web-Based Automati...
ShapeBlue
 
PDF
Arcee AI - building and working with small language models (06/25)
Julien SIMON
 
PPTX
python advanced data structure dictionary with examples python advanced data ...
sprasanna11
 
PDF
Lecture A - AI Workflows for Banking.pdf
Dr. LAM Yat-fai (林日辉)
 
PPTX
UI5Con 2025 - Beyond UI5 Controls with the Rise of Web Components
Wouter Lemaire
 
PDF
The Past, Present & Future of Kenya's Digital Transformation
Moses Kemibaro
 
PPTX
Machine Learning Benefits Across Industries
SynapseIndia
 
PPTX
The Yotta x CloudStack Advantage: Scalable, India-First Cloud
ShapeBlue
 
PDF
Bitcoin+ Escalando sin concesiones - Parte 1
Fernando Paredes García
 
PDF
2025-07-15 EMEA Volledig Inzicht Dutch Webinar
ThousandEyes
 
PDF
Novus Safe Lite- What is Novus Safe Lite.pdf
Novus Hi-Tech
 
PPTX
Building a Production-Ready Barts Health Secure Data Environment Tooling, Acc...
Barts Health
 
PDF
OpenInfra ID 2025 - Are Containers Dying? Rethinking Isolation with MicroVMs.pdf
Muhammad Yuga Nugraha
 
PDF
Productivity Management Software | Workstatus
Lovely Baghel
 
PDF
Market Insight : ETH Dominance Returns
CIFDAQ
 
PDF
Novus-Safe Pro: Brochure-What is Novus Safe Pro?.pdf
Novus Hi-Tech
 
PDF
CIFDAQ Market Insight for 14th July 2025
CIFDAQ
 
PDF
Building Resilience with Digital Twins : Lessons from Korea
SANGHEE SHIN
 
PDF
NewMind AI Journal - Weekly Chronicles - July'25 Week II
NewMind AI
 
HR agent at Mediq: Lessons learned on Agent Builder & Maestro by Tacstone Tec...
UiPathCommunity
 
Simplifying End-to-End Apache CloudStack Deployment with a Web-Based Automati...
ShapeBlue
 
Arcee AI - building and working with small language models (06/25)
Julien SIMON
 
python advanced data structure dictionary with examples python advanced data ...
sprasanna11
 
Lecture A - AI Workflows for Banking.pdf
Dr. LAM Yat-fai (林日辉)
 
UI5Con 2025 - Beyond UI5 Controls with the Rise of Web Components
Wouter Lemaire
 
The Past, Present & Future of Kenya's Digital Transformation
Moses Kemibaro
 
Machine Learning Benefits Across Industries
SynapseIndia
 
The Yotta x CloudStack Advantage: Scalable, India-First Cloud
ShapeBlue
 
Bitcoin+ Escalando sin concesiones - Parte 1
Fernando Paredes García
 
2025-07-15 EMEA Volledig Inzicht Dutch Webinar
ThousandEyes
 
Novus Safe Lite- What is Novus Safe Lite.pdf
Novus Hi-Tech
 
Building a Production-Ready Barts Health Secure Data Environment Tooling, Acc...
Barts Health
 
OpenInfra ID 2025 - Are Containers Dying? Rethinking Isolation with MicroVMs.pdf
Muhammad Yuga Nugraha
 
Productivity Management Software | Workstatus
Lovely Baghel
 
Market Insight : ETH Dominance Returns
CIFDAQ
 
Novus-Safe Pro: Brochure-What is Novus Safe Pro?.pdf
Novus Hi-Tech
 
CIFDAQ Market Insight for 14th July 2025
CIFDAQ
 
Building Resilience with Digital Twins : Lessons from Korea
SANGHEE SHIN
 
NewMind AI Journal - Weekly Chronicles - July'25 Week II
NewMind AI
 

WEB TECHNOLOGIES JSP

  • 1. WEB TECHNOLOGIES JSP Dr R Jegadeesan Prof-CSE Jyothishmathi Institute of Technology and Science, karimnagar
  • 2. SYLLABUS 2 UNIT- IV JSP The Anatomy of a JSP Page, JSP Processing, Declarations, Directives, Expressions, Code Snippets, implicit objects, Using Beans in JSP Pages, Using Cookies and session for session tracking, connecting to database in JSP.
  • 3. UNIT-IV : Introduction to JSP Aim & Objective : ➢ Java server pages is a technology for developing web pages that include dynamic content. ➢A JSP page can change its output based on variable items, identity of the user, the browsers type and other information. The Anatomy of JSP Page 3
  • 4. 4 UNIT-IV : Introduction to JSP Anatomy of a JSP page The main parts are JSP elements and template texts. <%@ page language="java" contentType="text/html" %> <html> <body bgcolor="white"> <jsp:useBean id = "userInfo" class= "com.ora.jsp.beans.userInfoBean"> <jsp:setProperty name="userInfo" property="*"/> </jsp:useBean> The following information was saved: <ul> <li> User Name: <jsp:getProperty name="userInfo" property="userName"/> <li> Email Address: <jsp:getProperty name="userInfo" property="emailAddr"/> </ul> </body> </html> The Anatomy of JSP Page
  • 5. UNIT-IV : Introduction to JSP JSP Processing 5 The following steps explain how the web server creates the Webpage using JSP: • As with a normal page, your browser sends an HTTP request to the web server . • The web server recognizes that the HTTP request is for a JSP page and forwards it to a JSP engine. This is done by using the URL or JSP page which ends with .jsp instead of .html. • The JSP engine loads the JSP page from disk and converts it into a servlet content. This conversion is very simple in which all template text is converted to println( ) statements and all JSP elements are converted to Java code. This code implements the corresponding dynamic behavior of the page. • The JSP engine compiles the servlet into an executable class and forwards the original request to a servlet engine. • A part of the web server called the servlet engine loads the Servlet class and executes it. During execution, the servlet produces an output in HTML format. This output is further passed on to the web server by the servlet engine inside an HTTP response
  • 6. 6 UNIT-IV : Introduction to JSP JSP Declarations: A declaration declares one or more variables or methods that you can use in Java code later in the JSP file. You must declare the variable or method before you use it in the JSP file. Following is the syntax of JSP Declarations : <%! declaration; [ declaration; ]+ ... %> You can write XML equivalent of the above syntax as follows: <jsp:declaration> code fragment </jsp:declaration> Following is the simple example for JSP Declarations: <%! int i = 0; %> <%! int a, b, c; %> <%! Circle a = new Circle(2.0); %> JSP Declarations
  • 7. 7 UNIT-IV : Introduction to JSP JSP Directives: JSP Directives: A JSP directive affects the overall structure of the servlet class. It usually has the following form: <%@ directive attribute="value" %> There are three types of directive tags: JSP Directives Directive Description <%@ page ... %> Defines page-dependent attributes, such as scripting language, error page, and buffering requirements <%@ include ... %> Includes a file during the translation phase. <%@ taglib ... %> Declares a tag library, containing custom actions, used in the page
  • 8. 8 UNIT-IV : Introduction to JSP JSP Expressions: A JSP expression element contains a scripting language expression that is evaluated, converted to a String, and inserted where the expression appears in the JSP file. Because the value of an expression is converted to a String, you can use an expression within a line of text, whether or not it is tagged with HTML, in a JSP file. The expression element can contain any expression that is valid according to the Java Language Specification but you cannot use a semicolon to end an expression. Following is the syntax of JSP Expression: <%= expression %> You can write XML equivalent of the above syntax as follows: <jsp:expression> expression </jsp:expression> Following is the simple example for JSP Expression: <html> <head> <title>A Comment Test</title> </head> <body> <p> Today's date: <%= (new java.util.Date()).toLocaleString()%> </p> </body> </html> This would generate following result: Today's date: 11-Sep-2010 21:24:25 JSP Expressions
  • 9. 9 UNIT-IV : Introduction to JSP JSP Implicit Objects JSP Implicit Objects: JSP supports nine automatically defined variables, which are also called implicit objects. These variables are: Objects Description request This is the HttpServletRequest object associated with the request. response This is the HttpServletResponse object associated with the response to the client. out This is the PrintWriter object used to send output to the client. session This is the HttpSession object associated with the request. application This is the ServletContext object associated with application context config This is the ServletConfig object associated with the page. pageContext This encapsulates use of server-specific features like higher performance JspWriters. page This is simply a synonym for this, and is used to call the methods defined by the translated servlet class.
  • 10. 10 UNIT-IV : Introduction to JSP JSP- Java Beans JSP- Java Bean A JavaBean is a specially constructed Java class written in the Java and coded according to the JavaBeans API specifications. Following are the unique characteristics that distinguish a JavaBean from other Java classes − • It provides a default, no-argument constructor . • It should be serializable and that which can implement the Serializable interface. • It may have a number of properties which can be read or written. •It may have a number of "getter" and "setter" methods for the properties. JavaBeans Properties • A JavaBean property is a named attribute that can be accessed by the user of the object. •The attribute can be of any Java data type, including the classes that you define. • A JavaBean property may be read, write, read only, or write only. JavaBean properties are accessed through two methods in the JavaBean's implementation class −
  • 11. 11 UNIT-IV : Introduction to JSP JSP- Java Beans JSP- Java Bean S.No. Method & Description 1 getPropertyName() For example, if property name is firstName, your method name would be getFirstName() to read that property. This method is called accessor . 2 setPropertyName() For example, if property name is firstName, your method name would be setFirstName() to write that property. This method is called mutator . A read-only attribute will have only a getPropertyName() method, and a write- only attribute will have only a setPropertyName() method.
  • 12. 12 UNIT-IV : Introduction to JSP JSP- Java Beans Example Java beans with JSP package com.tutorialspoint; public class StudentsBean implements java.io.Serializable { private String firstName = null; private String lastName = null; private int age = 0; public StudentsBean() { } public String getFirstName() { return firstName; } public String getLastName() { return lastName; } public int getAge(){ return age; } public void setFirstName(String firstName) { this.firstName = firstName; } public void setLastName(String lastName) { this.lastName = lastName; } public void setAge(Integer age){ this.age = age; } }
  • 13. 13 UNIT-IV : Introduction to JSP JSP- COOKIES JSP- Cookies: Cookies are text files stored on the client computer and they are kept for various information tracking purposes. JSP transparently supports HTTP cookies using underlying servlet technology. There are three steps involved in identifying and returning users − • Server script sends a set of cookies to the browser . For example, name, age, or identification number, etc. •Browser stores this information on the local machine for future use. •When the next time the browser sends any request to the web server then it sends those cookies information to the server and server uses that information to identify the user or may be for some other purpose as well. • This chapter will teach you how to set or reset cookies, how to access them and how to delete them using JSP programs. The Anatomy of a Cookie : •Cookies are usually set in an HTTP header (although JavaScript can also set a cookie directly on a browser).
  • 14. 14 UNIT-IV Introduction to JSP JSP- COOKIES A JSP that sets a cookie might send headers that look something like this − HTTP/1.1 200 OK Date: Fri, 04 Feb 2000 21:03:38 GMT Server: Apache/1.3.9 (UNIX) PHP/4.0b3 Set-Cookie: name = xyz; expires = Friday, 04-Feb-07 22:03:38 GMT; path = /; domain = tutorialspoint.com Connection: close Content-Type: text/html As you can see, the Set-Cookie header contains a name value pair, a GMT date, a path and a domain. The name and value will be URL encoded. The expires field is an instruction to the browser to "forget" the cookie after the given time and date.
  • 15. 15 UNIT-IV : Introduction to JSP JSP- Sessions JSP- Sessions: Session Tracking HTTP is a "stateless" protocol which means each time a client retrieves a Webpage, the client opens a separate connection to the Web server and the server automatically does not keep any record of previous client request. Maintaining Session Between Web Client And Server Let us now discuss a few options to maintain the session between the Web Client and the Web Server − Cookies A webserver can assign a unique session ID as a cookie to each web client and for subsequent requests from the client they can be recognized using the received cookie. This may not be an effective way as the browser at times does not support a cookie. It is not recommended to use this procedure to maintain the sessions. Hidden Form Fields A web server can send a hidden HTML form field along with a unique session ID as follows − <input type = "hidden" name = "sessionid" value = "12345"> This entry means that, when the form is submitted, the specified name and value are automatically included in the GET or the POST data. Each time the web browser sends the request back, the session_ id value can be used to keep the track of different web browsers. This can be an effective way of keeping track of the session but clicking on a regular (<A HREF...>) hypertext link does not result in a form submission, so hidden form fields also cannot support general session tracking. URL Rewriting You can append some extra data at the end of each URL. This data identifies the session; the server can associate that session identifier with the data it has stored about that session.
  • 16. 16 UNIT-IV : Introduction to JSP JSP- Sessions JSP- Sessions: For example, with http://tutorialspoint.com/file.htm;sessionid=12345, the session identifier is attached as sessionid = 12345 which can be accessed at the web server to identify the client. URL rewriting is a better way to maintain sessions and works for the browsers when they don't support cookies. The drawback here is that you will have to generate every URL dynamically to assign a session ID though page is a simple static HTML page. The session Object Apart from the above mentioned options, JSP makes use of the servlet provided HttpSession Interface. This interface provides a way to identify a user across.
  • 17. 17 UNIT-IV : Introduction to JSP JSP- Sessions JSP- Sessions: • a one page request or •visit to a website or • store information about that user . By default, JSPs have session tracking enabled and a new HttpSession object is instantiated for each new client automatically. Disabling session tracking requires explicitly turning it off by setting the page directive session attribute to false as follows − <%@ page session = "false" %> The JSP engine exposes the HttpSession object to the JSP author through the implicit session object. Since session object is already provided to the JSP programmer, the programmer can immediately begin storing and retrieving data from the object without any initialization or getSession().
  • 18. 18 UNIT-IV : Introduction to JSP JSP- Sessions JSP- Sessions: Session Tracking Example : This example describes how to use the HttpSession object to find out the creation time and the last-accessed time for a session. We would associate a new session with the request if one does not already exist. <%@ page import = "java.io.*,java.util.*" %> <% // Get session creation time. Date createTime = new Date(session.getCreationTime()); // Get last access time of this Webpage. Date lastAccessTime = new Date(session.getLastAccessedTime()); String title = "Welcome Back to my website"; Integer visitCount = new Integer(0); String visitCountKey = new String("visitCount"); String userIDKey = new String("userID"); String userID = new String("ABCD");
  • 19. UNIT-IV : Introduction to JSP // Check if this is new comer on your Webpage. if (session.isNew() ){ title = "Welcome to my website"; session.setAttribute(userIDKey, userID); session.setAttribute(visitCountKey, visitCount); } visitCount = (Integer)session.getAttribute(visitCountKey); visitCount = visitCount + 1; userID = (String)session.getAttribute(userIDKey); session.setAttribute(visitCountKey, visitCount); %> <html> <head> <title>Session Tracking</title> </head> JSP- Sessions 19
  • 20. UNIT-IV : Introduction to JSP // Check if this is new comer on your Webpage. if (session.isNew() ){ title = "Welcome to my website"; session.setAttribute(userIDKey, userID); session.setAttribute(visitCountKey, visitCount); } visitCount = (Integer)session.getAttribute(visitCountKey); visitCount = visitCount + 1; userID = (String)session.getAttribute(userIDKey); session.setAttribute(visitCountKey, visitCount); %> <html> <head> <title>Session Tracking</title> </head> JSP- Sessions 20
  • 21. UNIT-IV : Introduction to JSP <body> <center> <h1>Session Tracking</h1> </center> <table border = "1" align = "center"> <tr bgcolor = "#949494"> <th>Session info</th> <th>Value</th> </tr> <tr> <td>id</td> <td><% out.print( session.getId()); %></td> </tr> <tr> <td>Creation Time</td> <td><% out.print(createTime); %></td> </tr> <tr> <td>Time of Last Access</td> <td><% out.print(lastAccessTime); %></td> </tr> <tr> <td>User ID</td> <td><% out.print(userID); %></td></tr> <tr> <td>Number of visits</td> <td><% out.print(visitCount); %></td> </tr> </table> </body> </html> JSP- Sessions 21
  • 22. UNIT-IV : Introduction to JSP JSP- Sessions 22 JSP–universities & Important Questions: 1. What is JSP 2. What are advantages of JSP 3. What is the difference between include directive & jsp:include 4. What are Custom tags. Why do you need Custom tags. How do you create Custom tag 5. What is jsp:usebean. What are the scope attributes & difference between these attributes 6. What is difference between scriptlet and expression 7. What is Declaration?