Java Server Pages  Hot PDF Print E-mail
Tag it:
Delicious
Furl it!
Digg
NewsVine
Reddit
YahooMyWeb
Technorati
Articles Reviews JSP
Written by Munaf Sahaf   
Thursday, 12 April 2007
{mos_sb_discuss:34}

Java Server Pages is an extension of the Java Servlet technology where the java code is written inside the HTML pages which are then converted in Servlets and compiled and run on the server. It is used to create dynamic content on the server using HTML and java code which makes it platform independent.


Advantages:

• Separation of static from dynamic content.
• Dynamic content generation.
• Platform independent.
• Complete extension of the servlet API.

JSP Syntax - Basics

JSP syntax can be classified into

• Directives
• Declaration
• Scriplets
• Expression
• Standars Actions

Directives

• They act as Messages to the JSP Container from the JSP engine.
• They are used to set Global Values such as Class Declaration ,

Methods to be implemented, Output Content Type.

• They have scope of the entire JSP Page
• They do not produce any output to the client but they direct the JSP engine.
• Most important directives are page and include.
• They are characterized by @ char. <%@... %>

<%@ DirectiveName SName = Svalue %>


Types Of Directive

• Page

• Include

• Taglib


Page Directive

• It define a number of important attributes that affect the page.
• A Single JSP page can contain many page Directive but attribute/value pair must be unique.

(NOTE:There can be only one occurrence of any attribute except import attribute)

<%@ page AttibName = AttribValue %>

• the page directive is found at the top of JSP pages

Page Directive Attributes

• language

Defines the scripting language to be used.(Multiple Language support in Future)


• extends

The value is fully qualified class name of the super class

• import

Just like java code, comma separated list of packages or classes that need to be imported.

• session

Specifies if the page participates in an HTTP session

"true"

<%@ page session="true" %>

• isErrorPage

Indicates if the current page is used as the URL target of another JSP page's errorpage

"false"

if "true" then the implicit variable exception is available, and refers to the instance of the java.lang.Throwable thrown at run time by the JSP causing the error.

• errorpage

Defines a URL to another JSP that is invoked if an unchecked exception is thrown on the current page.

Include Directive

• This directive notifies the container to include the content of the specified resource in the current JSP at specified place.

• Content of the included file is parsed by the JSP at translation time

For Example

<%@ include file="header.html" %>

• The include directive lets you separate your content into more organized and reusable manner, such as those for including a common page header or footer.

• The page included can be a HTML or JSP content.

Declarations

• It is enclosed b/w <%! --- %>

• Declaration is a block of java code inside a JSP that is used to define class-wide variables and methods in generated class file.

• Declarations are initialized when JSP page is initialized and have class scope.
• Anything defined in declaration is available throughout JSP
• JSP declarations let you define page-level variables to save information or define supporting methods that the rest of a JSP page may need.

• Declarations are found within the <%! ... %> tag.
• Always end variable declarations with a semicolon, as any content must be valid Java statements:
<%! int i=0; %>

• You can also declare methods and even override the initialization event in the JSP life cycle by declaring:
<%! public void jspInit() {
//Your code
}
%>

Scriplets

• Block of Java Code that is executed at a request-processing time
• Enclosed b/w <% %>
• Is put into the service() method.
• It can produce output to the client.

Expression

• An Expression is a shorthand notation for a scriplet that outputs a Value.
• Enclosed b/w <%= … %>
• When the expression is evaluated, the result is converted to a String and displayed.

JSP – Comments

Although you can use HTML comments in JSP pages, but that is not may
not be the best way to do it because users can view these if they view
the page's source. So it is better to us JSP comments which will not
be displayed on the page's source

<%-- comment for server side only --%>

JSP - Implicit Objects

There are nine implicit objects available to a JSP page. These are• request: represents the HttpServletRequest triggering the service invocation.

• response: represents HttpServletResponse to the request.
• pageContext: encapsulates implementation-dependent features in PageContext.
• application: represents the ServletContext obtained from servlet configuration object.
• out: a JspWriter object that writes into the output stream.
• config: represents the ServletConfig for the JSP.
• page: synonym for the "this" operator, as an HttpJspPage.
• session: An HttpSession.
• exception: the uncaught Throwable object that resulted in the error page being invoked.

Standard Action

• Action are specific tags. They affect the runtime behavior of the JSP and the response send back to the client.
• Standard Actions provide page authors with somebasic functionality to exploit.
• JSP specification lists some action types that are standard.


User reviews

There are no user reviews for this item.

Add new review




Powered by jReviews

Last Updated ( Friday, 08 June 2007 )
 
< Prev   Next >