Friday, September 25, 2015

Remembering JSP Actions

JSP  mainly has 4 Types of actions

useBean
   Jsp usebean is used to associate a variable with an instance of a class . An example  is :

 <jsp:useBean id="today" class="java.util.Date"/>


setProperty and getProperty

    These actions are used to set or get property values from the variables defined using useBean  an Example :

 <jsp:useBean id="employee" class="testpackage.Employee"/>
<jsp:setProperty name="employee" property="firstName" value="someName"/>
First Name: <jsp:getProperty name="employee" property="firstName"/>


JSP Include and Forward 

    Jsp Include is used to include another resource dynamically . This should be a JSP page .
Difference between a JSP include and an Include directive is that , the JSP include adds the resource to the page in runtime while the other does in the time of translation .

  JSP forward forwards the current page to a different resource

Remembering JSP's

Lets look at some old JSP  terminologies

   
 
 
  JSP's are servlets
  
     All Jsps get translated to servlets during runtime. The servlet class name is normally decided by the servlet container . All these servlet classes extends implements jspPage interface .  When requested for the first time ,webserver compiles JSP page to the class and on subsequent requests it doesn't re compiles unless changes has been observed in the JSP Page .

JSPInit() and JSPDestroy()
   
          You can override JSPInit and JSPDestroy() methods in the JSP declaration tags. Doing this allows you the handle to the init() and destroy() functions of the jsp servlet.

JSP mappings in web.xml

         JSP's are mapped in web.xml's  to a particular url pattern . Init Parameter is also defined here.

Implicit Objects
     
      There are set of implicit objects in JSP pages which you can use without declaring it explicitly .
They are :

  request - servletRequest
  response - servletResponse
  out - PrintWriter (JspWriter)
  session - HTTPSession
  application -  ServletContext
  config - ServletConfig
  pageContext - pageContext
  page - JspPage
  exception - Throwable


Scopes of Variables in JSP 
   
           In Jsp a variable can be stored in 4 Scopes page, request , session and applicationScope
page - only for the page, request - ServletRequest , session - HttpSession  , application - ServletContext . The setAttribute method in PageContext has the following signature:

public abstract void setAttribute(java.lang.String name,
        java.lang.Object value, int scope)
         The value of scope can be one of the following static final ints in PageContext: PAGE_SCOPE, REQUEST_SCOPE, SESSION_SCOPE, and APPLICATION_SCOPE.

JSP Include Directive
   
  <%@include file ="url" />   is known as include directive in JSP pages. These are used to keep the code separated and have a modular architecture . When we do a jsp page include, it replaces the directive with the contents of the url page.


Scriptlets , Expressions and Directives

  The scriplets are "<%"  which shows beginning of the java code in JSP page .
   "<%=" are Expressions which calls a method and returns the value to output stream of JSP
  "<%! "  are directives which mark starting of a method definitions in the JSP page