JSP Interview Questions and Answers

    1 Votes

16. How variable behaves defined inside a scriptlet element and declaration element?

Variable defined inside scriptlet are like method local variables once the method execution is complete the value stored would be lost. Variables defined inside declaration are like class level instance variables and values will be retained across user requests.

17. What is a JSP directive?

A directive provides meta data information about the JSP file to the web container. Web container uses this during the translation/compilation phase of the JSP life cycle.

18. Give few examples of JSP directives?

Examples of JSP directive are:

  • Importing tag libraries
  • Import required classes
  • Set output buffering options
  • Include content from external files.

19. What are the different types of JSP directives?

The JSP specification defines three directives:

  • Page directive: Provides information about page, such as scripting language that is used, content type, or buffer size etc.
  • Include directive: Used to include the content of external files.
  • Taglib directive: Used to import custom tags defined in tag libraries. Custom tags are typically developed by developers.

20. What is the syntax for JSP directive?

A JSP directive is declared using following syntax:

<%@directive attribute=”value” %>

Where, directive – The type of directive(page, taglib or include)
attribute – Represents the behavior to be set for the directive to act upon.

21. What is page directive in JSP?

The page directive is used to provide the metadata about the JSP page to the container. Page directives may be coded anywhere in JSP page. By standards, page directives are coded at the top of JSP page. A page can have any number of page directives.

Syntax: <%@page attribute1=”value” attribute2=”value” %>

22. What is the work of include directive?

This directive inserts a HTML file, JSP file or text file at translation time. The include process is not dynamic, it means that the text of the included file is added to the JSP file similar to copy pasting the contents.

23. Should included file in include directive contain <html> or <body> tags and why?

Included file should not contain <html> or <body> tags because the entire content of the included file is added to the main JSP file, these tags would conflict with the same tags in the main JSP file, causing an error.

24. How to create an include directive?

Include file is created using the following syntax:

<%@include attribute=”value”%> - Include can have only file attribute.
<%@include file=”value”%> - Where file specifies the relative path of the file to be included.

25. What are JSP implicit objects?

Implicit objects in JSP are the objects that are created by the web container automatically and the container makes them available to the JSP to access it. Implicit objects are available only inside the _jspService() method hence cannot be accessed anywhere outside.

26. List all the JSP implicit objects?

List of implicit objects are

  • request
  • response
  • session
  • application
  • page
  • pagecontext
  • out
  • config
  • exception.

27. Describe briefly about all the JSP implicit objects?

  • Request – It references to the current request.
  • Response – HTTPServletResponse object for the current request.
  • Session – It is the session associated with the current request.
  • Application – Servlet context to which a page belongs.
  • Page – An instance of JSP page’s servlet class that processes the current request.
  • Pagecontext – It is an object to access request, response, session and application associated with a page.
  • Out – Object that writes to the response output stream.
  • Config – Servlet configuration for the page.
  • Exception – This object is available in JSP pages where the page directive isErrorpage is set as true stating that it is an error page for some other JSP page.

28. What are the session management techniques in JSP?

The session management techniques used in servlets are also applicable to handle session in JSP also. The following are the techniques used to manage session in JSP:

  • URL rewriting
  • Hidden field
  • Cookie
  • Session object

29. What is an expression language?

Expression language is a simple language for accessing data stored in java beans. This was introduced with the JSP 2.0 specification. It can also be used to access the values from implicit objects like page, context, header, cookie etc.

30. How expression language is specified?

Expression language is specified within curly braces and prefixed with a dollar sign.

Example: ${person.name}

Where ‘person’ is the java bean and ‘name’ is the java bean property.

31. What are the advantages of JSP Expression Language (EL) ?

    • Concise access to stored objects: Easy to access the attributes stored in session, request or application scope. 
Example: ${name} search the PageContext, HTTPServletRequest, HTTPSession and ServletContext(in that order) for an attribute named name.
Equivalent to <%= pageContext.findAttribute(“name”) %>
    • Short hand notation for bean properties: Easy to access bean properties.
Example: To print the property phoneNumber of a user bean we can use
${user.phoneNumber} instead of <%= user.getPhoneNumber()%>
    • Easy access to collection elements: To access an element of an array, List, or Map, you use ${variable[index or key]}.
Example: Consider an arraylist myList
${myList[0]}, gets the first element in the list and prints it.
    • Easy access to request parameters, cookies, and other request data. In order to access the standard types of request data, you can use one of several predefined implicit objects.
    • Conditional output: To choose among output options, we don't have to use java coding elements. Instead, you can use
${test ? option1 : option2}

 32. What is the use of period (.) operator in JSP Expression Language (EL)?

  • To access implicit objects attributes:
  • For example, to read an attributemessage in sessionScope we use 
  • ${sessionScope.message}
  • To access bean properties:
  • Consider a bean named personhaving a property name which can be accessed as {person.name}
  • To access map values:
  • Consider a map named country having a key named “ind”. Value of the key can be accessed as ${country.ind}.
Page 2 of 5

Popular Videos


How to improve your Interview, Salary Negotiation, Communication & Presentation Skills.

Got a tip or Question?
Let us know

Related Articles

Servlet Interview Questions and Answers
Advanced Servlet Interview Questions and Answers
EJB Interview Questions and Answers
Spring Interview Questions and Answers