JSP Technology
Objectives
Write the opening and closing tags for the
following JSP tag types
Directives
Declaration
Scriplet
Expression
JSP Technology
Identify JSP Life cycle
Page translation
JSP page compilation
Load class
Create instance
Call jspInit
Call _jspService
Call jspDestroy
JSP Technology
Identify page Directive attribute and its
value
Import a java class into JSP page
Declare that a JSP page exists within a session
Declare that a JSP page uses an error page
Declare that a JSP page is an error page
JSP Technology
JSP Directives
Provide global information about a particular JSP page
Can be of three types: page, include, and taglib.
JSP Technology
Page directive:
Notifies about the general settings of a JSP page
Attributes include:
contentType: To specify the MIME type of the
response
extends: To specify the name of the parent class
to trap errors
JSP Technology
isErrorPage (Boolean attribute): To specify if a JSP can be
used as an error page
import: To specify the names of the packages available for a
JSP page
language: To specify the scripting language
session: To specify the availability of session data for a JSP
page.
buffer:Specifies the size of the output buffer. If buffer size is
specified it must be specified in kilobytes.
autoFlush: A boolean literal indicating whether the buffer
should be flushed when it is full.
isThreadSafe:A boolean value indicating whether the JSP
page is thread safe.
JSP Technology
Include directive:
Used to specify the name of the files (in the form of their
relative URL) to be inserted while compiling the JSP page
Contents of the inserted files become part of the JSP page
pages
Example:
<%@ include=”bnkHeader.html” %>
About taglib directive we will learn in custom tags.
JSP Technology
Declaration
Declaration declare and define variables and methods that can be used in the
JSP page.
Syntax:
<%!<data type><variablename>=<value>%>
Example
<%!
int i=0;
int j=0;
int z=0;
int prod=0;
%>
JSP Technology
Note:
Instance variables are automatically initialized to their
request.
JSP Technology
Scriplets
Consists of valid code snippets enclosed within the <%
and %> JSP tags
Example:
To accept the user name and display the name 10
times:
<%@ page import="java.util.*" %>
<%@ page language="java" %>
<HTML>
<BODY>
JSP Technology
<%
String name=(request.getParameter("t1"));
out.println("<HTML>");
out.println("<BODY>");
out.println("<h3>A Scriplet example</h3>");
for(int i=0;i< 10;i++)
{
out.println("<BR>");
JSP Technology
out.println(" Hello" + " " + name + " " + "Count" + i );
}
out.println("</BODY>");
out.println("</HTML>");
%>
</HTML>
</BODY>
JSP Technology
JSP Expressions
Used to directly insert values into the output
Example:
<%=msg%>
JSP Technology
JSP Actions
Used to perform tasks such as:
insertion of files
reusing beans
forwarding a user to another page
instantiating objects
JSP Technology
Include actions such as:
<jsp:useBean>: To find and load an existing bean
Attribute used are id, class, scope, and beanName
<jsp:useBean id=“bean1” class=“bean.class” />
<jsp:getProperty>: To retrieve the property of the specified
bean and direct it as output.
Attributes used are name and property.
<jsp:setProperty>: To set the property of the specified bean
Attributes used are name, property, value, and param
JSP Technology
<jsp:forward>: To forward a request to a different page
Attribute used is page
Example:
<jsp:include page=“somepage.jsp”>
<jsp:param name=“name1” value=“value1” />
<jsp:param name=“name2” value=“value2” />
</jsp:include>
<jsp: include >: To insert a file into a particular JSP page
Attributes used are page and flush
JSP Technology
JSP comments
In jsp comments can be included with <%--text--%>
A comment cannot be inside another comment.
JSP Technology
JSP Page Life Cycle
Translation
calls jspDestroy().
This is the last method which is called.
Note
out.println(“hello”)
page: To represent the current instance of the JSP page
request: To represent an object of HttpServletRequest used
to retrieve the request data
Response: To represent an object of HttpServletResponse
used to write the HTML response output
Session: To represent a session object