Java Server Page
Click here to load reader
-
Upload
rajesh-vhadlure -
Category
Documents
-
view
212 -
download
0
Transcript of Java Server Page
Java Server Page
Web application creating is very complex task involving lot of man power require of two
basic field, one is making business logic and another is to create strong UI design i.e. web
application is divided into clearly two part ( code part and UI part).
JSP (Java Server Page) is standard java extension use to simplify creation and management of
dynamic web pages. The JSP allows us to separate dynamic content of the web page from it
static presentation content. The programming in servlet is very complex. It require additional
file like web.xml and java class files to make web application while JSP offers an easier
approach to develop dynamic web page. Here JSP program contains html tags and special
type of tag’s known as JSP tags.
The JSP tags are used to add dynamic content to the web pages. A JSP page is first converted
into a Servlet by JSP translator. This servlet is then compiled and execute to generate output
for the browser. The use of JSP is having following benefit.
1. JSP pages are portable and use easily across multiple platforms and web server’s
without making any changes.
2. Programming in JSP is easier than servlet because it uses tag base approach.
3. JSP pages are automatically compiled by the web server. Developers need not to be
performing compilation operation explicitly.
4. The JSP uses MVC architecture for making the application.
What are the advantages of JSP over Servlet?
1. As like servlet JSP pages cannot be require compile explicitly also we can make
changes in JSP pages without taking burden of its recompilation.
2. JSP pages are easier to write than the servlet since it follows tag based approach.
3. We can access JSP pages directly from the web browser where as Servlet have to be
firstly map in the web.xml file.
4. we can separate presentation and business logic
5. We can reduce development time.
CompilationTranslation
Loading
InstantiationInitialization
Servlet (.Java)Servlet (.class)
Service
Destruction
HTTPRequest
HTTPResponse
Already initialized
JSP Life-Cycle:-
JSP’s life cycle can be grouped into following stages.
1. Translation : - In this stages web container translate (JSP translator) JSP document
into the corresponding java port i.e. Servlet. In this state web application perform
operations like.
1. Locate the request JSP page.
2. Validating syntaxes correcting JSP page.
3. Interpretation of JSP directives, action and custom actions.
4. Writing source code into equivalent servlet.
5. The generated servlet class is subtype of java.Servlet.jsp class.
6. The generated servlet implements jspInit(), jspService(), jspDestroy().
This translated Servlet is different from a normal Servlet only in the sense that
it implements the interface javax.servlet.jsp.HttpJspPage.
2. Compilation : - This is stage where servlet source code is compile into java byte code
i.e. servlet class file. Translation and compilation can occur anytime between
deployment and first request made by the user.
Web container or web Service
3. Loading : - In this stage web container loads the generated servlet class file into
memory of web container using class loader.
4. Instantiation : - After successful loading of servlet class, JSP Container creates
instance of servlet class.
5. Initialization : - after servlet object instantiation, JSP container initializes the
instantiation object. Here jspInit () is called. jspInit () is called only once during JSP
life cycle.
6. Service : - This method is called for every request of this JSP during its life cycle.
This is where it serves the purpose of creation. This method cannot be overridden.
7. Destruction : - The servlet instance is taken out of the memory by invoking
jspDestroy ().
JSP basic tags:- (predefined tags) :-
JSP having three types of predefined tags
1. Directive tags
2. Scripting elements
3. Actions
JSP is a presentation level component. Hence it supports direct use of HTML and
XML tags. Along the tags there are certain tags included in JSP to perform specific
task. Concern servlet is affected by these tags.
1. Directive tags:-
Directive affects overall structure of the servlet that result from translation. They are
giving direction to the JSP translator. During the translation stage of JSP life cycle
directive tags are use of JSP life cycle. Directive tags are use to set global values such
as class declaration, methods to be implemented, output content type etc.
There are three type of directive
a. page directive
b. include directive
c. taglib directive
a. Page directive: - page directive is use to provide precompilation to the JSP
compiler. At the time of JSP is getting converted into servlet. These instructions
affect various properties of JSP page.
Syntax:- <%@ page attributes="value" %>
Attributes Description
languageSpecifies the language used in the page for JSP. The default
value of this attribute is "java".
extends Specifies whether the page uses an extended class
import Specifies list of packages of classes to be imported
sessionIt is used to make a session value available to a jsp page; this
attribute has a "boolean" value and its "true" by default.
autoFlushIf the value of autoflush is true then it automatically flushes
buffer as soon as it become full.
buffer Specifies the pages buffer size
isThreadSafe
If set is true then JSP engine maximum send multiple client
request at the same time, if set to false only one request is
sent.
errorPage Specifies the error page location for unhandled exceptions.
contentType Specifies the content and mime type.
isErrorPageSpecifies whether to use the exception object, it is set to
"true" to use the exception object, else it can be set to "false"
For example:-
<% @ page import="java.util.Date" %>
<% @ page language=”java” %>
<%@ page buffer="16kb" autoFlush=”false” %>
<%@ page errorPage="myerrorpage.jsp" %>
<%@ page isErrorPage="true" %>
b. include directive:-
The include directive is used to include the contents of any resource it may be jsp file,
html file or text file at compile time. We can merge contents of one or more file during
compiler. The included file can be static or dynamic.
Syntax: <%@ include file="filename" %>
For example: <%@ include file=”test.jsp” %>
c. taglib directive:-
Taglib directive is used to define the tag library which is a collection of tags. A Tag
library can be a standard (pre-defined) or a custom (user-defined) one. The "prefix"
attribute is a must for custom as well as standard tags in jsp.
Syntax: <%@ taglib uri="URI" prefix="unique_name" %>
Example: - <%@ taglib uri="http://www.javatpoint.com/tags" prefix="mytag" %>
JSP Implicit Object:-
JSP Implicit objects are created by the web container. These implicit objects are Java objects
that implement interfaces in the Servlet and JSP API. Scripting elements in a JSP page can
make use of these JSP implicit objects. There are nine (9) JSP implicit objects available.
1.