You are on page 1of 50

School of Computing Science and Engineering

Course Code : BTCS3601 Name: Web technology

UNIT-3
Introduction to JSP
•It stands for Java Server Pages.
•It is a server side technology.
•It is used for creating web application.
•It is used to create dynamic web content.
•In this JSP tags are used to insert JAVA code into HTML pages.
•It is an advanced version of Servlet Technology.
•It is a Web based technology helps us to create dynamic and
platform independent web pages.
•In this, Java code can be inserted in HTML/ XML pages or both.

Program Name: B.Tech (CSE)


Features of JSP

Coding in JSP is easy :- As it is just adding JAVA code to


HTML/XML.
Reduction in the length of Code :- In JSP we use action tags,
custom tags etc.
Connection to Database is easier :-It is easier to connect website
to database and allows to read or write data easily to the database.
Make Interactive websites :- In this we can create dynamic web
pages which helps user to interact in real time environment.
Portable, Powerful, flexible and easy to maintain :- as these are
browser and server independent.
Extension to Servlet :- as it has all features of servlets, implicit
objects and custom tags
Program Name: B.Tech (CSE)
JSP Elements – JSP Declaration, JSP Syntax, JSP Expression, JSP
Comments
JSP Declaration
•A declaration tag is a piece of Java code for declaring variables, methods and
classes. 

Syntax of declaration tag:

<%! Dec var %>

Program Name: B.Tech (CSE)


JSP Scriptlet
•Scriptlet tag allows to write Java code into JSP file.

Program Name: B.Tech (CSE)


Program Name: B.Tech (CSE)
JSP Comments

Program Name: B.Tech (CSE)


Creating a simple JSP Page

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"


pageEncoding="ISO-8859-1"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>JSP Example</title>
</head>
<body>

<%-- This is a JSP example with scriplets, comments , expressions --%>


<% out.println("This is guru JSP Example"); %>
<% out.println("The number is "); %>
<%! int num12 = 12; int num32 = 12; %>
<%= num12*num32 %>
Today's date: <%= (new java.util.Date()).toLocaleString()%>
</body>
</html>

Program Name: B.Tech (CSE)


A JavaBean is a specially constructed Java class written in the Java
and coded according to the JavaBeans API specifications.

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

Program Name: B.Tech (CSE)


Program Name: B.Tech (CSE)
In the above code we use “bean id” and “class path” of the bean.
Program Name: B.Tech (CSE)
Program Name: B.Tech (CSE)
Program Name: B.Tech (CSE)
Program Name: B.Tech (CSE)
<html>
<head>
<title>get and set properties Example</title>
</head>

<body>
<jsp:useBean id = "students" class = "com.point.StudentsBean">
<jsp:setProperty name = "students" property = "firstName" value = "Zara"/>
<jsp:setProperty name = "students" property = "lastName" value = "Ali"/>
<jsp:setProperty name = "students" property = "age" value = "10"/>
</jsp:useBean>

<p>Student First Name:


<jsp:getProperty name = "students" property = "firstName"/>
</p>

<p>Student Last Name:


<jsp:getProperty name = "students" property = "lastName"/>
</p>

<p>Student Age:
<jsp:getProperty name = "students" property = "age"/>
</p>
</body> Program Name: B.Tech (CSE)
</html>
Program Name: B.Tech (CSE)
JSP Directives
•JSP directives are the messages to JSP container. They provide global
information about an entire JSP page.

Program Name: B.Tech (CSE)


Following are its list of attributes associated
with page directive:
1.Language
2.Extends
3.Import
4.contentType
5.info
6.session
7.isThreadSafe
8.autoflush
9.buffer
10.IsErrorPage
11.pageEncoding
12.errorPage
13.isELIgonored

Program Name: B.Tech (CSE)


Program Name: B.Tech (CSE)
Program Name: B.Tech (CSE)
Program Name: B.Tech (CSE)
What is MVC?
MVC is an architecture that separates business logic,
presentation and data. In MVC,
•M stands for Model
•V stands for View
•C stands for controller.

MVC is a systematic way to use the application where the flow starts
from the view layer, where the request is raised and processed in
controller layer and sent to model layer to insert data and get back the
success or failure message.
Program Name: B.Tech (CSE)
Model Layer

•This is the data layer which consists of the business logic of the system.
•It consists of all the data of the application
•It also represents the state of the application.
•It consists of classes which have the connection to the database.
•The controller connects with model and fetches the data and sends to
the view layer.
•The model connects with the database as well and stores the data into a
database which is connected to it.

Program Name: B.Tech (CSE)


View Layer

•This is a presentation layer.


•It consists of HTML, JSP, etc. into it.
•It normally presents the UI of the application.
•It is used to display the data which is fetched from the controller which in
turn fetching data from model layer classes.
•This view layer shows the data on UI of the application.

Program Name: B.Tech (CSE)


Controller Layer

•It acts as an interface between View and Model.


•It intercepts all the requests which are coming from the view layer.
•It receives the requests from the view layer and processes the requests
and does the necessary validation for the request.
•This requests is further sent to model layer for data processing, and once
the request is processed, it sends back to the controller with required
information and displayed accordingly by the view.

Program Name: B.Tech (CSE)


Advantages of MVC Architecture
The advantages of MVC are:
•Easy to maintain
•Easy to extend
•Easy to test
•Navigation control is centralized

Program Name: B.Tech (CSE)


Advantage of JSTL

Fast Development JSTL provides many tags that simplify the JSP.


Code Reusability We can use the JSTL tags on various pages.
No need to use scriptlet tag It avoids the use of scriptlet tag.

Program Name: B.Tech (CSE)


Tag Name Description
Core tags The JSTL core tag provide variable support, URL management, flow
control, etc. The URL for the core tag
is http://java.sun.com/jsp/jstl/core. The prefix of core tag is c.

Function tags The functions tags provide support for string manipulation and string
length. The URL for the functions tags
is http://java.sun.com/jsp/jstl/functions and prefix is fn.
Formatting tags The Formatting tags provide support for message formatting, number
and date formatting, etc. The URL for the Formatting tags
is http://java.sun.com/jsp/jstl/fmt and prefix is fmt.
XML tags The XML tags provide flow control, transformation, etc. The URL for
the XML tags is http://java.sun.com/jsp/jstl/xml and prefix is x.

SQL tags The JSTL SQL tags provide SQL support. The URL for the SQL tags
is http://java.sun.com/jsp/jstl/sql and prefix is sql.

Program Name: B.Tech (CSE)


Tags Description
c:out It display the result of an expression, similar to the way <%=...%>
tag work.
c:import It Retrives relative or an absolute URL and display the contents to
either a String in 'var',a Reader in 'varReader' or the page.
c:set It sets the result of an expression under evaluation in a 'scope'
variable.
c:remove It is used for removing the specified scoped variable from a
particular scope.
c:catch It is used for Catches any Throwable exceptions that occurs in the
body.

Program Name: B.Tech (CSE)


c:if It is conditional tag used for testing the condition and display
the body content only if the expression evaluates is true.
c:choose, c:wh It is the simple conditional tag that includes its body content if
en, c:otherwise the evaluated condition is true.

c:forEach It is the basic iteration tag. It repeats the nested body content
for fixed number of times or over collection.
c:forTokens It iterates over tokens which is separated by the supplied
delimeters.
c:param It adds a parameter in a containing 'import' tag's URL.
c:redirect It redirects the browser to a new URL and supports the
context-relative URLs.
c:url It creates a URL with optional query parameters.

Program Name: B.Tech (CSE)


JSTL Core <c:out> Tag

<%@ taglib uri="http://java.sun.com/jsp/jstl/
core" prefix="c" %>  
<html>  
<head>  
<title>Tag Example</title>  
</head>  
<body>  
<c:out value="${'Welcome to javaTpoint'}"/>  
</body>  
</html>  

Program Name: B.Tech (CSE)


JSTL Function Tags List

JSTL Functions Description


fn:contains() It is used to test if an input string containing the
specified substring in a program.
fn:containsIgnor It is used to test if an input string contains the specified
eCase() substring as a case insensitive way.
fn:endsWith() It is used to test if an input string ends with the
specified suffix.
fn:escapeXml() It escapes the characters that would be interpreted as
XML markup.
fn:indexOf() It returns an index within a string of first occurrence of
a specified substring.
fn:trim() It removes the blank spaces from both the ends of a
string.

Program Name: B.Tech (CSE)


JSTL Function Tags List

fn:startsWith() It is used for checking whether the given string


is started with a particular string value.

fn:split() It splits the string into an array of substrings.

fn:toLowerCase() It converts all the characters of a string to lower


case.

fn:toUpperCase() It converts all the characters of a string to upper


case.

fn:substring() It returns the subset of a string according to the


given start and end position.

Program Name: B.Tech (CSE)


JSTL Formatting tags

Formatting Tags Descriptions


fmt:parseNumber It is used to Parses the string representation of a currency,
percentage or number.

fmt:timeZone It specifies a parsing action nested in its body or the time


zone for any time formatting.

fmt:formatNumber It is used to format the numerical value with specific format


or precision.

fmt:parseDate It parses the string representation of a time and date.

fmt:formatDate It formats the time and/or date using the supplied pattern and
styles.

Program Name: B.Tech (CSE)


JSTL XML tags List

XML Descriptions
Tags
x:out Similar to <%= ... > tag, but for XPath expressions.
x:parse It is used for parse the XML data specified either in the tag body or an attribute.
x:set It is used to sets a variable to the value of an XPath expression.
x:choose It is a conditional tag that establish a context for mutually exclusive conditional
operations.
x:when It is a subtag of 
x:otherw It is subtag of 
ise
x:if It is used for evaluating the test XPath expression and if it is true, it will processes
its body content.
x:transfo It is used in a XML document for providing the XSL(Extensible Stylesheet
rm Language) transformation.
x:param It is used along with the transform tag for setting the parameter in the XSLT style
sheet
Program Name: B.Tech (CSE)
JSTL SQL Tags

SQL Tags Descriptions


sql:setDataSou It is used for creating a simple data source suitable only for
rce prototyping.
sql:query It is used for executing the SQL query defined in its sql attribute or
the body.
sql:update It is used for executing the SQL update defined in its sql attribute or
in the tag body.
sql:param It is used for sets the parameter in an SQL statement to the specified
value.
sql:dateParam It is used for sets the parameter in an SQL statement to a specified
java.util.Date value.

sql:transaction It is used to provide the nested database action with a common


connection.

Program Name: B.Tech (CSE)


<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>  
<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql"%>  
<html>  
<head>  
<title>sql:setDataSource Tag</title>  
</head>  
<body>  
   
<sql:setDataSource var="db" driver="com.mysql.jdbc.Driver"  
     url="jdbc:mysql://localhost/test"  
     user="root"  password="1234"/>  
</body>  
</html>  
The taglib directive declares that your JSP
page uses a set of custom tags, identifies
the location of the library, and provides
means for identifying the custom tags in
your JSP page.

Program Name: B.Tech (CSE)


Create Table
In MYSQL database, we can create a table in the database with any MYSQL
client.
Here we are using PHPMyadminclient, and there we have an option “new” to
create a new table

Program Name: B.Tech (CSE)


Program Name: B.Tech (CSE)
Another option is by using command prompt and changes to MYSQL
directory:
C:\>
C:\>cd Program Files\MY SQL\bin
C:\>Program Files\MySql\bin>
We can login to database as follows:
C:\Program Files\MYSQL\bin>mysql –u gururoot –p
Enter Password: *******

Program Name: B.Tech (CSE)


Program Name: B.Tech (CSE)
Create Data Records
Let us now create a few records in the Employee table as follows − −

mysql> INSERT INTO Employees VALUES (100, 18, 'Zara', 'Ali');


Query OK, 1 row affected (0.05 sec)

mysql> INSERT INTO Employees VALUES (101, 25, 'Mahnaz', 'Fatma');


Query OK, 1 row affected (0.00 sec)

mysql> INSERT INTO Employees VALUES (102, 30, 'Zaid', 'Khan');


Query OK, 1 row affected (0.00 sec)

mysql> INSERT INTO Employees VALUES (103, 28, 'Sumit', 'Mittal');


Query OK, 1 row affected (0.00 sec)

mysql>

Program Name: B.Tech (CSE)


<%@ page import = "java.io.*,java.util.*,java.sql.*"%>
<%@ page import = "javax.servlet.http.*,javax.servlet.*" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix = "c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix = "sql"%>

<html>
<head>
<title>SELECT Operation</title>
</head>

<body>
<sql:setDataSource var = "snapshot" driver = "com.mysql.jdbc.Driver"
url = "jdbc:mysql://localhost/TEST"
user = "root" password = "pass123"/>

<sql:query dataSource = "${snapshot}" var = "result">


SELECT * from Employees;
</sql:query>

<table border = "1" width = "100%">


Program Name: B.Tech (CSE)
<tr>
<table border = "1" width = "100%">
<tr>
<th>Emp ID</th>
<th>First Name</th>
<th>Last Name</th>
<th>Age</th>
</tr>

<c:forEach var = "row" items = "${result.rows}">


<tr>
<td><c:out value = "${row.id}"/></td>
<td><c:out value = "${row.first}"/></td>
<td><c:out value = "${row.last}"/></td>
<td><c:out value = "${row.age}"/></td>
</tr>
</c:forEach>
</table>

</body>
</html> Program Name: B.Tech (CSE)
Emp ID First Name Last Name Age

100 Zara Ali 18

101 Mahnaz Fatma 25

102 Zaid Khan 30

103 Sumit Mittal 28

Program Name: B.Tech (CSE)


INSERT Operation

Program Name: B.Tech (CSE)


Out Put:

Emp ID First Name Last Name Age

100 Zara Ali 18


101 Mahnaz Fatma 25
102 Zaid Khan 30
103 Sumit Mittal 28
104 Nuha Ali 2

Program Name: B.Tech (CSE)


<c:set var = "empId" value = "102"/>

<sql:update dataSource = "${snapshot}" var = "count">


UPDATE Employees SET WHERE last = 'Ali'
<sql:param value = "${empId}" />
</sql:update>

<sql:query dataSource = "${snapshot}" var = "result">


SELECT * from Employees;
</sql:query>

Program Name: B.Tech (CSE)


Out Put:

Emp ID First Name Last Name Age

100 Zara Ali 18


101 Mahnaz Fatma 25
102 Zaid Ali 30

Program Name: B.Tech (CSE)

You might also like