You are on page 1of 22

JSP

java server pages


A JSP is an HTML file that has Java servlet code
embedded in it in special tags . When you run a
JSP, all the HTML is automatically sent as part of
the response, along with any HTML thats created
by the Java code you embed in the JSP file. As a
result, JSP spares you the chore of writing all
those out.println statements.
A Java Server Page is an HTML document thats
saved in a file with the extension .jsp instead of
.htm or .html. Unlike servlet class files, you can
store a JSP file in any directory thats available to
the Web server.
The first time a user requests a JSP file, the JSP
file is run through a translator program that
converts the file into a Java servlet program and
compiles it. All the HTML from the original JSP
file is converted to out.print statements that send
the HTML to the response, and the Java
statements from the JSP file are incorporated
into the servlet program. Then, the servlet
program is executed and the results sent back to
the browser.
When you create a JSP, you intermix special JSP elements with your
normal HTML. You can include four
types of JSP elements:
Directives: Directives let you do things such as specify what
import statements the servlet requires, specify whether the servlet is
thread-safe, and include other source files in the servlet.
<%@ page attribute=value %>
Expressions: An expression can be any Java expression.
Expressions assume the following form:
<%= expression %>
Scriptlets: A scriptlet is a sequence of Java statements that
are inserted directly into the servlet code generated for the JSP.
Scriptlets have the following form:
<% statements %>
Declarations: A declaration is Java code that is placed in the
servlet class outside of any methods. You use declarations to create
class variables
or define methods that can be called by scriptlets or expressions.
Declarations take on this form:
<%! statements %>
Directives

<%@ page import=java.util.* %>


Expressions

<%=new java.util.Date()%>
----------- or ---------------
<%@ page import=java.util %>
<%=new Date()%>
<%= request.getParameter(Name)%>
Expressions
Example
<html>
<head>
<title>Input JSP</title>
</head>
<body>
<form action=InputJSP.jsp method=post>
Enter some text:&nbsp;
<input type=text name=Text> <br><br>
<input type=submit value=Submit>
</form><br>
<h3>You entered:&nbsp;
<%= request.getParameter(Text)%></h3>
</body>
</html>
Scriptlets
Example-1
<html>
<%@ page import=java.text.* %>
<%@ page import=java.util.* %>
<head>
<title>Date JSP</title>
</head>
<body>
<h1> Today is
<%
DateFormat df = DateFormat.getDateInstance(DateFormat.FULL);
Date today = new Date();
String msg = df.format(today);
out.println(msg);
%>
</h1>
<h1>Have a nice day!</h1>
</body>
</html>
Scriptlets
Example-2

<html>
<head>
<title>
Cant you see Im trying to work here?
</title>
</head>
<body>
<% for (int i = 0; i < 12; i++)
{
%>
All work and no play makes Jack a dull boy.<br>
<%
}
%>
</body>
</html>
Declarations
Example-1

<html>
<%@ page import=java.text.* %>
<%@ page import=java.util.* %>
<head>
<title>Counter JSP</title>
</head>
<body>
<h1>
This JSP has been displayed <%= count++ %>
time.
</h1>
</body>
</html>
<%!
private static int count = 1;
%>
Declarations
Example-2
<html>
<%@ page import=java.text.* %>
<%@ page import=java.util.* %>
<head>
<title>Date JSP</title>
</head>
<body>
<h1> Today is <%= getDate() %></h1>
<h1>Have a nice day!</h1>
</body>
</html>
<%!
private String getDate()
{
DateFormat df =DateFormat.getDateInstance(DateFormat.FULL);
Date today = new Date();
return df.format(today);
}
%>
Exercise
JavaBeans
A JavaBean is any Java class that conforms to
the following rules:
It must have an empty constructor. That is, a
constructor that accepts
It must have no public instance variables. All the
instance variables defined by the class must be
either private or protected.
It must provide methods named getProperty and
setProperty to get and set the value of any
properties the class provides, except for
boolean properties that use isProperty to get the
property value.
JavaBeans

Example-1
A JSP page
that uses a bean
Scope of A JavaBeans
JavaBeans
Example-2
A JSP page
that uses a bean

You might also like