Total Pageviews

Saturday, August 8, 2015

JSP Interview Questions


JavaServer Pages (JSP) is a technology for developing web pages that support dynamic content which helps developers insert java code in HTML pages by making use of special JSP tags, most of which start with <% and end with %>.

JSP offer several advantages as listed below:
  • Performance is significantly better because JSP allows embedding Dynamic Elements in HTML Pages itself.
  • JSP are always compiled before it's processed by the server unlike CGI/Perl which requires the server to load an interpreter and the target script each time the page is requested.
  • JavaServer Pages are built on top of the Java Servlets API, so like Servlets, JSP also has access to all the powerful Enterprise Java APIs, including JDBC, JNDI, EJB, JAXP etc.
  • JSP pages can be used in combination with servlets that handle the business logic, the model supported by Java servlet template engines.

The advantages of JSP are twofold.
First, the dynamic part is written in Java, not Visual Basic or other MS specific language, so it is more powerful and easier to use.
Second, it is portable to other operating systems and non-Microsoft Web servers.
It is more convenient to write (and to modify!) regular HTML than to have plenty of println statements that generate the HTML. Other advantages are:
  • Embedding of Java code in HTML pages.
  • Platform independence.
  • Creation of database-driven Web applications.
  • Server-side programming capabilities.

SSI is really only intended for simple inclusions, not for "real" programs that use form data, make database connections, and the like.
JavaScript can generate HTML dynamically on the client but can hardly interact with the web server to perform complex tasks like database access and image processing etc.
Regular HTML, of course, cannot contain dynamic information.
A JSP Lifecycle consists of following steps:
  • Compilation: When a browser asks for a JSP, the JSP engine first checks to see whether it needs to compile the page. If the page has never been compiled, or if the JSP has been modified since it was last compiled, the JSP engine compiles the page.
    The compilation process involves three steps:
    • Parsing the JSP.
    • Turning the JSP into a servlet.
    • Compiling the servlet.
  • Initialization: When a container loads a JSP it invokes the jspInit() method before servicing any requests
  • Execution: Whenever a browser requests a JSP and the page has been loaded and initialized, the JSP engine invokes the _jspService() method in the JSP.The _jspService() method of a JSP is invoked once per a request 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.
  • Cleanup: The destruction phase of the JSP life cycle represents when a JSP is being removed from use by a container.The jspDestroy() method is the JSP equivalent of the destroy method for servlets.

A scriptlet can contain any number of JAVA language statements, variable or method declarations, or expressions that are valid in the page scripting language.
Following is the syntax of Scriptlet:<% code fragment %>

A declaration declares one or more variables or methods that you can use in Java code later in the JSP file. You must declare the variable or method before you use it in the JSP file.
<%! declaration; [ declaration; ]+ ... %>
A JSP expression element contains a scripting language expression that is evaluated, converted to a String, and inserted where the expression appears in the JSP file.
The expression element can contain any expression that is valid according to the Java Language Specification but you cannot use a semicolon to end an expression.
Its syntax is:<%= expression %>
JSP comment marks text or statements that the JSP container should ignore. A JSP comment is useful when you want to hide or "comment out" part of your JSP page.
Following is the syntax of JSP comments:<%-- This is JSP comment --%>
A JSP directive affects the overall structure of the servlet class. It usually has the following form:
<%@ directive attribute="value" %>
The types directive tags are as follows:
  • <%@ page ... %> : Defines page-dependent attributes, such as scripting language, error page, and buffering requirements.
  • <%@ include ... %> : Includes a file during the translation phase.
  • <%@ taglib ... %> : Declares a tag library, containing custom actions, used in the page.

JSP actions use constructs in XML syntax to control the behavior of the servlet engine. You can dynamically insert a file, reuse JavaBeans components, forward the user to another page, or generate HTML for the Java plugin.
Its syntax is as follows:<jsp:action_name attribute="value" />
jsp:include, jsp:useBean,jsp:setProperty,jsp:getProperty, jsp:forward,jsp:plugin,jsp:element, jsp:attribute, jsp:body, jsp:text
Literals are the values, such as a number or a text string, that are written literally as part of a program code. The JSP expression language defines the following literals:
  • Boolean: true and false
  • Integer: as in Java
  • Floating point: as in Java
  • String: with single and double quotes; " is escaped as \", ' is escaped as \', and \ is escaped as \\.
  • Null: null

The page directive is used to provide instructions to the container that pertain to the current JSP page. You may code page directives anywhere in your JSP page.
Page directive contains the following 13 attributes.
  1. language
  2. extends
  3. import
  4. session
  5. isThreadSafe
  6. info
  7. errorPage
  8. isErrorpage
  9. contentType
  10. isELIgnored
  11. buffer
  12. autoFlush
  13. isScriptingEnabled

The buffer attribute specifies buffering characteristics for the server output response object.
When buffer is set to “none”, servlet output is immediately directed to the response output object.
The autoFlush attribute specifies whether buffered output should be flushed automatically when the buffer is filled, or whether an exception should be raised to indicate buffer overflow.
A value of true (default) indicates automatic buffer flushing and a value of false throws an exception.
The contentType attribute sets the character encoding for the JSP page and for the generated response page. The default content type is text/html, which is the standard content type for HTML pages.
The errorPage attribute tells the JSP engine which page to display if there is an error while the current page runs. The value of the errorPage attribute is a relative URL.
The isErrorPage attribute indicates that the current JSP can be used as the error page for another JSP.
The value of isErrorPage is either true or false. The default value of the isErrorPage attribute is false.
The extends attribute specifies a superclass that the generated servlet must extend.
The import attribute serves the same function as, and behaves like, the Java import statement. The value for the import option is the name of the package you want to import.
The info attribute lets you provide a description of the JSP.
The isThreadSafe option marks a page as being thread-safe. By default, all JSPs are considered thread-safe. If you set the isThreadSafe option to false, the JSP engine makes sure that only one thread at a time is executing your JSP.
The language attribute indicates the programming language used in scripting the JSP page.
The session attribute indicates whether or not the JSP page uses HTTP sessions. A value of true means that the JSP page has access to a builtin session object and a value of false means that the JSP page cannot access the builtin session object.
The isELIgnored option gives you the ability to disable the evaluation of Expression Language (EL) expressions.
The default value of the attribute is true, meaning that expressions, ${...}, are evaluated as dictated by the JSP specification. If the attribute is set to false, then expressions are not evaluated but rather treated as static text.
The isScriptingEnabled attribute determines if scripting elements are allowed for use.
The default value (true) enables scriptlets, expressions, and declarations. If the attribute's value is set to false, a translation-time error will be raised if the JSP uses any scriptlets, expressions (non-EL), or declarations.
The include directive is used to includes a file during the translation phase. This directive tells the container to merge the content of other external files with the current JSP during the translation phase. You may code include directives anywhere in your JSP page.
The general usage form of this directive is as follows:<%@ include file="relative url" >
The taglib directive follows the following syntax:<%@ taglib uri="uri" prefix="prefixOfTag">
uri attribute value resolves to a location the container understands
prefix attribute informs a container what bits of markup are custom actions.
The taglib directive follows the following syntax:<%@ taglib uri="uri" prefix="prefixOfTag" >
  • Id attribute: The id attribute uniquely identifies the Action element, and allows the action to be referenced inside the JSP page. If the Action creates an instance of an object the id value can be used to reference it through the implicit object PageContext
  • Scope attribute: This attribute identifies the lifecycle of the Action element. The id attribute and the scope attribute are directly related, as the scope attribute determines the lifespan of the object associated with the id. The scope attribute has four possible values: (a) page, (b)request, (c)session, and (d) application.

This action lets you insert files into the page being generated. The syntax looks like this:<jsp:include page="relative URL" flush="true" 

No comments:

Post a Comment