Friday, October 21, 2005

What is Expression Language (EL)? What’s its purpose?

It is a bad practice to put all the java code in your JSP Page. There was no solution to this problem before JSP Spec 2.0, to have Java code somewhere else and use some simple commands to invoke them.

EL has solved two of the major problems:

  1. Web page designers don’t have to know java
  2. Java code is JSP is hard to change and maintain.
With this new feature in JSP 2.0 you write java code somewhere else and call it using EL.

The purpose of EL is to offer a simpler way to invoke java code - that is residing some where in the Model component.

What’s the difference between HTML Comment and JSP Comment.

The difference between HTML/XML(Markup) comments and JSP comments is that the JSP translator doesn't recognize the former as comments and treats them as template text while the later is stripped out of translated JSP Page.

HTML comments are sent to the client but are not displayed by the browser. A user can still view these comments by viewing the source HTML for the page. JSP comments are stripped by JSP translator at page translation time.

Comments

You have various methods to add comments to a Java Server Page. The first and most obvious is to just use the


HTML comment tag:

<  !-- some comment -->

JSP comment tag:

<%-- some comment --%>

Java Comment tag:

<%
//This is a Java comment
/* This is also a valid Java comment that
   can be split over more than one line */
%>

Explain the various implicit objects that are available?

Object

Class or Interface

Description

page

jsp.HttpJspPage

Page's servlet instance

config

ServletConfig

Servlet configuration information

pageContext

jsp.pageContext

Provides access to all the namespaces associated with a JSP page and access to several page attributes

request

http.HttpServletRequest

Data included with the HTTP Request

response

http.HttpServletResponse

HTTP Response data, e.g. cookies

out

jsp.JspWriter

Output stream for page context

session

http.HttpSession

User specific session data

application

ServletContext

Data shared by all application pages

The application, session and request implicit objects have the additional ability to hold arbitrary values. By setting and getting attribute values these objects are able to share information between several JSP pages.

What are implicit Objects?

These objects will be automatically instantiated under specific variable names hence the name implicit. Implicit objects are a set of Java objects that the JSP Container makes available to developers in each page.

All the implicit objects maps to something from the Servlet/JSP API. The request implicit object, for example, is a reference to the HttpServletRequest object passed to the service method by the container.

You can use implicit Objects within Scriptlets and Expressions and not within Directives or Declarations.

A good article about Implicit Objects can be found in IBM DeveloperWorks

Wednesday, October 19, 2005

Write down the Valid and Invalid JSP Expressions

Valid:


  1. All primitive literals are valid.
  2. String literals are allowed.
  3. Anything that resolves to Boolean is fine.
  4. Object declaration is allowed.
  5. Method calls should always result in a value( primitive, String, Boolean).

Invalid:


  1. Semicolon should be avoided at the end of the statement.
  2. No variable declarations are allowed.
  3. Method calls should not result in a void value.

What are the different attributes available in page Directive?

  1. import
  2. session
  3. contentType
  4. isELIgnored
  5. isThreadSafe
  6. isErrorPage
  7. errorPage
  8. language
  9. extends
  10. buffer
  11. autoFlush
  12. info
  13. pageEncoding

What are the different types of Directives?


  1. page
  2. include
  3. taglib

JSP Elements

Scriptlet: Everything between <% and %> is a Scriptlet, which is just plain old java. Scriptlet’s can have variable declarations. But they are local variables.

Syntax: Scriptlet doesn’t add any additional character.

<% out.println(“Hi”); %>

Directive: A directive is a way for you to give special instructions to the container at the page translation time.

Syntax: Directive adds an additional character to the start of the element – the @ sign.

<%@ page import = ”java.util.*” %>

Expression: Expressions becomes the argument to out.print()

Syntax: Expression adds an additional character to the start of the element – an equal sign (=). Semicolon should be avoided at the end of the statement. No variable declarations are allowed.

<%= 50 %> Valid

<%= 50; %> Invalid (Semicolon not allowed)

<%= int i=5 %> Invalid (variable declaration not allowed)

Example:

<%= “Plus” %>

out.print(“Plus”);

NOTE:

All Scriptlet and expression code lands in a service method.

That means variables declared in a Scriptlet are always local variables.

Declaration: JSP declarations are for declaring instance members of the generated servlet class. That means both variables and methods.

Syntax: Declaration adds an additional character to the start of the element – an exclamation sign (!).

<%! int count = 0; %>


JSP Page LifeCycle

When the container looks at your JSP Page, it starts with

  1. Translating the JSP Page into Java source code
  2. Compiling it into full-fledged java servlet class file
  3. Loading the class
  4. creating instance
  5. calling the jspInit method
  6. calling the _jspService method and finally
  7. calling the jspDestroy method

Monday, October 17, 2005

The Secret Life of JavaServer Pages

A very good article explaining about Jasper(JSP Engine), JSP internals and its Life-Cycle by Bear Bibeault

The Secret Life of JSP Pages

Expression Language made easy. Article by Bear Bibeault

Scriptless JSP Pages: The Power of the Map