JSP Java Server Pages
JSP Java Server Pages
<%=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
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