JSP :-Introduction
Jsp technology is used to web application like servlet.It can be thought as extension of servlet as it provides more functionality more than that of servlet like expression language,jstl etc.
Life Cycle of JSP:
1- Translation
2- Compilation
3- Classloading(class file is loaded by classloader)
4- Instantiation(Object Of generated servlet is created )
5- Initialization( jspInit() method is invoked by container)
6- Request Processing( _jspService() method is invoked by container)
7- Destroy( jspDestroy() method is invoked by container)
Note: in _jspService() ,underscore points out that it can not be overrides.
Jsp scriptlet tag(Scriptlet Elements):-
1-scriptlet tag
2-expression tag
3-Jsp Declaration tag
1-scriptlet tag
This one is used to write the java source code.for eg. <% java source code %>
2-expression tag
This one replacement of output stream in case of servlet.
<%= source code %> For eg..
Note:-
Basic Difference between Jsp declaration and expression tag.
Jsp declaration tag only declare the variable and methods and most thing is the it is placed out side the
_jspService() method so that it doesn’t memory get every time when service method is called.
For eg.
JSP Standard Tag Library:-
The JSP Standard Tag Library represents a set of tags to simplify the JSP development.JSTL mainly 5 types of tags:-
1-core tags
2-Sql tags
3-xml tags
4-internationalization tags
5-Functions tags
The JSTL core tag provides variable support, URL management, Flow Control, etc.the url for the core tag is http://java.sun.com/jsp/jstl/core
The prefix of the core tag is c.There are following jstl core tags that are given below:-
Tag | Description |
| Like <%= ... >, but for expressions. |
| Sets the result of an expression evaluation in a 'scope' |
| Removes a scoped variable (from a particular scope, if specified). |
| Catches any Throwable that occurs in its body and optionally exposes it. |
| Simple conditional tag which evalutes its body if the supplied condition is true. |
| Simple conditional tag that establishes a context for mutually exclusive conditional operations, marked by and |
| Subtag of that includes its body if its condition evalutes to 'true'. |
| Subtag of that follows tags and runs only if all of the prior conditions evaluated to 'false'. |
| Retrieves an absolute or relative URL and exposes its contents to either the page, a String in 'var', or a Reader in 'var Reader'. |
| The basic iteration tag, accepting many different collection types and supporting subsetting and other functionality . |
| Iterates over tokens, separated by the supplied delimeters. |
| Adds a parameter to a containing 'import' tag's URL. |
| Redirects to a new URL. |
| Creates a URL with optional query parameters. |
Snapshot of Code
2-Sql tags:-
provides SQL support and its url is http://java.sun.com/jsp/jstl/sql
The prefix of this tag is sql.
The JSTL SQL tag library provides tags for interacting with relational databases (RDBMSs) such as Oracle, mySQL, or Microsoft SQL Server.
Following is the syntax to include JSTL SQL library in your JSP:
Following is the list of SQL JSTL Tags:
Tag | Description |
| Creates a simple DataSource suitable only for prototyping |
| Executes the SQL query defined in its body or through the sql attribute. |
| Executes the SQL update defined in its body or through the sql attribute. |
| Sets a parameter in an SQL statement to the specified value. |
| Sets a parameter in an SQL statement to the specified java.util.Date value. |
| Provides nested database action elements with a shared Connection, set up to execute all statements as one transaction. |
3-xml tags:-
Provides Flow control and transformation etc.
Its url is http://java.sun.com/jsp/jstl/xml
The prefix is x.
The JSTL XML tags provide a JSP-centric way of creating and manipulating XML documents. Following is the syntax to include JSTL XML library in your JSP.
The JSTL XML tag library has custom tags for interacting with XML data. This includes parsing XML, transforming XML data, and flow control based on XPath expressions.
Before you proceed with the examples, you would need to copy following two XML and XPath related libraries into your \lib:
[1]- XercesImpl.jar: Download it from http://www.apache.org/dist/xerces/j/
[2]- xalan.jar: Download it from http://xml.apache.org/xalan-j/index.html
Following is the list of XML JSTL Tags:
Tag | Description |
| Like <%= ... >, but for XPath expressions. |
|
Use to parse XML data specified either via an attribute or in the tag body. |
| Sets a variable to the value of an XPath expression. |
| Evaluates a test XPath expression and if it is true, it processes its body. If the test condition is false, the body is ignored. |
| To loop over nodes in an XML document. |
| Simple conditional tag that establishes a context for mutually exclusive conditional operations, marked by and |
| Subtag of that includes its body if its expression evalutes to 'true' |
| Subtag of that follows tags and runs only if all of the prior conditions evaluated to 'false' |
| Applies an XSL transformation on a XML document |
4-internationalization tags:-
Provides message formatting,number and date formatting etc. the url for this is
http://java.sun.com/jsp/jstl/fmt
and prefix is fmt.
The JSTL formatting tags are used to format and display text, the date, the time, and numbers for internationalized Web sites. Following is the syntax to include Formatting library in your JSP:
Following is the list of Formatting JSTL Tags:
Tag | Description |
| To render numerical value with specific precision or format.. |
|
Parses the string representation of a number, currency, or percentage. |
| Formats a date and/or time using the supplied styles and pattern |
| Parses the string representation of a date and/or time |
| Loads a resource bundle to be used by its tag body. |
| Stores the given locale in the locale configuration variable. |
| To display an internationalized message. |
| Specifies the time zone for any time formatting or parsing actions nested in its body. |
5-Functions tags:-
Provide for String manipulation and length url of this is http://java.sun.com/jsp/jstl/functions
Its prefix is fn.
JSTL includes a number of standard functions, most of which are common string manipulation functions. Following is the syntax to include JSTL Functions library in your JSP:
Following is the list of JSTL Functions:
Tag | Description |
fn:contains() | Tests if an input string contains the specified substring. |
fn:containsIgnoreCase() | Tests if an input string contains the specified substring in a case insensitive way. |
fn:endsWith() | Tests if an input string ends with the specified suffix. |
fn:indexOf() | Returns the index withing a string of the first occurrence of a specified substring. |
fn:join() | Joins all elements of an array into a string. |
fn:length() | Returns the number of items in a collection, or the number of characters in a string. |
fn:split() | Splits a string into an array of substrings. |
fn:startsWith() | Tests if an input string starts with the specified prefix. |
fn:substring() | Returns a subset of a string. |
fn:substringAfter() | Returns a subset of a string following a specific substring. |
fn:toUpperCase() | Converts all of the characters of a string to upper case.. |
JSP implicit Objects:-
There are 9 implicit objects that are availabale for all jsp pages.
A list of the 9 implicit objects are given below:
Object | Type |
out | JspWriter. |
request | HttpServletRequest |
response | HttpServletResponse |
config | ServletConfig |
application
| ServletContext |
session | HttpSession |
pageContext | PageContext. |
Page | Object. |
exception | Throwable. |
JSP directives:
These are the message which tells to container how to translate a JSP page into the corresponding servlet.
There are three type of directive:-
[1]-Page directive
[2]-Include directive
[3]-Taglib directive
[1]-Page directive:-
Page directive defines the attribute that apply an entire JSP page.
Attributes of JSP pages are:-
[1]- import
[2]- contentType
[3]- extends
[4]- info
[5]- buffer
[6]- language
[7]- isELIgnored
[8]- isThreadSafe
[9]- autoFlush
[10] session
[11]- pageEncoding
[12]- errorPage
[13]- isErrorPage
[2]-include directive:-
this is used to include the contents of any resourcesit may be jsp file,html file or text file,since JSP page is translated only once so better to use static resources.
<% include file=”resourcesName”%>
<% include file=”header.html”%>
[3]-Taglib:-
The JavaServer Pages API allows you to define custom JSP tags that look like HTML or XML tags and a tag library is a set of user-defined tags that implement custom behavior.
The taglib directive declares that your JSP page uses a set of custom tags, identifies the location of the library, and provides a means for identifying the custom tags in your JSP page.
The taglib directive follows the following syntax:
Where the uri attribute value resolves to a location the container understands and the prefix attribute informs a container what bits of markup are custom actions.
You can write XML equivalent of the above syntax as follows:
Check more detail related to taglib directive at Taglib Directive.
Example:
suppose the custlib tag library contains a tag called hello. If you wanted to use the hello tag with a prefix of mytag, your tag would be and it will be used in your JSP file as follows:
Advantage Of JSTL over JSP scriptlet tags:-
1- Automatic JavaBean Introspection Support-
if you consider that JSTL's expression language handles JavaBean introspection automatically—which means you don't need to downcast objects you retrieve as scoped attributes—
Because of JSP's automatic support of JavaBean introspection, this block of code becomes much more simple in JSTL.
2-Standardized Support for Formatting and I18N:-
In proper Web design, JSP pages should be responsible only for the presentation of data. Two very common components to this presentation are the formatting of numbers and strings, and internationalization (I18N) support. Prior to JSTL, there was no official way to support these two features in JSP scriptlet code. Many companies developed their own sets of objects that could be used to support multiple languages. Data formatting was handled in a similar fashion
JSP EL(Expression Languange):-
Expression Language (EL) makes it possible to easily access application data stored in JavaBeans components. JSP EL allows you to create expressions both (a) arithmetic and (b) logical. Within a JSP EL expression, you can use integers, floating point numbers, strings, the built-in constants true and false for boolean values, and null.
Simple Syntax:-
${expr}
Note :-
When the JSP compiler sees the ${} form in an attribute, it generates code to evaluate the expression and substitues the value of expresson.
0 comments:
Post a Comment