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
-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
-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
- 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
- Suitable for coding the business layer of an enterprise application
- Created and maintained by Java programmers
- JSP
- 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
- 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
• 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 - session
The page Directive – contentType
- The contentType attribute
The page Directive – errorPage
- The errorPage attribute
- The page Directive – isErrorPage
- 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
- 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: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
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
- 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
- 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
- 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
- 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
- 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
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 taglib Directive
- Tag Library Descriptor or TLD is an xml file that describes a custom tag
- The helloworld Custom Tag
- The emplist 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
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 applicationWeb 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