JSP 29587
JSP 29587
JSP 29587
ARCHITECTURE
Goals of JSP
Simplify Creation of dynamic pages.
Separate Dynamic and Static content .
What are JSP?
JSP is a specification and not a product
but a specification.
JSP More on line of the J2EE
specification.
Can utilize the Beans to separate the code
out .
Is compiled to Servlet the first time call is
received for the page.
THE PROCESS
Uses JSP
First <JSP Tags>
Request JSP <Java code>
<JSP Tags>
<Java code>
Client JSP
Text <JSP Tags>
Uses <Java code>
Servlet
Generates
Response
A SIMPLE JSP CODE
%@page import=“java.util.Date”%
<html>
<body>
The current time is <%=new Date().toString()
%>
</body>
</html>
The Container
According to the JSP specifications that the
servlets implement the Servlets must extend from:
Specified by JSP author via. Extends
command.
Or
A container specific Implementation class
that implements javax.servlet.jsp.JSPPage
interface .
The JSP Life Cycle
public void jspInit() used for initialization
purposes defined in javax.servlet.jsp.JSPpage
Interface.
public void jspDestroy() used for performing
cleanup operations defined in
javax.servlet.jsp.JSPPage Interface.
public void _jspService(HttpServletRequest
request, HttpServletResponese response) throws
ServletException,IOException.
The implementation of this method is generated by
the container and should never be provided by
Page authors.
General Rules for JSP Pages
JSP tags are case sensitive.
Attribute values in tags always appear
quoted,Single or double.
Eg: <somejsptag attributename=“attribute
value”> body</somejsptag>
Or
<somejsptag attributename=“attribue value” />
The Second one is valid in case when there is no
body.
General Rules for JSP Pages
The character \ can be used as an escape
character eg: to use %.
URL that does not start with / are interpreted
relative to the current JSP.
URL starting with a / , called a context relative
path, is interpreted with reference to the web
application to which the JSP page belongs.
Any white spaces with in the body text of a
document are not significant although they are
preserved during translation into a servlet.
Types of JSP Tags
Directives.
Scripting Elements.
Actions.
JSP Directives
They serve as messages to the JSP container
from the JSP.
Used to set global values as
Class Declarations
Methods to be implemented.
import="java.rmi.*,java.util.*" session="true"
buffer="12kb"
request object.
At request time from an evaluated
expression.
From a specified string.
Syntax:
<jsp:setProperty name=“help” property=“word”
/>
<jsp:getProperty>
It is used to access the properties of a
bean.
It accesses a property , converts it into a
String, and prints it into the output stream
of the client.
Syntax:
<jsp:getProperty name=“name”
property=“propertyName” />
Example utilizing <jsp:useBean>
<jsp:setProperty> <jsp:getProperty>
For this example we will first of all create an
HTML file beans.html in which user can insert
his name and choose his favorite language from
the drop down menu . Another file beans.jsp will
be used to set the bean properties as per the values
entered by the user and then by utilizing the
methods defined in the LanguageBean.class it
retrieves the information and displays it to the
user.
beans.html
<html>
<head>
<title>useBean action test page</title>
</head>
<body>
<h1>useBean action test page</h1>
<form method="post" action="beans.jsp">
<p>Please enter your user name :
<input type="text" name="name">
<br>What is your favorite programming language?
beans.html continued..
<select name="language">
<option value="Java">Java
<option value="c++">c++
<option value="Perl">Perl
</select>
</p>
<p><input type="submit" value="submit information">
</form>
</body>
</html>
beans.jsp
<jsp:useBean id="languageBean" scope="page"
class="LanguageBean">
<jsp:setProperty name="languageBean"
property="*" />
</jsp:useBean>
<html>
<head>
<title>useBean action test result</title>
</head>
beans.jsp continued…
<body>
<h1>useBean action test result</h1>
<p> Hello, <jsp:getProperty name="languageBean"
property="name"/>.</p>
public LanguageBean() {}