JSP


       AboutMe        MyCurrentLocation      Blog      GuestBook         Think Big- Possibilities are Endless     HOME  

Jsp are bits of Java Code embedded into HTML.They are suitable for coding the Presentation layer of an Enterprise Application.They should be ideally created and maintained by Web Designers.

By Seperation the business logic from the Design , JSP technology makes it faster and easier to build web bases applications

NOTE : Finally jsp pages are compiled into servlets before they can be used, so they have all the benefits of servlets including access to Java API

Ideally what a Web Container Does to a JSP page is as illustrated in the following steps:
1. Web Container translates the jsp page to a servlet and compiles the class. Translation into a servlet class will only be done if the web  container finds that the servlet class is older than the JSP page.

2. Tomcat then loads the generated servlet class and initializes an instance of the servlet class by calling the jspInit method.

3. Tomcat then invokes the _jspService method, passing a Request and Response object.

4. It invokes the jspDestroy method if the container needs to remove the instance of the servlet class.

Implicit objects
The JSP container creates these objects to provide an environment for scriplets and expressions to operate. These are the objects created by the container while parsing a jsp page into a servlet file.All these objects are created in the service method, so you can access them in the servlet page as they are declared in the service method of servlet file.

The Implicit objects are request, response, pageContext,session, application, out, config, page and exception.

SCOPE :
The scope attribute defines "where" there is a reference to the object and "where" the reference is removed.
JSP Specification define four types of scope page, request, session and application

INCLUDING OTHER RESOURCES:
Inclusion of the content of other page is done by Static Inclusion and Dynamic Inclusion 


<!--==========JSP FACTS ALWAYS TO BE REMEMBERED ===========-->


Directives are special instructions to the container
Scriptlets are plain old java code that land up inside the _jspService method of generated servlet by container
Result of a expression always becomes an argument to the println() method

An expression never ends with a semicolon
Its Syntax is like <%= %>


A Scriptlet always ends with a semicolon
Its Syntax is like <%  ;%>
VAriables declared in a Scriptlet are always Local Variables

Jsp declarations are for declaring members of the generated servlet class.
This means both variables methods .
Anything between <%! %> tag is added to the class outside the service method.
This means that you can declare both static and instance methods.


When a container translates a JSP into a Servlet the beginning of the service method is a pile of implicit object declarations and assignments. All the refernces by a JSP map to the API as follows:

Implicit Object        API
out                    JspWriter
request                HttpServletRequest
response            HttpServletResponse
session                HttpSession
application            ServletContext
config                ServletConfig
exception            JspException
pageContext            PageContext
page                Object

<!--===============How to get Servlet Init Parameter's in JSP==========-->

Solution : Just follow these two steps

Step 1:
In the Deployment Descriptor akka web.xml add the following tag within the servlet tag

            <servlet>
                <jsp-file>/NAME_OF_JSP_FILE.jsp</jsp-file>   
            </servlet>

Step2 :

Override the jspInit()method in a declaration within the JSP page

Example:
<%!
ServletConfig Aconfig = getServletConfig();
ServletContext cxt = getServletContext();
String eMail = Aconfig.getInitParameter("eMail");
 <!--Get a refernce to ServletContext and set an application scope attribute -->
cxt.setAttribute("mail",eMail);

%>

Note about jspInit()
jspInit() has access to ServletContext
jspInit() has access to ServletConfig
It is called only once
It can be overidden


<-==============How to Get and Set Attributes in a JSP ==============-->

Most of the time you will be using one out of the four implicit objects o get and set attribute acopes available in a JSP.
These four are request , session , application and page.

<!--=============How to Disable Scriptlets in your web application ======= -->

The <jsp-property-group> element allows you to disable scripting in selective JSP's by speifying URL patterns to be disabled.

You can disable scripting  only through the deployment descriptor.
This is fairly simple.
Go to your Deloyment Descriptor
Make the following entry in jsp-config tag

                    <jsp-config>
                        <scipting-invalid>true</scripting-valid>
                    </jsp-config>

<!--==============How to ignore EL in your web application ============= -->


El is enabled by default.To ignore is fairly simple
Go to your Deloyment Descriptor
Make the following entry in jsp-config tag

                    <jsp-config>
                        <el-ignored>true</el-ignored>
                    </jsp-config>

The other option is to use isELIgnored page directive
               
                    <%@page isELIgnored="true %>