Академический Документы
Профессиональный Документы
Культура Документы
Servlet
What is JSP Page?
JSP is a technology based on the Java language and enables development of
dynamic web sites
Dynamic content
Java code
Displaying properties of JavaBeans
Invoking business logic defined in Custom tags
Servlet
JSP
Separation of dynamic and static content
This allows for the separation of application logic and Web page design, reducing the complexity of
Web site development and making the site easier to maintain.
Platform independence
Because JSP technology is Java-based, it is platform independent. JSPs can run on any nearly any
Web application server. JSPs can be developed on any platform and viewed by any browser
because the output of a compiled JSP page is HTML.
Component reuse
Using JavaBeans and Enterprise JavaBeans, JSPs leverage the inherent reusability offered by
these technologies. This enables developers to share components with other developers or their
client community, which can speed up Web site development.
Servlet
A Simple JSP Page
(Blue: static, Red: Dynamic contents)
<html>
<body>
Hello World!
<br>
Current time is <%= new java.util.Date() %>
</body>
</html>
Output
Servlet
Difference between JSP and Servlet
Servlet
JSP Benefits
Automatic deployment
Recompile automatically when changes are made to JSP pages
Platform-independent
Servlet
Why JSP over Servlet?
Servlet
Separate Request processing From Presentation
JSP is the separation of contents generation logic from presentation logic. Here the
contents generation includes business logic processing.
both servlet and JSP are used together in which servlet is used for controlling and
dispatching while JSP page is used for displaying. This slide shows exactly that
scenario.
On the left, you have a pure servlet based design where contents generation and
presentation logic are all handled within a servlet.
Now on the right side of the picture where servlet and JSP are used together,
servlet performs controlling and dispatching while JSP is used for displaying.
Servlet
Separate Request processing From Presentation
Servlet
JSP Architecture
Servlet
JSP Architecture
The Web browser makes a request to the JSP page.
The JSP engine creates temporary servlet source code based on the contents of
the JSP. The generated servlet is responsible for rendering the static elements of
the JSP specified at design time in addition to creating the dynamic elements of the
page.
The servlet source code is compiled by the Java compiler into a servlet class file.
The servlet is instantiated. The init and service methods of the servlet are called,
and the servlet logic is executed.
The combination of static HTML and graphics combined with the dynamic elements
specified in the original JSP page definition are sent to the Web browser through the
output stream of the servlets response object.
Servlet
Java Server Pages
JSP is a technology based on the Does not embed HTML into Java
Java language and enables
development of dynamic web Embeds special active elements
sites (Java) into HTML pages
Servlet
JSP Page Lifecycle Phases
Translation phase
translation phase in which a JSP page gets translated into a servlet code,
Compile phase
compilation phase in which servlet code gets compiled
Execution phase
execution phase in which servlet instance then serves client request. Usually
translation and compilation phases occur together.
Servlet
Translation/Compilation Phase
A JSP page gets translated and compiled automatically if there is no complied class already. So
if you deploy JSP page for the first time.
if you modified the JSP page, then translation and compilation will occur
During the translation phase, each type of data in a JSP page is treated differently
Static data
Static data (called as template data) is transformed into Java code that will emit the data into the
output stream which returns data to the client.
Dynamic data
Directives are used to control how the Web container translates and executes the JSP page.
Scripting elements are directly inserted into the JSP page's servlet class unmodified.
Elements of the form <jsp:XXX ... /> are converted into method calls to JavaBeans components
or invocations of the Java Servlet API.
Servlet
JSP Lifecycle Methods during Execution Phase
If an instance of the JSP page's servlet does not exist, the container Loads the
JSP page's servlet class
Container then initializes the servlet instance by calling the jspInit method
Servlet
Initialization of a JSP Page
Servlet
Components of Java Server Pages
Directives
Declarations
Scriptlets
Comments
Expressions
Servlet
JSP directives
Directives provide additional information to the JSP container and describe attributes for page
The directives do not produce any output to the current output stream.
Directive Purpose
include Includes the contents of a file into the JSP at translation time
taglib Makes a custom tag library available within the including page
syntax :
< % @ directive [ attr = "value ] % >
Or
Servlet
The page directive
Servlet
Ex :
<jsp:directive.page import="java.util.Date"/>
<HTML>
<BODY>
Hello World.
The local server time is <%= new Date() %>.
</BODY> </HTML>
Servlet
page directive attributes
Other page attributes control page buffering, exception handling, the scripting
language, and so forth.
Attribute Use
language This attribute defines the scripting language to be used in the scriptlets,
expressions, and declarations within the body of the translation unit
extends Specifies the superclass of the class to which this JSP is transformed. It
should be defined as a fully qualified class name. It is implementation-
dependent, and you should use it with caution. Each implementation
provides its own default class; overriding the default can affect the quality
of the implementation.
import Describes all Java types available as imports. It follows the same rules as
standard Java language import statements. By default, java.lang.*,
javax.servlet.http.*, javax.servlet.*, and javax.servlet.jsp.* are imported. You
can add any additional packages or types you need imported for your page.
Servlet
Session Indicates whether the page requires participation in an HttpSession. By
default, the value is true. You have the option of specifying the value as false
(in that case, any reference to HttpSession in the page will not be resolved).
buffer This page attribute indicates whether the content output from the page will be
buffered. By default, the page output is buffered with an implementation
buffer size no smaller than 8 KB.
autoFlush Specifies whether the buffered output should be flushed automatically (true
value) when the buffer is filled, or whether an exception should be raised
(false value) to indica
info Defines a string that can be incorporated into the translated page.
isErrorPage Indicates if the current JSP is the intended target of another JSP's errorPage.
The default value is false.
ErrorPage Defines a URL to a resource that will catch the current page's exception.
ContentType Defines the MIME type and the character encoding for the response of the
JSP. Values are either of the form "TYPE" or "TYPE;charset= CHARSET".
pageEncoding Defines a URL to a resource that will catch the current page's exception.
Servlet
The include directive
The include directive includes the content of the named file directly into JSP's
source, as it is compiled. The two ways to write a JSP include directive:
EX :
<jsp:directive.include file="/copyright.html" />
<HTML>
<HEAD><TITLE>Hello World JSP Example</TITLE></HEAD>
<BODY>
Hello World.
</BODY>
</HTML>
The included file becomes part of JSP page when the page is translated.
Servlet
The taglib directive
The ability to create and use custom tag libraries is one of the most powerful and
useful features JSP offers.
This feature enables developers to define new XML-style tags for use by page
designers;
JSPs that use custom tags have a much cleaner separation of interface design
and business logic implementation than JSPs with a lot of embedded Java code
Servlet
Example
<jsp:directive.taglib uri="jdg.tld" prefix="jdg" />
<HTML>
<HEAD><TITLE>Hello World JSP Example w/Current
Time</TITLE></HEAD>
<BODY>
Hello World.
The local server time is <jdg:DATE />.
</BODY>
</HTML>
The first line is a taglib directive.
The uri attribute tells the JSP container where to find the taglib definition.
The prefix attribute tells the JSP container that will use jdg: as a prefix for the tag.
This prefix is mandatory and prevents namespace collisions.
The jsp prefix is reserved by Sun.
The tag <jdg:DATE /> results in the use of a custom tag handler class. In this
handler class, the current date and time are put into the output stream.
Servlet
JSP declarations
Declarations declare new data and function members for use within
the JSP.
JSP declaration in two ways:
<%! java declarations % >
or
< jsp: declaration > java declarations < / jsp:declaration >
Servlet
JSP declaration example
To uses a JSP declaration to declare a class variable and some class functions.
<jsp:directive.page import="java.util.Date"/>
<jsp:declaration>
private static String loadTime = new Date().toString(); // variable
private static String getLoadTime() // function
{
return loadTime;
}
private static String getCurrentTime() //function
{
return new Date().toString();
}
</jsp:declaration>
<HTML>
<HEAD><TITLE>Hello World JSP Example</TITLE></HEAD>
<BODY>
Hello World.<BR>
This page was loaded into memory at <%= getLoadTime() %>.<BR>
The current time is <%= getCurrentTime() %>
</BODY>
</HTML>
Servlet
JSP expressions
Example :
Servlet
JSP scriptlets
Servlet
Scriptlet example
The following JSP uses scriptlets to execute differently depending on the browser
<jsp:scriptlet>
String userAgent = (String) request.getHeader("user-agent");
</jsp:scriptlet>
<HTML>
<HEAD><TITLE>JSP Scriptlet Example</TITLE></HEAD>
<BODY>
<jsp:scriptlet>
if (userAgent.indexOf("MSIE") != -1)
{
</jsp:scriptlet>
<p>You are using Internet Microsoft Explorer.</p>
<jsp:scriptlet>
}
else
{
</jsp:scriptlet>
<p>You are not using Internet Microsoft Explorer.
You are using <%= userAgent %></p>
<jsp:scriptlet>
}
</jsp:scriptlet>
</BODY> </HTML>
Servlet