Q. What is JSP?
A.
JSP (Java ServerPages) is an extension of the Java Servlet technology.It is commonly used as the presentation layer for combining HTML and Java code while Java Servlet technology is capable of generating HTML . This process of embedding HTML code with escape characters is awkward and hard to maintain.The JSP technology solves this by providing a level of abstraction so that the developer can use custom tags and action elements, which can speed up Web development and are easier to maintain.This is a technology that is used to build applications for generating dynamic web content, such as HTML, DHTML, XHTML, and XML. JSP technology enables the web pages to create dynamic content with maximum power and flexibility.

Q. On what general concept, JSP technology builds?
A.
The technology builds on the following general concepts-
1. Template Data
A substantial portion of most dynamic content is fixed or template content.Text or XML fragments are typical template data. JSP technology supports natural manipulation of template data.
2. Addition of Dynamic Data
JSP technology provides a simple,powerful way to add dynamic data to template data.
3. Encapsulation of functionality
JSP technology provides two related mechanisms for the encapsulation of functionality: JavaBeans component architecture, and tag libraries delivering custom actions, functions, listener classes, and validation.
4. Good Tool Support
Good tool support leads to significantly improved productivity. Accordingly,JSP technology has features that enable the creation of good authoring tools.Careful development of these concepts yields a flexible and powerful serverside technology.

Q. What are the JSP APIs consists of?
A.
The JSP API consists of two packages:
javax.servlet.jsp
javax.servlet.jsp.tagext

Q. Write a short note on Web Containers and Web Components?
A.
JSP container is a system-level entity that provides life-cycle management and runtime support for JSP pages and servlet components. Requests sent to a JSP page are delivered by the JSP container to the appropriate JSP page implementation object. The term web container is synonymous with JSP container.A web component is either a servlet or a JSP page. The servlet element in a web.xml deployment descriptor is used to describe both types of web components. JSP page components are defined implicitly in the deployment descriptor through the use of an implicit .jsp extension mapping, or explicitly through the use of a jsp-group element.

Q. What are Empty Elements in JSP?
A.
Following the XML specification, an element described using an empty tag is indistinguishable from one using a start tag, an empty body, and an end tag.As examples, the following are all empty tags-

[code lang=”xml”]<x:foo></x:foo>
<x:foo />
<x:foo/>
<x:foo><%– any comment –%></x:foo> [/code]

While the following are all non-empty tags-

[code lang=”xml”]<foo> </foo>
<foo><%= expression %></foo>
<foo><% scriptlet %></foo>
<foo><bar/></foo>
<foo><!– a comment –></foo> [/code]

Q. Explain JSP Life Cycle?
A.
JSP’s life cycle have the following phases-
1.Translation
In this phase, a java servlet file is generated from the JSP source file and the container validates the syntactic correctness of the JSP pages and tag files. The container interprets the standard directives and actions, and the custom actions referencing tag libraries used in the page.
2.Compilation
In this phase,the generated java servlet file is compiled into a java servlet class.
3. Class Loading
In this phase,the java servlet class that was compiled from the JSP source is loaded into the container.
4. Execution
In this phase the container manages one or more instances of this class in response to requests and other events. The interface JspPage contains jspInit() and jspDestroy(). The JSP specification has provided a special interface HttpJspPage for JSP pages serving HTTP requests and this interface contains _jspService().
5. Initialization
jspInit() method is called immediately after the instance created. It is called only once during JSP life cycle.To perform JSP-specific initialization, override the jspInit() method.

[code lang=”java”]public void jspInit(){

// Initialization code…

}[/code]

6. _jspService() execution
This method is called for every request of this JSP during its life cycle and is responsible for generating the response for that request and this method is also responsible for generating responses to all seven of the HTTP methods ie. GET, POST, DELETE etc. It passes the request and the response objects. _jspService() cannot be overridden. The _jspService() method takes an HttpServletRequest and an HttpServletResponse as its parameters as follows:

[code lang=”java”]void _jspService(HttpServletRequest request,
HttpServletResponse response)
{
// Service handling code…
} [/code]

7. jspDestroy() execution

This method is called when this JSP is destroyed. With this call the servlet serves its purpose and submits itself to garbage collection.This is the end of jsp life cycle.Override jspDestroy when you need to perform any cleanup, such as releasing database connections or closing open files.

[code lang=”java”]public void jspDestroy()
{
// Your cleanup code goes here.
}[/code]

jspInit(), _jspService() and jspDestroy() are called the life cycle methods of the JSP.Their resposibilities are as follows-
jspInit() is responsible to initialize config params. _jspService(HttpServletRequest request, HttpServletResponse response) is responsible for handling client requests.jspDestroy()is responsible to unload the JSP from memory.

Q. What are the advantages of JSP over competing technologies?
A.
The advantages of JSP Over competing technologies are-
Versus ASP or ColdFusion
-Better language for dynamic part.
-Portable to multiple servers and operating systems.
Versus PHP
-Better language for dynamic part.
-Better tool support.
Versus pure servlets
-More convenient to create HTML
-Can use standard tools (e.g., DreamWeaver)
-Divide and conquer
Versus client-side JavaScript (in browser)
Capabilities mostly do not overlap with JSP, but You control server, not client richer language
Versus server-side JavaScript(e.g., LiveWire, BroadVision)
-Richer language
Versus static HTML
-Dynamic features
-Adding dynamic features no longer “all are nothing” decisions.

Q. What are the key features of JavaServer Pages?
A.
The key features of JavaServer Pages are:
• Standard directives
• Standard actions
• Scripting elements
• Tag Extension mechanism
• Template content

Q. How will you avoid scriptlet code in JSP?
A.
Use JavaBeans or Custom Tags instead.

Q.Is JSP variable declaration thread safe?
A.
No.The declaration of variables in JSP is not thread-safe.