Monday, December 13, 2010

Java Servlet Pages - I

               JSP.. Java Server Pages is like other CGI scripting files which has HTML and scripting language specific code. Container transform JSP code into java source code and compiled into java servlet class. JSP is like that of other servlet class except that, it was written by the container.

JSP -> Servlet:
               JSP file was converted into a java servlet with name matching the jsp file. A service method was created with HTTP request and response as arguments. This service method will throw exceptions related to its operations. JSP file has as many as tags. Each tags are processed and corresponding servlet code was generated. Some of the processing are:
Directives are made as import
HTML tags are printed using out.write
Declarative tags are initialized
Expression tags are printed using print.
More information about these tags comes now....

Scriptlet:
               Scriptlet is the basic tag that takes any java code inside it.
Scriplet tag ~ <% ..... %>
A small variation in Open Scriptlet tag conveys different meaning to the container.

Directive
               Directive instructs container a special meaning other than poj. Directives are usually added at the beginning of the jsp file.
Directive tag ~ <%@ . 
Three ways Directive can be used:
  1. Page
  2. Include
  3. Taglib
               Each type is identified by the their name like <%@ page... %>, <%@ include... %> and <%@ taglib... %>.
These are like HTML tag attributes. Important thing is that there should not be any semi-colon in these tags.
    Page:
                   This directive specifies the package to be imported for this jsp file. Syntax will be of following patterns
    • <%@ page import="Lib.foo" %> ~ to import single class in a package.
    • <%@ page import="Lib.*" %> ~ to import all classes in a package.
    • <%@ page import="Lib.*, java.util.*" %> ~ to import classes from more than onepackage.
    Include:
    <releasing soon>
    Taglib:
    <releasing soon>

    Expression:
                   Expression is another tag type supported by JSP. Its like a printer, whatever you put inside gets printed. There is no need to put print statement to display it in pages.
    Expression tag ~ <%= ...... %>
    Statement inside the expression tag should not have a semi-colon. But why? Here it is, whatever inside the expression tag is treated as argument for print function. Putting semi-colon at the end means putting the semi-colon at the end of argument in the function call within "()".

    Declaration:
                   A variable can be declared in scriptlet and can be used in other tags. But each time when the service method 
    Declaration is a tag type inside which method and variable can be declared. These are static by nature i.e. whatever declared inside this declaration tag, gets printed outside the service class in the generated servlet.
    Declaration tag ~ <%! %>


    3 Methods:
                   The process and stages involved in converting a JSP file to servlet file need not to be known, except the interface that will be implemented by the servlet class and teh 3 methods.
    jspInit():
    This method is called from init() method. This can be overridden.
    jspDestroy():
    This method is called from destroy() method. This can be overridden.
    _jspService()
    This method is called from servlet's service method.  This makes a new thread for each  request and response. This methods cannot be overloaded or overridden


    Translation & Compilation:
                   This translation and compilation of JSP pages to servlet will take place only once for a page. It happens during the first client request. This process can even made before first client request. But it depends on the vendor who supports the feature. A query string "?jsp_precompile" when append on a jsp, the container translate and compile the file even without waiting for the first client request.

    Accessing Context parameters:
    Configuration in web.xml:
                   Initialising parameters for the page can be specified the same way as like that of a servlet. In addition to servlet mapping in web.xml, <jsp-file>xyz.jsp</jsp-file> tag should be added inside corresponding <servlet> tag.
    Overriding init method:
                   Method that initialises init parameters should be created in the name "jspInit". "jspInit" function should be created inside the declaration tag. This function will be called from init method of the translated servlet. Servlet context parameters are fetched and set to as attributes.
    Accessing Attributes:
                   In JSP, there is nothing called as context scoped. Even though attributes in application scope are bound to servlet context object. The attributes set in jspInit method can be accessed thur application.getAttribute("attribute name");

    Attribute Scope:
                   Servlets have only 3 attribute scope (context) .i.e Application. Request and Session.  JSP have one additional attribute scope i.e Page scope. With this page context reference, any other scoped attributes can be accessed. Access in the sense to get and set. 
                   The getAttribute and setAttribute methods of pageContext comes in two forms (overloaded). The overloaded methods takes one additional int argument. This int argument is to specify the context type.
    pageContext.getAttribute("foo", PageContext.SESSION_SCOPE)
    pageContext.getAttribute("foo", PageContext.APPLICATION_SCOPE)
    pageContext.setAttribute("foo", "asdf" ,PageContext.SESSION_SCOPE)
    pageContext.setAttribute("foo", "asdfsadfasfa",  PageContext.APPLICATION_SCOPE)

    1 comment: