JSP Interview questions

What is JSP?

Java Server Pages technology (JSP) is used to create dynamic web page. It is an extension to the servlet technology. A JSP page is internally converted into servlet.

What are advantages of using JSP?

JSP has several advantages as listed below

  • JSP 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.
  • JSP 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.

What are the life-cycle methods for a jsp?

JSP life cycle methods are listed below :

  • public void jspInit() : It is invoked only once, same as init method of servlet.
  • public void _jspService(ServletRequest request,ServletResponse) throws ServletException,IOException : It is invoked at each request, same as service() method of servlet.
  • public void jspDestroy() : It is invoked only once, same as destroy() method of servlet.

What are the advantages of JSP over Active Server Pages (ASP)?

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.

What are the advantages of JSP over Pure Servlets?

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.

What are the advantages of JSP over JavaScript?

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.

What is difference between hide comment and output comment?

The jsp comment is called hide comment whereas html comment is called output comment. If user views the source of the page, the jsp comment will not be shown whereas html comment will be shown.

Explain lifecycle of a JSP.

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.

What are the JSP implicit objects?

JSP provides 9 implicit objects by default. They are as follows:

  • out : the out is type of JspWriter
  • request : the request is type of HttpServletRequest
  • response : the response is type of HttpServletResponse
  • config : the config is type of ServletConfig
  • session : the session is type of HttpSession
  • application : the application is type of ServletContext
  • pageContext : the pageContext is type of PageContext
  • page : the page is type of Object, it refer to this
  • exception : the exception is type of Throwable

What is a sciptlet in JSP and what is its syntax?

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:

<% some java code %>

What are JSP declarations?

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.

<%! some variable declaration; %>

What are JSP expressions?

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 %>

What are JSP comments?

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 --%>

What are JSP Directives?

A JSP directive affects the overall structure of the servlet class. It usually has the following form:

<%@ directive attribute="value" %>

What are the types of directive tags?

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.

What are JSP actions?

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" />

Name some JSP actions.

Some of the JSP Actions : jsp:include, jsp:useBean,jsp:setProperty,jsp:getProperty, jsp:forward,jsp:plugin,jsp:element, jsp:attribute, jsp:body, jsp:text

What are JSP literals?

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

What is a page directive?

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.

What is difference between include directive and include action?

include directive

  • The include directive includes the content at page translation time.
  • The include directive includes the original content of the page so page size increases at runtime.
  • It’s better for static pages.

include action

  • The include action includes the content at request time.
  • The include action doesn’t include the original content rather invokes the include() method of Vendor provided class.
  • It’s better for dynamic pages.

Is JSP technology extensible?

Yes. JSP technology is extensible through the development of custom actions, or tags, which are encapsulated in tag libraries.

How can I implement a thread-safe JSP page? What are the advantages and Disadvantages of using it?

You can make your JSPs thread-safe by having them implement the SingleThreadModel interface. This is done by adding the directive <%@ page isThreadSafe="false" %> within your JSP page.

How can we handle the exceptions in JSP ?

There are two ways to perform exception handling, one is by the errorPage element of page directive, and second is by the error-page element of web.xml file.

What are the two ways to include the result of another page. ?

There are two ways to include the result of another page:

  • By include directive
  • By include action

Can we use the exception implicit object in any jsp page?

No. The exception implicit object can only be used in the error page which defines it with the isErrorPage attribute of page directive.

How is JSP used in the MVC model?

JSP is usually used for presentation in the MVC pattern (Model View Controller ) i.e. it plays the role of the view. The controller deals with calling the model and the business classes which in turn get the data, this data is then presented to the JSP for rendering on to the client.

What are the different scope values for the tag?

There are 4 values:

  • page
  • request
  • session
  • application

What is the difference between ServletContext and PageContext?

ServletContext gives the information about the container whereas PageContext gives the information about the Request.

What is the difference in using request.getRequestDispatcher() and context.getRequestDispatcher()?

request.getRequestDispatcher(path) is used in order to create it we need to give the relative path of

the resource whereas context.getRequestDispatcher(path) in order to create it we need to give the absolute path of the resource.

What is EL in JSP?

The Expression Language(EL) is used in JSP to simplify the accessibility of objects. It provides many objects that can be used directly like param, requestScope, sessionScope, applicationScope, request, session etc.

What is basic differences between the JSP custom tags and java beans?

  • Custom tags can manipulate JSP content whereas beans cannot.
  • Complex operations can be reduced to a significantly simpler form with custom tags than with beans.
  • Custom tags require quite a bit more work to set up than do beans.
  • Custom tags are available only in JSP 1.1 and later, but beans can be used in all JSP 1.x versions.

Can an interface be implemented in the jsp file?

No.

What is JSTL?

JSP Standard Tag Library is library of predefined tags that ease the development of JSP.

How many tags are provided in JSTL?

There are 5 type of JSTL tags.

  • core tags
  • sql tags
  • xml tags
  • internationalization tags
  • functions tags

Which directive is used in jsp custom tag?

The jsp taglib directive.

What are the 3 tags used in JSP bean development?

  • jsp:useBean
  • jsp:setProperty
  • jsp:getProperty

How to disable session in JSP?

<%@ page session="false" %>

What are various attributes Of page directive?

Page directive contains the following 13 attributes.

  • language
  • extends
  • import
  • session
  • isThreadSafe
  • info
  • errorPage
  • isErrorpage
  • contentType
  • isELIgnored
  • buffer
  • autoFlush
  • isScriptingEnabled

What is a include directive?

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" >

What is a taglib directive?

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" >

what is the function of action?

This action lets you insert files into the page being generated. The syntax looks like this:

<jsp:include page="relative URL" flush="true" />

Where page is the the relative URL of the page to be included.

Flush is the boolean attribute the determines whether the included resource has its buffer flushed before it is included.

What is Action?

The plugin action is used to insert Java components into a JSP page. It determines the type of browser and inserts the

<object> or <embed>

tags as needed.

If the needed plugin is not present, it downloads the plugin and then executes the Java component. The Java component can be either an Applet or a JavaBean.

What is difference between GET and POST method in HTTP protocol?

The GET method sends the encoded user information appended to the page request. The page and the encoded information are separated by the ? Character.

The POST method packages the information in exactly the same way as GET methods, but instead of sending it as a text string after a ? in the URL it sends it as a separate message. This message comes to the backend program in the form of the standard input which you can parse and use for your processing.