2. Java Server Pages (JSP)
• JSP (Java Server Pages) is a standard for developing interactive Web
applications (pages containing dynamic content).
• A JSP web page (recognizable by the .jsp extension) may display
different content based on certain parameters.
• JSPs are integrated in a web page in HTML using special tags which will
notify the Web server that the code included within these tags are to be
interpreted.
• The result (HTML codes) will be returned to the client browser .
3. Java Server Pages (JSP)
• JSP is a server-side programming technology
• Enables the creation of dynamic, platform-independent method for
building Web-based applications.
• JSP have access to the entire family of Java APIs, including the JDBC
API to access enterprise databases.
• JSP support dynamic content which helps developers insert java code in
HTML pages by making use of special JSP tags, most of which start with
<% and end with %>.
4. Advantage of JSP
• Easy to maintain
• High Performance and Scalability.
• JSP is built on Java technology, so it is platform independent.
• JSPs are multithreaded.
• JSPs are portable.
• JSPs are object-oriented.
• JSPs are secure.
6. Web Server: Tomcat
• Apache Tomcat is an open source software implementation of the JSP
and Servlet technologies and can act as a standalone server for testing
JSP and Servlets and can be integrated with the Apache Web Server.
• The JSP container is responsible for intercepting requests for JSP
pages.
• JSP container works with the Web server to provide the runtime
environment and other services a JSP needs.
• Container knows how to understand the special elements that are part of
JSPs.
7. JSP Life Cycle
• JSP life cycle can be defined as the entire process from its creation till
the destruction.
• A JSP page is converted into Servlet in order to service requests. The
translation of a JSP page to a Servlet is called Lifecycle of JSP. JSP
Lifecycle consists of following steps.
1. Translation of JSP to Servlet code.
2. Compilation of Servlet to bytecode.
3. Loading Servlet class.
4. Creating servlet instance.
5. Initialization by calling
jspInit() method
6. Request Processing by calling
_jspService() method
7. Destroying by calling jspDestroy() method
9. JSP Compilation
• JSP pages are converted into Servlet by the Web Container.
• The Container translates a JSP page into servlet class source(.java) file and then
compiles into a Java Servlet class.
10. Elements of a JSP page
• A JSP page can contain four types elements (excluding the HTML
code):
Statements: To declare methods and attributes
Scriptlets: Java code that will be translated into code in the
service() method
Expressions: To easily send dynamically created string to the
browser
Directives: Comprehensive information on the page
11. JSP Declarations Tag
• A declaration is a block of code to define methods and class variables to
be used throughout the page.
• The syntax for a declaration is as follows:
Syntax:
<%! declaration %>
Example:
<%!String str = "Hi every one…..";
int Number = 10;
public static int add(int a, int b) {
return a+b;
}%>
12. JSP Expressions Tag:
• Expression is used to print all Literals, Method return values, & variable
values.
• Whatever u like to print, same thing we can keep inside a Expression
• The code placed within expression tag is written to the output stream of
the response. So you need not write out.print() to write data. It is mainly
used to print the values of variable or method.
• Whatever statements we're keeping inside a Scriptlet, those should not
end with a semicolon (;)
13. JSP Expressions Tag
• JSP expressions can insert strings (dynamically generated) in HTML
page. The syntax of a JSP expression is as follows:
• It can be used instead of the following scriptlet:
Syntax:
<%= Expression %>
Example:
<%= "Welcome to Hidaya Institute of Science
and Technology" %>
Syntax:
<% Statement %>
Example:
<%out.println(showData());%>
14. Scriptlet Tag:
• Inside Scriptlet, we're keeping the java statements.
• Scriptlets are almost look like a statements which are inside a method.
• Whatever statements we're keeping inside a method, same statements,
we can keep inside a Scriptlet.
• Whatever statements we're keeping inside a Scriptlet, those should end
with a semicolon (;)
Syntax :
<% java source code; %>
Example:
<% out.println("Welcome in JSP"); %>
15. JSP comments
• With JSP you can add comments in two ways.
Generate a comment visible in the HTML source code (HTML
comment) of the client with the following syntax:
Create a comment in the JSP code for the purpose of
documentation (not visible to the client) with the following syntax:
<!-- comments [<%= expression %>] -->
<%-- comments --%>
16. JSP Directives
• Directives control the processing of an entire JSP page. It gives
directions to the server regarding processing of a page.
• Directive Tag gives special instruction to Web Container at the
time of page translation.
Directive Description
<%@ page ... %>
Defines page dependent properties such as language,
session, errorPage etc.
<%@ include ... %> Defines file to be included.
<%@ taglib ... %> Declares tag library used in the page
17. Page Directive
Attribut Possible values Description
language java
Specifies the language to be used to
process to the instructions of the page.
import pakage.*
Allows you to import a list of classes or
packages
errorPage URL
Allows you to specify a JSP page to
manage unhandled exceptions.
contentType
text/html;charset=I
SO-8859-1
Indicates the MIME type of the page as
well as the character set used
• There are several attributes, which are used along with Page
Directives like
18. The import Attribute:
• The import attribute serves the same function as, and behaves like, the
Java import statement. The value for the import option is the name of the
package you want to import.
• To import java.sql.*, use the following page directive:
Syntax :
<%@ page import="java.sql.*" %>
Example:
<%@page import="java.util.Random" %>
19. <%@page import="java.util.Random" %>
<h1><%= "Hello World!"%></h1>
<% out.print("Welcome in JSP page");%><br />
<%
int radius = 7;
double pi = 3.1415;
out.print("Result: " + (radius + pi));
%>
<br />
<% Random rand = new Random();
int a = rand.nextInt();
out.print(a);
%>
20. Include Directive
• Used to copy the content of one JSP page to another. It’s like
including the code of one file into another.
• For merging external files to the current JSP page during
translation phase (The phase where JSP gets converted into the
equivalent Servlet).
Syntax:
<%@ include file="URL of the file" %>
Example:
<%@ include file=“header.html" %>
21. Deployment Descriptor: web.xml
• Java web applications use a deployment descriptor file to determine how
URLs map to servlets, which URLs require authentication, and other
information.
• This file is named web.xml, and resides in WEB-INF/ directory.
• web.xml is part of the servlet standard for web applications.
• IT describes the classes, resources and configuration of the application
and how the web server uses them to serve web requests.
• When the web server receives a request for the application, it uses the
deployment descriptor to map the URL of the request to the code that
ought to handle the request.
25. session
• Java have an HttpSession object which you can use to store state
information for a user.
• The session is managed on the client by a cookie (JSESSIONID) or can
be done using URL rewrites.
• The session timeout describes how long the server will wait after the last
request before deleting the state information stored in a HttpSession.
session.setAttribute("name", name);
session.getAttribute("name")
27. DataBase
<%@ page contentType="text/html; charset=utf-8" language="java"
import="java.sql.*" errorPage="" %>
<%@page import="java.util.*" %>
<!doctype html>
<%!
public class Bean{
public int Id;
public String Name;
public String Fname;
public String Surname;
}
28. public Vector<Bean> getStudent()throws SQLException{
Statement stat = null;
ResultSet result = null;
Vector<Bean> vect = new Vector<Bean>();
String query = "Select StudentId, StudentName, FatherName, surname from student";
try{
stat = con.createStatement();
result=stat.executeQuery(query);
while(result.next()){
Bean std = new Bean();
std.Id = result.getInt("StudentId");
std.Name = result.getString("StudentName");
std.Fname = result.getString("FatherName");
std.Surname = result.getString("FatherName");
vect.addElement(std);
}//end loop
}finally{
if(stat!=null) stat.close();
}
return vect;
}//end getStudent
29. Add Method
public int addStudent(String name, String fname, String surname)throws
SQLException{
Statement stat = null;
int rows=0;
String query = "insert into student (StudentName, FatherName,
surname) VALUES ('"+name+"','"+fname+"','"+surname+"')";
try{
stat = con.createStatement();
rows = stat.executeUpdate(query);
return rows;
}finally{
if(stat!=null) stat.close();
}
}//end insert
}//end class
%>