Presented by:
Akanksha Shukla
 Introduction
JSP pages
Markup
Scriptlets
Example
 Accessing Database From JSP Page
Steps to access database
Example
Description
 References
2
Java Server Pages (JSP) is a Java technology
that helps software developers serve dynamically
generated web pages based on HTML, XML, or
other document types. JSP pages are loaded in the
server and operated from a structured special
installed Java server packet called a Java EE Web
Application, often packaged as a .war or .ear file
archive. JSP allows Java code and certain pre-
defined actions to be interleaved with static web
markup content, with the resulting page being
compiled and executed on the server to deliver an
HTML or XML document.
.
3
4
JSP allows Java code and certain pre-defined
actions to be interleaved with static web
markup content, with the resulting page
being compiled and executed on the server to
deliver an HTML or XML document.
JSP syntax is a fluid mix of two basic content
forms:
 1. Scriptlet Elements
 2. Markup
Markup is typically standard HTML or XML.
When the page is requested, the Java code is
executed and its output is added with the
surrounding markup to create the final page.
Because Java is a compiled language, not a
scripting language, JSP pages must be compiled
to Java bytecode classes before they can be
executed, but such compilation is needed only
when a change to the source JSP file has
occurred.
5
Scriptlet elements are delimited blocks of Java
code which may be intermixed with the
markup. Java code is not required to be
complete (self contained) within its scriptlet
element block, but can straddle markup
content providing the page as a whole is
syntactically correct (for example, any Java
if/for/while blocks opened in one scriptlet
element must be correctly closed in a later
element for the page to successfully compile).
6
7
Request
JSP Engine
and
Web Server
JSP file
Component
Client
Response
Request Request
Response Response
Fig: Data Passed b/w Client & Server
 <%-- use the 'taglib' directive to make the JSTL 1.0 core tags available; use the uri
 "http://java.sun.com/jsp/jstl/core" for JSTL 1.1 --%>
 <%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>

 <%-- use the 'jsp:useBean' standard action to create the Date object; the object is set
 as an attribute in page scope
 --%>
 <jsp:useBean id="date" class="java.util.Date" />

 <html>
 <head><title>First JSP</title></head>
 <body>
 <h2>Here is today's date</h2>
 <c:out value="${date}" />
 </body>
 </html>
8
9
While accessing JSP database from JSP page, we
should have some database package
installed. Here we consider the connectivity
from MYSQL database with JSP.
Assumptions:
 TOMCAT web server is installed.
 MYSQL server is installed.
 JDK is installed.
Various steps are:
1. Create a database named “books”, statement is:
MYSQL>CREATE DATABASE books ;
2. Create a table named “books_details”, statement is:
MYSQL>CREATE TABLE books_details (
`id` INT( 11 ) NOT NULL AUTO_INCREMENT ,
`book_name` VARCHAR( 100 ) NOT NULL ,
`author` VARCHAR( 100 ) NOT NULL ,
PRIMARY KEY ( `id` )
)
10
11
The table is:
ID Book Name Author
1. Java I/O Tim Ritchey
2. Java & XML,
2 Edition Brett McLaughlin
3. Java Swing,
2nd Edition Dave Wood, Marc Loy,
3. Copy the MYSQL-connector-java-3.1.14-
bin.jar to the C:tomcat_directorycommonlib.
Then set the Classpath. For that, we have to
perform the following:
Control Panel->System-> System Properties->
Environmental Variables and set Classpath.
12
13
4. After setting Classpath, restart the Tomcat
Web Server using the startup.bat file at the
command promt.
Create a folder DBDemo using following line:
C:tomcat_directoryjsp-examplesDBDemo
and now write the JSP program which is used
to connect the database.
14
<%@ page language="java" import="java.sql.*"
%>
<%
Connection con=null;
ResultSet rst=null;
Statement stmt=null;
String driver = "org.gjt.mm.mysql.Driver";
Class.forName(driver).newInstance();
Classes &
interfaces
object
instantiation
try
{
String url=
"jdbc:mysql://localhost/books?user=
<user>&password=<password>";
con=DriverManager.getConnection(url);
stmt=con.createStatement();
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
15
16
if(request.getParameter("action") != null)
{
String book=
request.getParameter("bookname");
String author=request.getParameter("author");
stmt.executeUpdate("insert into
books_details(book_name, author)
values('"+bookname+"','"+author+"')");
rst=stmt.executeQuery("select * from
books_details");
%>
Retrieve fields
while(rst.next())
{%>
<tr><td><%=no%></td>
<td><%=rst.getString(“book_name")%> </td>
<td> <%=rst.getString("author“)%> </td>
</tr>
<%no++;
}
rst.close();
stmt.close();
con.close();
%>
17
1. JSP is a kind of scripting language in which we can
embed JAVA code along with HTML elements.
2. Above code of JDBC and jsp shows how we can
manipulate data in a relational database from JSP page.
We need to use the java.sql package: DriverManager,
Connection, Statement, and ResultSet. To create a Web
application, we need JDBC to use more features such as
prepared statements and connection pooling.
18
 Figures- http://www.google.co.in
 “Web Technology” by A.A.Puntambekar
 http://www.wikipedia.org
19
Queries
??? 20
Thanking to
you all......
21

DataBase Connectivity

  • 1.
  • 2.
     Introduction JSP pages Markup Scriptlets Example Accessing Database From JSP Page Steps to access database Example Description  References 2
  • 3.
    Java Server Pages(JSP) is a Java technology that helps software developers serve dynamically generated web pages based on HTML, XML, or other document types. JSP pages are loaded in the server and operated from a structured special installed Java server packet called a Java EE Web Application, often packaged as a .war or .ear file archive. JSP allows Java code and certain pre- defined actions to be interleaved with static web markup content, with the resulting page being compiled and executed on the server to deliver an HTML or XML document. . 3
  • 4.
    4 JSP allows Javacode and certain pre-defined actions to be interleaved with static web markup content, with the resulting page being compiled and executed on the server to deliver an HTML or XML document. JSP syntax is a fluid mix of two basic content forms:  1. Scriptlet Elements  2. Markup
  • 5.
    Markup is typicallystandard HTML or XML. When the page is requested, the Java code is executed and its output is added with the surrounding markup to create the final page. Because Java is a compiled language, not a scripting language, JSP pages must be compiled to Java bytecode classes before they can be executed, but such compilation is needed only when a change to the source JSP file has occurred. 5
  • 6.
    Scriptlet elements aredelimited blocks of Java code which may be intermixed with the markup. Java code is not required to be complete (self contained) within its scriptlet element block, but can straddle markup content providing the page as a whole is syntactically correct (for example, any Java if/for/while blocks opened in one scriptlet element must be correctly closed in a later element for the page to successfully compile). 6
  • 7.
    7 Request JSP Engine and Web Server JSPfile Component Client Response Request Request Response Response Fig: Data Passed b/w Client & Server
  • 8.
     <%-- usethe 'taglib' directive to make the JSTL 1.0 core tags available; use the uri  "http://java.sun.com/jsp/jstl/core" for JSTL 1.1 --%>  <%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>   <%-- use the 'jsp:useBean' standard action to create the Date object; the object is set  as an attribute in page scope  --%>  <jsp:useBean id="date" class="java.util.Date" />   <html>  <head><title>First JSP</title></head>  <body>  <h2>Here is today's date</h2>  <c:out value="${date}" />  </body>  </html> 8
  • 9.
    9 While accessing JSPdatabase from JSP page, we should have some database package installed. Here we consider the connectivity from MYSQL database with JSP. Assumptions:  TOMCAT web server is installed.  MYSQL server is installed.  JDK is installed.
  • 10.
    Various steps are: 1.Create a database named “books”, statement is: MYSQL>CREATE DATABASE books ; 2. Create a table named “books_details”, statement is: MYSQL>CREATE TABLE books_details ( `id` INT( 11 ) NOT NULL AUTO_INCREMENT , `book_name` VARCHAR( 100 ) NOT NULL , `author` VARCHAR( 100 ) NOT NULL , PRIMARY KEY ( `id` ) ) 10
  • 11.
    11 The table is: IDBook Name Author 1. Java I/O Tim Ritchey 2. Java & XML, 2 Edition Brett McLaughlin 3. Java Swing, 2nd Edition Dave Wood, Marc Loy,
  • 12.
    3. Copy theMYSQL-connector-java-3.1.14- bin.jar to the C:tomcat_directorycommonlib. Then set the Classpath. For that, we have to perform the following: Control Panel->System-> System Properties-> Environmental Variables and set Classpath. 12
  • 13.
    13 4. After settingClasspath, restart the Tomcat Web Server using the startup.bat file at the command promt. Create a folder DBDemo using following line: C:tomcat_directoryjsp-examplesDBDemo and now write the JSP program which is used to connect the database.
  • 14.
    14 <%@ page language="java"import="java.sql.*" %> <% Connection con=null; ResultSet rst=null; Statement stmt=null; String driver = "org.gjt.mm.mysql.Driver"; Class.forName(driver).newInstance(); Classes & interfaces object instantiation
  • 15.
  • 16.
    16 if(request.getParameter("action") != null) { Stringbook= request.getParameter("bookname"); String author=request.getParameter("author"); stmt.executeUpdate("insert into books_details(book_name, author) values('"+bookname+"','"+author+"')"); rst=stmt.executeQuery("select * from books_details"); %> Retrieve fields
  • 17.
  • 18.
    1. JSP isa kind of scripting language in which we can embed JAVA code along with HTML elements. 2. Above code of JDBC and jsp shows how we can manipulate data in a relational database from JSP page. We need to use the java.sql package: DriverManager, Connection, Statement, and ResultSet. To create a Web application, we need JDBC to use more features such as prepared statements and connection pooling. 18
  • 19.
     Figures- http://www.google.co.in “Web Technology” by A.A.Puntambekar  http://www.wikipedia.org 19
  • 20.
  • 21.