Tuesday, May 25, 2010

A JSP is just a servlet in the end

The Container takes what you’ve written in your JSP, translates it into a servlet class source (.java) file, and then compiles that into a Java servlet class. After that, servlet runs in exactly the same way it would if you’d written and compiled the code yourself.

1:Scriptlet
Scriptlet code are just plain old Java that lands as-is within the generated servlet’s service method. The scriptlet code is between angle brackets with percent signs: < % and % >.

<  % out.println (Counter.getCount()); %  >
Where Counter is a class name and getCount () is a method present in Counter Class.

2:Expression
Expression code becomes the argument to a print () method. The expression adds an additional character to the start of the element—an equals sign (=).

<  %= Counter.getCount () %  >

When the Container sees above code then it turns it into this below code:

out.print (Counter.getCount ());
Note:-There is no need of semicolon at the end of Expression code

3: Declaration
JSP declarations are for declaring members of the generated servlet class. That means both variables and methods. In other words, anything between the tag is added to the class outside the service method. That means you can declare both static variables and methods.

< %! int count=0; % >
Note: using this we can create a counter that can increment each time we call the same page.

4: Use the page directive to import packages
A directive is a way for you to give special instructions to the Container at page translation time.
To import a single package: 
< %@ page import=”SamplePackage.*” % >

To import multiple packages:
< %@ page import=” SamplePackage.*, java.util.*” % >

Use a comma to separate the packages. The quotes go around the entire list of packages!

Directives come in three flavors: page, include, and taglib.
The taglib directive:-
Defines tag libraries available to the JSP.

< %@ taglib tagdir=”/WEB-INF/tags/hot” prefix=”hot” % >

The include directive:-
Defines text and code that gets added into the current page at translation time. This lets you build reusable chunks that can be added to each page without having to duplicate all that code in each JSP.

<  %@ include file=”SampleHeader.html” %  >

The page directive:-
Defines page-specific properties such as character encoding, the content type for this page’s response, and whether this page should have the implicit session object.

< %@ page import=”Sample.*” session=”false” % >

Main Attributes to the page directive

Import: -Defines the Java import statements that’ll be added to the generated servlet class.
isThreadSafe: -Defines whether the generated servlet needs to implement the SingleThreadModel. The default value is...”true”, which means, “My app is thread safe, so I do NOT need to implement SingleThreadModel, which I know is inherently evil.”
contentType : -Defines the MIME type (and optional character encoding) for the JSP response.
isELIgnored : -Defines whether EL expressions are ignored when this page is translated.
isErrorPage : -Defines whether the current page represents another JSP’s error page. The default value is “false”.
errorPage:-Defines a URL to the resource to which uncaught Throwables should be sent.

5:Expression Language
In order to avoid direct usage of java in Jsp, we can use Expression Language. The purpose of EL is to offer a simpler way to invoke Java code—but the code itself belongs somewhere else.

Example:-Please contact: ${applicationScope.mail}

6: Actions
They come in two flavors: standard and other action
Standard Action:
< jsp:include page=”Footer.jsp” / >
custom or Other Action:
< c:set var=”rate” value=”43” / >

Using < scripting-invalid >

You can make it invalid for a JSP to have scripting elements (Scriptlets, Java expressions, or declarations) by putting a tag in the DD:

< web-app ... > 
< jsp-config >< jsp-property-group >
< url-pattern > *.jsp< /url-pattern >
< scripting-invalid > true< /scripting-invalid >
< /jsp-property-group > < /jsp-config >
< /web-app >

A comment

You can put two different types of comments in a JSP:

<  !-- HTML comment --  > The Container just passes this straight on to the client, where the browser interprets it as a comment.

<  %-- JSP comment --%  >These are for the page developers, and just like Java comments in a Java source file, they’re stripped out of the translated page.

API for the generated servlet

The Container generates a class from your JSP that implements the HttpJspPage interface. This is the only part of the generated servlet’s API that you need to know.
All you need to know about are the three key methods:

jspInit()
  1. This method is called from the init() method.
  2. You can override this method. 
jspDestroy()
  1. This method is called from the servlet’s destroy() method.
  2. You can override this method as well.

_jspService()
  1. This method is called from the servlet’s service() method, which means it runs in a separate thread for each request. The Container passes the Request and Response objects to this method.
  2. You can’t override this method

No comments:

Post a Comment