Pages

Wednesday, June 20, 2012

JSP tutorial (Java Server Pages)


JavaServer Pages (JSP) technology provides a simplified, fast way to create dynamic web content. JSP technology enables rapid development of web-based applications that are server- and platform-independent.

Steps in processong of Jsp
i)Jsp is translated to servlet(.java file)
ii)Compiled to .class file
iii)Loaded and initailized as servlet object


Jsp Elements
------------
i)Scriptlet(<%  %>)
    Eg:<% out.println("Hello Jsp");%>


ii)Directive(<%@ %>)
    Eg:<%@ page import="java.util.*" %>
Page directive is about giving the container information it need when translating Jsp into a Servlet.
iii)Expression(<%= %>)
    Eg:<%= "Hello JSP" %>

Note : Never end an expression with a semicolon.
         All scriptlet and expression code lands in a service method.
iv)Declaration(<%!  %>)
    Eg:<%! int count=0; %>
We can declare both methods and variables in JSP Declaration.

v)Actions
    Standard actions
        Eg:<jsp:include page="home.jsp" />
    Other action   
        Eg:<c:set var="name" value="standardized non-standard action" />


JSP Implicit Objects 
-----------------------
API                                Implicit Object
**********                    ************   
JspWriter                        out
HttpServletRequest          request
HttpServletResponse        response
HttpSession                    session
ServletContext                application
ServletConfig                  config
Throwable                      exception
PageContext                   pageContext   
Object                           page


Comments
-------------
JSP Comment   : <%-- This is a Jsp comment --%>
HTML comment : <!-- This is a HTML comment,this is sent along with response but not displayed  on client's browser -->

The Container generates a class from a JSP that implements the HttpJspPage interface.

<<interface>> javax.servelt.jsp.JspPage : jspInit() , jspDestroy()   <!-- We can override these methods -->
<<interface>> javax.servlet.jsp.HtpJspPage : _jspService(HttpServletRequest,HttpServletResponse) <!-- We Can't override this method -->


Configure init parameters

------------------------------ 
<web-app ...>
 <servlet>
  <servlet-name>InitParamEx</servlet-name>
  <jsp-file>/checkInitParam.jsp</jsp-file>
 </servlet>
 <servlet-mapping> 
  ....
  ....
 </servlet-mapping>
</web-app>
Attributes in JSP 
-------------------
Most of the time we'll be using one of the four implicit objects to get ans set attributes corresponding to the four attribute scopes available in a JSP. 


To find an attribute when we don't know the scope (using pageContext): <%= pageContext.findAttribute("movie") %> 

EL - Expression Language
------------------------------
The purpose of EL is to offer a simpler way to invokea Java code .But the code itself belongs to somewhere else.

EL is enabled by default.

EL Eg: ${9*9} //o/p:81

To ignore EL
i) set <el-ignored>true</el-ignored> in Deployment descriptor.
ii) set <%@ page isELIgnored="true" %>  This is in JSP

Always page directive precedes the DD if, it's set in both DD and in Jsp.

No comments:

Post a Comment