Вы находитесь на странице: 1из 29

 Java Server Pages Technology

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 Syntax ELEMENTS


 Just like any other language,the JSP scripting language has

well defined grammar and includes syntax element for


performing various tasks,such as declaring variables and
methods, writing expression and calling other JSP page.
JSP Technology
 JSP elements Types
 Directive
 Declaration
 Scriptlet
 Expressions
 Action
 Comment
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

 errorPage: To specify the URL of a JSP page used

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

 Also used to insert a part of the code common to multiple

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

default value, while local variable must be initialized


explicitly before they are used.
 Instance variable are created and initialized only once.

Thus variable declared in JSP declaration retain their


values across multiple requests.
 Local variables are created and destroy for every

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:forward age=“other.jsp” />



<jsp: param >: Used as a sub attribute with jsp:include and
jsp:forward to pass additional request parameters
 Attributes used are name and value
 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

 During the translation phase,the JSP engine reads a JSP


page, parses it and validate the syntax of the tag used.

In addition to checking the syntax, the engine perform other
validity also
 The attribute-value pairs in the directive and
standard action are valid.
 The same Java Bean name is not used more than
once in a translation phase.
 If we are using custom tag library, the library is
valid
 The usage of custom tag is valid.
JSP Technology
 Compilation
 In the compilation phase the java file generated in the

previous step is compiled using the normal java


compiler javac. All the java code that we write in
declaration,Scriplets and expression is validated during
this phase.
 For example: the following declaration tag is a valid

JSP tag and will pass to translation phase but it is not


valid java declaration statement because it does not end
with semicolon(;)
JSP Technology
 Loading and instantiation
 After successful compilation,the container load the

servlet class into memory and instantiate it.


 Generated servlet class for a JSP page implements
HttpJspPage interface extends the JspPage interface of
same package which in turn extends the Servlet interface
of the javax.servlet package.
 JspPage interface declares only two method jspInit() and
jspDestroy() that must be implemented by all JSP page
regardless of client-server protocol.
 HttpJspPage interface declares one method: _jspService()
 jspInit()
 The container calls the jspInit() to initialize the servlet
instance. It is called before any other method, and is
called only once for a servlet.
 _jspService()
 The container calls the _jspService() for each request,
passing it the request and response objects. All of the
HTML elements, JSP Scriplets and JSP expressions
become a part of method during the translation phase.
 jspDestroy()
 When the container decides to take instance of servlet it

calls jspDestroy().
 This is the last method which is called.

 Note

 We are not required to implement jspInit() and

jspDestroy() method because they are


automatically implemented.
 JSP implicit objects.
 application: the application variable is of type

javax.servlet.ServletContext. It refers to the environment of


the web application to which the JSP page belongs.
<%String path=application.getRealPath(“/web-
inf/counter.db”)%>
 out: To write to the output stream

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

Вам также может понравиться