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.
Size: 352.54 KB
Language: en
Added: May 05, 2021
Slides: 23 pages
Slide Content
WEB TECHNOLOGIES JSP
Dr R Jegadeesan Prof-CSE
Jyothishmathi Institute of Technology and Science,
karimnagar
UNIT-IV : Introduction to JSP
Aim & Objective :
➢Javaserverpagesisatechnologyfordevelopingwebpagesthat
includedynamiccontent.
➢AJSPpagecanchangeitsoutputbasedonvariableitems,identityofthe
user,thebrowserstypeandotherinformation.
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:useBeanid = "userInfo" class= "com.ora.jsp.beans.userInfoBean"> <jsp:setPropertyname="userInfo"
property="*"/>
</jsp:useBean>
The following information was saved:
<ul>
<li> User Name: <jsp:getPropertyname="userInfo" property="userName"/>
<li> Email Address:
<jsp:getPropertyname="userInfo" property="emailAddr"/> </ul>
</body>
</html>
The Anatomy of JSP Page
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
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 HttpServletRequestobject associated with the request.
response This is the HttpServletResponseobject associated with the response to
the client.
out This is the PrintWriterobject used to send output to the client.
session This is the HttpSessionobject associated with the request.
applicationThis is the ServletContextobject associated with application context
config This is the ServletConfigobject associated with the page.
pageContextThis 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.
11
UNIT-IV : Introduction to JSP
JSP-Java Beans
JSP-Java Bean
S.No. Method&Description
1 getPropertyName()Forexample,ifpropertynameisfirstName,yourmethodname
wouldbegetFirstName()toreadthatproperty.Thismethodiscalledaccessor.
2 setPropertyName()Forexample,ifpropertynameisfirstName,yourmethodname
wouldbesetFirstName()towritethatproperty.Thismethodiscalledmutator.
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; }
}
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
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?