Search This Blog

JSP Concepts

Concepts :
-Responsibilities of servlet
-what is JSP?
-Servlet vs JSP
-JSP Overview and Functioning
-JSP Elements
 
Responsibilities of servlet
  •     Coding the presentations logic together is not a good practice
                -A change in any one of these requires the entire code
                -Programmer with different skill sets are requires for creating and maintaining these
  •     Servlets, being java programs, are best suited for coding business logic.
  •     Servlets are not suitable to code presentation logic
                           -It is not easy to mix static contents with dynamic contents in Servlets
                           -As Servlets are not as easy as HTML, it will be difficult for web designs to use this technology
  •      A technology with power of Servlets and ease of HTML is required to code presentation logic, so that web designers can also easily use it
JSP

  • JSP or Java Server Pages is a technology developed by Sun Microsystems for coding the presentation layer of an enterprise application.
  • A JSP file is very similar to an HTML file
  •  Unlike HTML files, JSP files will contain some Java code also with in <% %> tags
  • When a client requests for a JSP, the Server (For example, Tomcat) sends the HTML tags as such to the browser
  • The code between <% and %> will be complied, executed and the output will be send to the browser
JSP
-    Can be used to code presentation logic
-    Can be used to generate dynamic pages
-    Is as simple as HTML, even web designers can use it easily

SERVLETS VS JSP
  • Servlet
-    Bits of HTML embedded in Java code
-    Suitable for coding the business layer of an enterprise application
-    Created and maintained by Java programmers
  • JSP
-    Bits of Java code embedded in HTML
-    Suitable for coding the presentation layer of an enterprise application
-    Created and maintained by web designers

JSP Overview
  • Internally, the JSP is getting converted to a Servlet
  • When a user requests for a .jsp file the first time, the JSP Container will create a servlet that would produce the output that .jsp file is supported to produce
    • The Servlet is complied, run and the output is given to the browser
    • Starting from the second request, there is no overhead of compilation
    • Starting from the second request, there is no overhead of compilation
Implicit Objects

  •  A JSP developer can use some implicit objects
  •  Some of the important objects and their classes are as follows
      • -    Out ( JSP Writer)
      • -    Request ( HttpServletRequest )
      • -    Response (HttpServletResponse )
      • -    Session ( HttpSession )
      • -    Config ( ServletConfig )
      • -    Exception ( Throwable )
  •    These objects will be decleared by the generated Servlet and hence the statements we write in JSP using these variables will get a meaning once they are pasted in the Servlets code
Implicit Objects – Example
  •  This example uses the out, request and session objects
Tags in JSP
  • Tags in JSP can be categorized as
  • -    Comments
  • -    Scripting elements
  • -    Directive elements
  • -    Action elements
  • -    Template data
•    Anything other than the above mentioned four categories fall under template data
•    This will include all HTML tags and text

Comments
Just like any other HTML tag, standard HTML comment tag also can be used in JSP.

Scripting Elements
  • Scripting elements are elements in the page that include the Java code
  • JSP can contain 3 types of Scripting elements
  •    Declarations
  •    Expressions
  •    Scriptlets
Scripting elements – declaration of variables
  •  Variables declared within <% and %> will be local to the service method of the generated of the generated Servlet and each request
Scripting Elements – Declaration of methods
  •  Methods also can be declared using scripting elements
  •  All methods should be declared within <%! and %> and they become a method of the generated Servlet
Scripting of Elements – Declaration of JSP Lifecycle methods

  •  The code that should be executed only once when the JSP is unloaded from the memory for the first time can be coded in a method jspInit()
  •  The jspInit() method will be executed only once per JSP is invoked for the first time can be coded in a method jspInit()
  •  The jspInit() method will be executed  only per JSP, not per request
  •  Typically this method contains code for initialization
  •  The code that should be executed only once when the JSP is unloaded from memory can be coded in a method jspDestory()
  •  Typically this method contains code to cleanup the resources used by the JSP
Scripting Elements – Expressions
 The value of an expressions can be printed to the browser using the syntax <%=expression%>

Scripting Elements – Scriptlets
    The java statements that appear between <% and %> are called scriptlets


Directive Elements
  • Directive elements provide information to the container about the page
  • JSP can contain three types of directives
  •  Page
  •  Include
  •  Taglib
The page Directive
  • The page directive has the following form
                    The page Directive – import
                    The page Directive - session
                    The page Directive – contentType
  • The contentType attribute
-    Example
The page Directive – errorPage
  • The errorPage attribute
-    Example
  • The page Directive – isErrorPage
-    Error pages like Error.jsp in the previous example should contain this tag
-    The presence of this tag creates a new Throwable object called exception in the generated Servlet
-    The exception generated in the original page and passed as an attribute of the request will be assigned to this
-    So, exception is an implicit object that we can use only in error pages
 The page Directive – errorPage and isErrorPage

The include Directive

The include directive can be used to include the contents of some other file in a JSP

-    Example

  • Many dynamic pages contain common static parts in them, mostly header and footer
  • The common static parts can be stored as HTML files that can be included in a JSP
The taglib Directive

  • The taglib Directive will be discussed later in the topic Custom Actions
Action Elements
  •  Action Elements are tags that affect the runtime behaviour of a JSP
  •  Action Elements are also known as Standard Actions
  •  Some common standards actions are
-    <jsp:forward>
-    <jsp:include>
-    <jsp:useBean>
-    <jsp:setProperty>
-    <jsp: getProperty>
-    <jsp :param>
Standards Actions – jsp:forward
  • The <jsp:forward> tag is used to forward a request to another page
  • The control will be given to the target page
  • The syntax of the tag is as follows
    • Standard Actions – jsp:forward
    • Standard Actions – jsp:include
-- The <jsp:include> tag is used to include the contents of another file in a JSP
  
Standard Actions – jsp:useBean, jsp:setProperty, jsp:getProperty
  • Minimizing Java code in a JSP will enable even a web designer to maintain it
  • To separate presentation from code, we can encapsulating the logic in a JavaBean
  •  JSP can instantiate a JavaBean using the <jsp:useBean> tag, set the bean properties using the <jsp:setProperty> tag and get the bean properties using the <jsp:getProperty> tag
    • The wishBean
Standard Actions – jsp:useBean

  • The <jsp:useBean> tag can be used to create a bean object
  • The important attributes of <jsp:useBean> tag are
-    Id
-    Class
-    Scope
  • The id attributes-    Specifies the name of the Bean object
  • The class attribute-    Specifies the fully qualified name of the bean class
Standard Actions - jsp:useBean

The scope attribute
-    Specifies the scope of the bean object as page, request, session  or application
The page scope
-    Available only for this request and only in this page
-    By default, the scope will be page
The request scope
-    Available only for this request
-    Available to others forwarded and included JSPs
The session scope
-    Available to the current session
The application scope
-    Available to any JSP in the same application

Standards Action – jsp:useBean

The <jsp:setProperty>  tag cab be used to set the Bean properties
The attributes are
-    Name
-    Property
-    Param
-    Value
The name attributes
-    Specifies the id of the Bean object

Standard Actions – jsp:setProperty
  •  The property attribute
-    Specifies the name of the bean property that is to be set
-    If the attributes is *, all the request parameters will be assigned to bean properties based on matching name
-    If the request parameter is having a value “”, the bean property is left unaltered
  • The param attribute
-    Specifies the name of the request parameter whose is to be put in to the bean property
-    If this value is not specified, the value of the request parameter whose  name is same as that of the bean property will be assigned to the bean property
  •  The value attribute
-    Specifies the value to be assigned to the bean property
-    A tag cannot have both param and value attributes together
  • The <jsp:setProperty> tag to set the bean property wish of WishBean to the value Welcome is as follows
  • The <jsp:getProperty> tag can be used to get the value of a bean property
  • The attributes are
    • -    Name
    • -    Property
  • The name attribute
  • -    specifies the id of the Bean object
  • The property attribute
  • -    specifies the name of the bean property to get
Standard Actions – jsp:getProperty

The <jsp:getProperty> can be used to get the property wish of the WishBean as follows
Standard Actions – jsp:useBean, jsp:setProperty, jsp:getProperty

Custom Actions
  • In JSP, the programmer can create her own customized tags to encapsulate code from presentation
  • These tags are categorized as Custom Actions
  • The syntax of using custom actions is as follows
  • The Tag interface
  • The implementation class of a custom tag should implement javax.servlet.jsp.jspext. Tag interface
  • Two important methods of the Tag interface are as follows
  • The Tag interface
  • The Tag interface has the  following final static int fields
  • The TagSupport class
  • The TagSupport class implements the Tag interface and provides blank implementation for all the methods
  •  It is easy to extend TagSupport than to implement Tag
  • The doStartTag() and doEndTag() methods of TagSupport class return EVAL_BODY_INCLUDE and EVAL_PAGE respectively


The PageContext class
  • A protected object of the type javax.servlet.jsp.PageContext called pageContext is declared in the  TagSupport class
  • This object can be used to get many attributes of the page like the out, response, request and session objects
  •  Some important methods of the PageContext class is as follows
The helloworld Custom Tag
The taglib Directive
  • Tag Library Descriptor or TLD is an xml file that describes a custom tag
    •  The helloworld Custom Tag
    •  The emplist Custom Tag
The hellouser Custom Tag
  • Custom Tags can accept attributes and behave according to their value
  • The TLD file should contain the information about these attributes
Expressions Language
  • JSP supports Expression Language to create a “scriptless” JSP
  • Expression Language or EL provides simpler syntax and implicit objects to perform some of the actions that could be performed by scriptlets
-    example
JSP Standard Tag Library – JSTL
  • Many developers creating tag libraries were duplicating many actions as these libraries were created separately
  • There was a need for standardizing the tag libraries, and JSP Standard Tag Library ( JSTL ) was created based on this
  •  JSTL provides a rich set of Tags that helps the web designers to perform various actions like
  • -    iterate over each item in a collection
  • -    format
  • -    process xml
  • -    access database
  • Application programmers rarely create custom tags and instead use the powerful JSTL and other open source tag libraries
Summary
JSP is a technology for coding the presentation logic of an enterprise application
Web designers without programming skills can create and maintain JSP
The Standard Actions and Custom Actions help to encapsulate the code from presentation
Rich, open source, Standard Tag Libraries are available

No comments:

Post a Comment