Professional Documents
Culture Documents
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:
<input type=text name=Text> <br><br>
<input type=submit value=Submit>
</form><br>
<h3>You entered:
<%= 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
JavaBeans
Example-1
A JSP page
that uses a bean
Scope of A JavaBeans
JavaBeans
Example-2
A JSP page
that uses a bean