Adv. P Chapter Five
Adv. P Chapter Five
Adv. P Chapter Five
10/19/2024
1
Contents
Introduction
Java Server Pages Overview
Elements of JSP
Static content
Scripting elements
Action
Directives
First Java Server Page Examples
Implicit Object
2 10/19/2024
What's Wrong with Servlets?
import javax.servlet.*; out.println("<HEAD><TITLE>Using
import javax.servlet.http.*; Servlets</TITLE></HEAD>");
import java.io.*; out.println("<BODY BGCOLOR=#123123>");
import java.util.*; //Get parameter names
public class MyDearServlet extends HttpServlet Enumeration parameters =
{ request.getParameterNames();
//Process the HTTP GET request String param = null;
public void doGet(HttpServletRequest request, while (parameters.hasMoreElements()) {
HttpServletResponse response) param = (String) parameters.nextElement();
throws ServletException, IOException { out.println(param + ":" +
request.getParameter(param) +
doPost(request, response);
"<BR>");
}
} Displays All
//Process the HTTP POST request
out.println("</BODY>"); Parameter/Value
public void doPost(HttpServletRequest
request, out.println("</HTML>"); Pairs in a Request
HttpServletResponse response) out.close(); Using JSP
throws ServletException, IOException { } //End of doPost method
response.setContentType("text/html"); } //End of class
PrintWriter out = response.getWriter();
out.println("<HTML>");
3 10/19/2024
Servlet vs JSP
JSP solves drawbacks in the servlet technology by
allowing the
programmer to intersperse code with static content, for
example.
5 10/19/2024
Servlet vs JSP
Youcan see that <HTML> tags stay as they are.
When you need to add dynamic content, all you
need to do is enclose your code in <% … %> tags.
7 10/19/2024
What is JSP ?
A Java Server Pages component is a type of
Java servlet that is designed to fulfill the
role of a user interface for a Java web
application.
8 10/19/2024
Why Use JSP?
JavaServer Pages often serve the same purpose as
programs implemented using the Common
Gateway Interface (CGI).
But JSP offers several advantages in comparison
with the CGI.
Performance is significantly better because JSP
allows embedding Dynamic Elements in HTML
Pages itself instead of having separate CGI files.
10 10/19/2024
Advantages of JSP over Servlets
Servlets use println statements for printing an HTML
document which is usually very difficult to use.
JSP has no such tedious task to maintain.
11 10/19/2024
Advantages of JSP-over other
Technologies
Following is the list of other advantages of using JSP over
other technologies:
Active Server Pages (ASP)
The advantages of JSP are twofold.
First, the dynamic part is written in Java, not Visual
Basic
or other MS specific language, so it is more powerful
and easier to use.
Second, it is portable to other operating systems and
12 10/19/2024
Advantages of JSP-over other
Technologies
Server-Side Includes (SSI)
SSI is really only intended for simple inclusions, not for
"real" programs that use form data, make database
connections, and the like.
JavaScript
JavaScript can generate HTML dynamically on the
client but can hardly interact with the web server to
perform complex tasks like database access and image
processing etc.
Static HTML
Regular HTML, of course, cannot contain dynamic
information.
13 10/19/2024
Elements of JSP
1. The Scriptlet
A scriptlet can contain any number of JAVA language
statements, variable or expressions that are valid in
the page scripting language.
Following is the syntax of Scriptlet:
<jsp:scriptlet>
code fragment
</jsp:scriptlet>
14 10/19/2024
Elements of JSP-scriptlet
Any text, HTML tags, or JSP elements you write must be
outside the scriptlet. Following is the simple and first
example for JSP:
<html>
<head><title>Hello World</title></head>
<body>
Hello World!<br/>
<%
out.println("Your IP address is " +
request.getRemoteAddr());
%>
</body>
</html>
15 10/19/2024
The Scriptlet-example
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<h1><font color="red"> Hello World</h1>
<body>
<h1>Hello World!</h1>
<%
for(int i=0;i<10;i++){
out.println(i);
}
%>
</body>
</html>
16 10/19/2024
Elements of JSP
2. JSP Declarations : allow you to declare methods and
variables that can be used from any point in the JSP
page.
Declarations also provide a way to create initialization
and clean-up code by utilizing the jspInit and jspDestroy
methods.
can appear anywhere throughout the page
<%! declaration; [ declaration; ]+ ...
Following is the syntax for JSP Declarations:
%>
19 10/19/2024
Expressions- Example
<html>
<%
int a = 5;
int b = 8;
%>
<body>
The first number is : <%= a %> <br/>
The second number is : <%= b %>
<br/>
The sum is : <%= ( a + b ) %> <br/>
</body
</html>
20 10/19/2024
Elements of JSP
4. JSP Comments
JSP comment marks the text or the statements that
the JSP container should ignore.
A JSP comment is useful when you want to hide or
"comment out", a part of your JSP page.
21 10/19/2024
Elements of JSP
example shows the JSP Comments:
<html>
<head><title>A Comment Test</title></head>
<body>
<h2>A Test of Comments</h2>
<%-- This comment will not be visible in the
page source --%>
</body>
</html>
22 10/19/2024
Elements of JSP
5. JSP Directives
A JSP directive is a statement that gives the JSP
engine information about the JSP page.
23 10/19/2024
Elements of JSP-Directives
There are three types of directive tag:
Directive Description
<%@ taglib ... %> tablib lets you define custom tags.
24 10/19/2024
JSP ─ DIRECTIVES
These directives provide directions and
instructions to the container, telling it how to
handle certain aspects of the JSP processing.
25 10/19/2024
The include Directive
The include directive is used to include a file during the
translation phase.
This directive tells the container to merge the content of
other external files with the current JSP during the
translation phase. You may code the include directives
anywhere in your JSP page.
26 10/19/2024
include example
display.j
Index.js sp
<html>
p
<head> <html>
<title>JSP include Directive <head>
example</title> <body>
</head> <p>This is the content of my
<body> file</p>
<%@ include file="display.jsp" </body>
%> </html>
</body>
</html>
27 10/19/2024
Include -Example
A good example of the include directive is
including a common header and footer with
multiple pages of content.
Let us define following three files
a) header.jps,
b) footer.jsp,
c) main.jsp as follows:
28 10/19/2024
Include -Example
The following is the content of header.jsp:
<%!
int pageCount = 0;
void addCount() {
pageCount++;
}
%>
<% addCount(); %>
<html>
<head>
<title>The include Directive Example</title>
</head>
<body>
<center>
<h2>The include Directive Example</h2>
<p>This site has been visited <%= pageCount %> times.</p>
</center>
<br/><br/> 10/19/2024
29
Include -Example
Following is the content of footer.jsp:
30 10/19/2024
Include -Example
you will receive the following output:
31 10/19/2024
The page Directive
The page directive is used to provide instructions
to the container.
These instructions pertain to the current JSP page.
You may code page directives anywhere in your
JSP page.
32 10/19/2024
The page Directive-example
Example:
33 10/19/2024
The page Directive-Attributes
Following table lists out the attributes associated with page directive:
Attribute Purpose
buffer Specifies a buffering model for the output stream.
autoFlush Controls the behavior of the servlet output buffer.
contentType Defines the character encoding scheme.
Defines the URL of another JSP that reports on Java
errorPage
unchecked runtime exceptions.
Indicates if this JSP page is a URL specified by another JSP
isErrorPage
page's errorPage attribute.
Specifies a superclass that the generated servlet must
extends
extend.
Specifies a list of packages or classes for use in the JSP as
import
the Java import statement does for Java classes.
Defines a string that can be accessed with the servlet's
info
getServletInfo() method.
isThreadSafe Defines the threading model for the generated servlet.
language Defines the programming language used in the JSP page.
Specifies whether or not the JSP page participates in HTTP
session
sessions
34 10/19/2024
Example
The extends Attribute
The extends attribute specifies a superclass that
the generated servlet must extend.
35 10/19/2024
Example
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.
36 10/19/2024
Elements of JSP
6. JSP Actions
JSP actions use constructs in XML syntax to
control the behavior of the servlet engine.
You
can dynamically insert a file, reuse JavaBeans
components, forward the user to another page, or
generate HTML for the Java plugin.
37 10/19/2024
Elements of JSP-Actions
Action elements are basically predefined functions.
Following table lists out the available JSP Actions:
Syntax Purpose
38 10/19/2024
Elements of JSP-Actions
39 10/19/2024
JSP Implicit Objects
JSP supports nine automatically defined variables, which are
also called implicit objects. These
variables are:
Objects Description
40 10/19/2024
JSP Implicit Objects
This is the ServletContext object
application associated with
application context.
41 10/19/2024
First JSP program
<html>
<head>
<title>Welcome</title>
</head>
<body>
<h1>Hello World.</h1>
</body>
</html>
42 10/19/2024
First JSP program
<html>
<%
int a = 5;
int b = 8;
%>
<body>
The first number is : <%= a %> <br/>
The second number is : <%= b %>
<br/>
The sum is : <%= ( a + b ) %> <br/>
</body
</html>
43 10/19/2024
First JSP program
44 10/19/2024
JSP JDBC Example
<%@ page session="false" %>
<%@ page import="java.sql.*" %>
<%
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
System.out.println("JDBC driver loaded");
}
catch (ClassNotFoundException e) {
System.out.println(e.toString()); }
%>
<HTML>
<HEAD>
<TITLE>Display All Users</TITLE>
</HEAD>
<BODY>
<CENTER>
<BR><H2>Displaying All Users</H2>
45 10/19/2024
JSP JDBC Example
<BR>
<BR>
<TABLE>
<TR>
<TH>First Name</TH>
<TH>Last Name</TH>
<TH>User Name</TH>
<TH>Password</TH>
</TR>
<%
String sql = "SELECT FirstName, LastName, UserName, Password" +
" FROM Users";
try {
Connection con = DriverManager.getConnection(“com:mysql:jdbc:Driver");
Statement s = con.createStatement();
ResultSet rs = s.executeQuery(sql);
46 10/19/2024
JSP JDBC Example
while (rs.next()) {
out.println("<TR>");
out.println("<TD>" + rs.getString(1) + "</TD>");
out.println("<TD>" + rs.getString(2) + "</TD>");
out.println("<TD>" + rs.getString(3) + "</TD>");
out.println("<TD>" + rs.getString(4) + "</TD>");
out.println("</TR>");
}
rs.close();
s.close();
con.close(); }
catch (SQLException e) {}
catch (Exception e) { }
%>
</TABLE>
</CENTER>
</BODY>
</HTML>
47 10/19/2024