Javatpoint Logo
Javatpoint Logo

JSP Interview Questions

There is a list of top 40 frequently asked JSP interview questions and answers for freshers and professionals. If you know any JSP interview question that has not been included here, post your question in the Ask Question section.


1) What is JSP?

Java Server Pages technology (JSP) is a server-side programming language used to create a dynamic web page in the form of HyperText Markup Language (HTML). It is an extension to the servlet technology.

A JSP page is internally converted into the servlet. JSP has access to the entire family of the Java API including JDBC API to access enterprise database. Hence, Java language syntax has been used in the java server pages (JSP). The JSP pages are more accessible to maintain than Servlet because we can separate designing and development. It provides some additional features such as Expression Language, Custom Tags, etc.

More details.

2) What are the life-cycle methods for a JSP?

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

3) List out some advantages of using JSP.

  • Better performance.
  • The compilation of JSP is done before it is processed by the server which eradicates the need for loading of interpreter and code script each time.
  • JSP has access to all-powerful enterprises.
    Easy to maintain: JSP can be easily managed because we can easily separate our business logic with presentation logic. In Servlet technology, we mix our business logic with the presentation logic.
  • JSP can also be used in combination with servlets.

4) Give the syntax for JSP comments.

The syntax for JSP comments is:



5) What is the difference between hide comment and output comment?

The JSP comment is called hide comment whereas HTML comment is called output comment. If a user views the source of the page, the JSP comment will not be shown whereas HTML comment will be displayed.


6) What are the JSP implicit objects?

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

ObjectType
1) outJspWriter
2) requestHttpServletRequest
3) responseHttpServletResponse
4) configServletConfig
5) sessionHttpSession
6) applicationServletContext
7) pageContextPageContext
8) pageObject
9) exceptionThrowable
More details.

7) What is the difference between include directive and include action?

include directiveinclude action
1) The include directive includes the content at page translation time.1) The include action includes the content at request time.
2) The include directive includes the original content of the page, so page size increases at runtime2) The include action doesn't include the original content rather invokes the include() method of Vendor provided class.
3) It's better for static pages.3) 3) It's better for dynamic pages.

8) Is JSP technology extensible?

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


9) 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.


10) How can I prevent the output of my JSP or Servlet pages from being cached by the browser?

(OR) How to disable caching on the back button of the browser?


11) 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 the web.xml file.

More details.

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

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


13) How can we forward the request from JSP page to the servlet?

Yes of course! With the help of "forward action" tag, but we need to give the URL-pattern of the servlet.

forward action tag

14) 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.

More details.

15) 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, and this data is then presented to the JSP for rendering on to the client.

forward action tag

16) What are context initialization parameters?

Context initialization parameters are specified by the <context-param> in the web.xml file, and these are initialization parameter for the whole application and not specific to any servlet or JSP.

More details.

17) What are the different scope values for the <jsp:useBean> tag?

There are 4 values:

  1. page
  2. request
  3. session
  4. application
More details.

18) What do JSP literals consist of?

  • Boolean
  • Integer
  • Floating point
  • String
  • Null

19) What is the purpose of <jsp:useBean>?

The jsp:useBean action searches for the existence of the object with specified name. If not found, it creates one.


20) What is the purpose of <jsp:setProperty >?

This action sets the properties of a bean.


21) What is the purpose of <jsp:getProperty >?

This action retrieves the properties of a bean.


22) List out the various scope values of JSP action.

The possible scope values are:

  • page
  • request
  • session
  • application

23) What is the use of 'out' implicit object?

The object is used to give a response to contents.


24) Give the use of session object.

The object is used between the client requests for the tracking of client sessions.


25) Give the use of exception object.

The object is used for the generation of a response to the errors thrown.


26) What is the difference between ServletContext and PageContext?-

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


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

request.getRequestDispatcher(path) is used to create it we need to give the relative path of the resource whereas context.getRequestDispatcher(path)to create it we need to give the absolute path of the resource.


28) 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.


29) What are the primary 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.

30) Can an interface be implemented in the JSP file?

No.


31) What is JSTL?

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

More details.

32) How many tags are provided in JSTL?

There is 5 type of JSTL tags.

  1. core tags
  2. sql tags
  3. xml tags
  4. internationalization tags
  5. functions tags
More details.

33) Which directive is used in JSP custom tag?

The JSP taglib directive.


34) What are the three tags used in JSP bean development?

  1. jsp:useBean
  2. jsp:setProperty
  3. jsp:getProperty
More details.

35) How to disable session in JSP?

36) List the various action tags used in JSP.

Following are the list of various action tags used in JSP:

  • jsp:forward: This action tag forwards the request and response to another resource.
  • jsp:include: This action tag is used to include another resource.
  • jsp:useBean: This action tag is used to create and locates bean object.
  • jsp:setProperty: This action tag is used to set the value of the property of the bean.
  • jsp:getProperty: This action tag is used to print the value of the property of the bean.
  • jsp:plugin: This action tag is used to embed another component such as the applet.
  • jsp:param: This action tag is used to set the parameter value. It is used in forward and includes mostly.
  • jsp:fallback: This action tag can be used to print the message if the plugin is working.

37) Explain the steps for creating custom tags in JSP?

For creating any custom tag, we need to follow the following steps:

  1. Create the Tag handler class
    To generate the Tag Handler, we are inheriting the TagSupport class and overriding its method doStartTag().To write data for the JSP, we need to use the JspWriter class.
    The PageContext class provides getOut() method that returns the instance of JspWriter class. TagSupport class provides an instance of pageContext by default.
  2. Create the TLD file
    Tag Library Descriptor (TLD) file contains information of tag and Tag Hander classes. It must hold inside the WEB-INF directory.
  3. Create the JSP file
    Let's use the tag in our JSP file. Here, we are specifying the path of tld file directly. However, it is recommended to use the URI name instead of full a path of tld file. We will learn about URI later. It uses taglib directive to use the tags defined in the tld file.

38) How can the applets be displayed in the JSP? Explain with an example.

The jsp:plugin action tag is used to embed an applet in the JSP file. The jsp:plugin action tag downloads plugin at client side to execute an applet or bean.

Example of displaying applet in JSP


39) What is Expression language in JSP?

The Expression Language (EL) simplifies the accessibility of data stored in the Java Bean component, and other objects like request, session, application.

There are many implicit objects, operators and reserve words in EL.

It is the newly added feature in JSP technology version 2.0.


40) Explain various implicit objects in expression language.

Implicit Objects Usage
pageScope it maps the given attribute name with the value set in the page scope
requestScope it maps the given attribute name with the value set in the request scope
sessionScope it maps the given attribute name with the value set in the session scope
applicationScope it maps the given attribute name with the value set in the application scope
param it maps the request parameter to the single value
paramValues it maps the request parameter to an array of values
header it maps the request header name to the single value
headerValues it maps the request header name to an array of values
cookie it maps the given cookie name to the cookie value
initParam it maps the initialization parameter
pageContext it provides access to many objects request, session, etc.