SlideShare a Scribd company logo
1 of 23
Download to read offline
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)

Architecture of Linux
 Architecture of Linux Architecture of Linux
Architecture of Linux
 
Analysis modeling in software engineering
Analysis modeling in software engineeringAnalysis modeling in software engineering
Analysis modeling in software engineering
 
Java Server Pages(jsp)
Java Server Pages(jsp)Java Server Pages(jsp)
Java Server Pages(jsp)
 
Unified process model
Unified process modelUnified process model
Unified process model
 
Dynamic HTML (DHTML)
Dynamic HTML (DHTML)Dynamic HTML (DHTML)
Dynamic HTML (DHTML)
 
Ado.Net Tutorial
Ado.Net TutorialAdo.Net Tutorial
Ado.Net Tutorial
 
Identifying classes and objects ooad
Identifying classes and objects ooadIdentifying classes and objects ooad
Identifying classes and objects ooad
 
Bootstrap
BootstrapBootstrap
Bootstrap
 
enterprise java bean
enterprise java beanenterprise java bean
enterprise java bean
 
Ajax ppt
Ajax pptAjax ppt
Ajax ppt
 
Session bean
Session beanSession bean
Session bean
 
Web controls
Web controlsWeb controls
Web controls
 
Sgml
SgmlSgml
Sgml
 
Java script
Java scriptJava script
Java script
 
Struts framework
Struts frameworkStruts framework
Struts framework
 
JavaScript - Chapter 12 - Document Object Model
  JavaScript - Chapter 12 - Document Object Model  JavaScript - Chapter 12 - Document Object Model
JavaScript - Chapter 12 - Document Object Model
 
Class diagrams
Class diagramsClass diagrams
Class diagrams
 
Enhanced Entity-Relationship (EER) Modeling
Enhanced Entity-Relationship (EER) ModelingEnhanced Entity-Relationship (EER) Modeling
Enhanced Entity-Relationship (EER) Modeling
 
Introduction to ASP.NET
Introduction to ASP.NETIntroduction to ASP.NET
Introduction to ASP.NET
 
Cgi
CgiCgi
Cgi
 

Similar to WEB TECHNOLOGIES JSP

Similar to WEB TECHNOLOGIES JSP (20)

JSP.pptx
JSP.pptxJSP.pptx
JSP.pptx
 
15 expression-language
15 expression-language15 expression-language
15 expression-language
 
Java server pages
Java server pagesJava server pages
Java server pages
 
JAVA SERVER PAGES
JAVA SERVER PAGESJAVA SERVER PAGES
JAVA SERVER PAGES
 
Jsp in Servlet by Rj
Jsp in Servlet by RjJsp in Servlet by Rj
Jsp in Servlet by Rj
 
JavaScript, often abbreviated as JS, is a programming language and core techn...
JavaScript, often abbreviated as JS, is a programming language and core techn...JavaScript, often abbreviated as JS, is a programming language and core techn...
JavaScript, often abbreviated as JS, is a programming language and core techn...
 
SCWCD : Java server pages CHAP : 9
SCWCD : Java server pages  CHAP : 9SCWCD : Java server pages  CHAP : 9
SCWCD : Java server pages CHAP : 9
 
Unit 4 web technology uptu
Unit 4 web technology uptuUnit 4 web technology uptu
Unit 4 web technology uptu
 
Unit 4 1 web technology uptu
Unit 4 1 web technology uptuUnit 4 1 web technology uptu
Unit 4 1 web technology uptu
 
14 mvc
14 mvc14 mvc
14 mvc
 
Spatial approximate string search Doc
Spatial approximate string search DocSpatial approximate string search Doc
Spatial approximate string search Doc
 
Introduction to the Servlet / JSP course
Introduction to the Servlet / JSP course Introduction to the Servlet / JSP course
Introduction to the Servlet / JSP course
 
Jsp basic
Jsp basicJsp basic
Jsp basic
 
Java server pages
Java server pagesJava server pages
Java server pages
 
Jsp
JspJsp
Jsp
 
Jsp
JspJsp
Jsp
 
Jsp
JspJsp
Jsp
 
Jsp and jstl
Jsp and jstlJsp and jstl
Jsp and jstl
 
Servlets and jsp pages best practices
Servlets and jsp pages best practicesServlets and jsp pages best practices
Servlets and jsp pages best practices
 
Enterprise java unit-3_chapter-1-jsp
Enterprise  java unit-3_chapter-1-jspEnterprise  java unit-3_chapter-1-jsp
Enterprise java unit-3_chapter-1-jsp
 

More from Jyothishmathi Institute of Technology and Science Karimnagar

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

JAVA PROGRAMMING- GUI Programming with Swing - The Swing Buttons
JAVA PROGRAMMING- GUI Programming with Swing - The Swing ButtonsJAVA PROGRAMMING- GUI Programming with Swing - The Swing Buttons
JAVA PROGRAMMING- GUI Programming with Swing - The Swing Buttons
 
JAVA PROGRAMMING - The Collections Framework
JAVA PROGRAMMING - The Collections Framework JAVA PROGRAMMING - The Collections Framework
JAVA PROGRAMMING - The Collections Framework
 
JAVA PROGRAMMING- Exception handling - Multithreading
JAVA PROGRAMMING- Exception handling - MultithreadingJAVA PROGRAMMING- Exception handling - Multithreading
JAVA PROGRAMMING- Exception handling - Multithreading
 
JAVA PROGRAMMING – Packages - Stream based I/O
JAVA PROGRAMMING – Packages - Stream based I/O JAVA PROGRAMMING – Packages - Stream based I/O
JAVA PROGRAMMING – Packages - Stream based I/O
 
Java programming -Object-Oriented Thinking- Inheritance
Java programming -Object-Oriented Thinking- InheritanceJava programming -Object-Oriented Thinking- Inheritance
Java programming -Object-Oriented Thinking- Inheritance
 
WEB TECHNOLOGIES JavaScript
WEB TECHNOLOGIES JavaScriptWEB TECHNOLOGIES JavaScript
WEB TECHNOLOGIES JavaScript
 
WEB TECHNOLOGIES Servlet
WEB TECHNOLOGIES ServletWEB TECHNOLOGIES Servlet
WEB TECHNOLOGIES Servlet
 
WEB TECHNOLOGIES XML
WEB TECHNOLOGIES XMLWEB TECHNOLOGIES XML
WEB TECHNOLOGIES XML
 
WEB TECHNOLOGIES- PHP Programming
WEB TECHNOLOGIES-  PHP ProgrammingWEB TECHNOLOGIES-  PHP Programming
WEB TECHNOLOGIES- PHP Programming
 
Compiler Design- Machine Independent Optimizations
Compiler Design- Machine Independent OptimizationsCompiler Design- Machine Independent Optimizations
Compiler Design- Machine Independent Optimizations
 
COMPILER DESIGN Run-Time Environments
COMPILER DESIGN Run-Time EnvironmentsCOMPILER DESIGN Run-Time Environments
COMPILER DESIGN Run-Time Environments
 
COMPILER DESIGN- Syntax Directed Translation
COMPILER DESIGN- Syntax Directed TranslationCOMPILER DESIGN- Syntax Directed Translation
COMPILER DESIGN- Syntax Directed Translation
 
COMPILER DESIGN- Syntax Analysis
COMPILER DESIGN- Syntax AnalysisCOMPILER DESIGN- Syntax Analysis
COMPILER DESIGN- Syntax Analysis
 
COMPILER DESIGN- Introduction & Lexical Analysis:
COMPILER DESIGN- Introduction & Lexical Analysis: COMPILER DESIGN- Introduction & Lexical Analysis:
COMPILER DESIGN- Introduction & Lexical Analysis:
 
CRYPTOGRAPHY AND NETWORK SECURITY- E-Mail Security
CRYPTOGRAPHY AND NETWORK SECURITY- E-Mail SecurityCRYPTOGRAPHY AND NETWORK SECURITY- E-Mail Security
CRYPTOGRAPHY AND NETWORK SECURITY- E-Mail Security
 
CRYPTOGRAPHY AND NETWORK SECURITY- Transport-level Security
CRYPTOGRAPHY AND NETWORK SECURITY- Transport-level SecurityCRYPTOGRAPHY AND NETWORK SECURITY- Transport-level Security
CRYPTOGRAPHY AND NETWORK SECURITY- Transport-level Security
 
CRYPTOGRAPHY & NETWORK SECURITY- Cryptographic Hash Functions
CRYPTOGRAPHY & NETWORK SECURITY- Cryptographic Hash FunctionsCRYPTOGRAPHY & NETWORK SECURITY- Cryptographic Hash Functions
CRYPTOGRAPHY & NETWORK SECURITY- Cryptographic Hash Functions
 
CRYPTOGRAPHY & NETWOK SECURITY- Symmetric key Ciphers
CRYPTOGRAPHY & NETWOK SECURITY- Symmetric key CiphersCRYPTOGRAPHY & NETWOK SECURITY- Symmetric key Ciphers
CRYPTOGRAPHY & NETWOK SECURITY- Symmetric key Ciphers
 
CRYPTOGRAPHY & NETWORK SECURITY
CRYPTOGRAPHY & NETWORK SECURITYCRYPTOGRAPHY & NETWORK SECURITY
CRYPTOGRAPHY & NETWORK SECURITY
 
Computer Forensics Working with Windows and DOS Systems
Computer Forensics Working with Windows and DOS SystemsComputer Forensics Working with Windows and DOS Systems
Computer Forensics Working with Windows and DOS Systems
 

Recently uploaded

Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptxLBM Solutions
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...shyamraj55
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebUiPathCommunity
 
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024BookNet Canada
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...Fwdays
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 
Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Neo4j
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brandgvaughan
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr LapshynFwdays
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 

Recently uploaded (20)

Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptx
 
DMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special EditionDMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special Edition
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
 
Hot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort Service
Hot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort ServiceHot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort Service
Hot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort Service
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio Web
 
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 
Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brand
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 

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?