JSP & EL Questions with Answers

8 practice questions on JSP & EL (Advanced Java) with answers and step-by-step solutions. Try 8 samples below, then practise all 8 free in the AptiNest app.

Sample questions

Question 1Easy

Which JSP implicit object represents the HttpSession?

  1. application
  2. pageContext
  3. session
  4. config

Answer: C. session

Explanation

JSP gives ready-made variables. session is the HttpSession of the current user (unless the page disables it with session="false").

Step-by-step solution

  1. The container creates implicit objects in the translated servlet.
  2. session maps to HttpSession.
  3. Use it directly in scriptlets.

Why the other options are wrong

  • A. application is the ServletContext shared by all users.
  • B. pageContext gives access to all scopes.
  • D. config is the ServletConfig of this JSP servlet.

Question 2Easy

What does the EL expression ${name} print if no attribute called name exists in any scope?

  1. The text null printed on the page
  2. An error page with a stack trace
  3. The literal word name
  4. Nothing (an empty string)

Answer: D. Nothing (an empty string)

Explanation

EL treats a missing attribute as null and renders it as an empty string, so no error appears.

Step-by-step solution

  1. EL searches page, request, session, application.
  2. Nothing found.
  3. Output is empty.

Why the other options are wrong

  • A. EL does not print the literal text null.
  • B. EL is forgiving and does not throw for a missing attribute.
  • C. The expression name is not echoed as text.

Question 3Medium

What is the main difference between the directive <%@ include file="a.jsp" %> and the action <jsp:include page="a.jsp" />?

  1. The directive runs at request time; the action merges at translation time into the servlet source
  2. The directive merges the file at translation time; the action includes the output at request time
  3. Both are identical in timing and behaviour, only the syntax and the tag library differ
  4. The directive can only include HTML, the action only Java classes

Answer: B. The directive merges the file at translation time; the action includes the output at request time

Explanation

A directive include is static: the content is copied into the JSP before it is compiled. jsp:include is dynamic and calls the target on each request, so changes to the target show without recompiling the including page.

Step-by-step solution

  1. Directive: textual copy during translation.
  2. Action: runtime call of the included resource.
  3. Choose action for content that changes.

Why the other options are wrong

  • A. The timings are reversed in this option.
  • C. They differ in when inclusion happens.
  • D. Both can include JSP or HTML resources.

Question 4Medium

What is the main difference between <%@ include file="a.jsp" %> and <jsp:include page="a.jsp" />?

  1. The directive runs at request time; the action merges the source at compile time, as one single unit
  2. Both merge source at compile time
  3. The directive merges the file at translation time; the action includes the output at request time
  4. The action can include only static HTML files, never JSPs or servlets

Answer: C. The directive merges the file at translation time; the action includes the output at request time

Explanation

The include directive copies the source into the page before it is compiled. jsp:include calls the other resource at run time and inserts its output.

Step-by-step solution

  1. Directive: source copied, one servlet generated.
  2. Action: separate resource executed per request.

Why the other options are wrong

  • A. It is the opposite; the directive is static.
  • B. jsp:include is dynamic and is not merged at compile time.
  • D. jsp:include can include JSPs and servlets.

Tip: Static vs dynamic include.

Question 5Medium

Which JSP element declares a method that becomes a member of the generated servlet class?

  1. <% ... %>
  2. <%! ... %>
  3. <%= ... %>
  4. <%-- ... --%>

Answer: B. <%! ... %>

Explanation

A declaration <%! %> puts code at class level, so fields and methods can be defined. Scriptlet code is placed inside the service method.

Step-by-step solution

  1. Declaration: class-level code.
  2. Scriptlet: inside _jspService.
  3. Expression: written to out.

Why the other options are wrong

  • A. Scriptlets go inside _jspService, so a method cannot be declared there.
  • C. An expression is output, not a declaration.
  • D. That is a hidden JSP comment.

Question 6Medium

In JSTL, which tag is used to loop over a list stored in the request attribute items?

  1. <c:forEach items="${items}" var="it">
  2. <c:loop list="${items}" var="it" end="all">
  3. <c:for each="${items}">
  4. <jsp:forEach items="${items}" var="it">

Answer: A. <c:forEach items="${items}" var="it">

Explanation

The JSTL core library provides c:forEach, with items taking an EL collection and var naming each element.

Step-by-step solution

  1. Import the core taglib with a prefix c.
  2. Use items for the collection.
  3. Use var to refer to the current element.

Why the other options are wrong

  • B. c:loop is not a JSTL tag.
  • C. c:for each is not valid syntax.
  • D. jsp:forEach does not exist; JSP standard actions have no loop.

Question 7Medium

In what order does ${user} search scopes?

  1. page, request, session, application
  2. application, session, request, page
  3. request, page, application, session
  4. session, request, page, application

Answer: A. page, request, session, application

Explanation

EL looks from the narrowest scope to the widest and stops at the first match.

Step-by-step solution

  1. Check page scope.
  2. Then request.
  3. Then session.
  4. Finally application.

Why the other options are wrong

  • B. That is the reverse order.
  • C. Page is checked first, not the application.
  • D. Session comes after request.

Tip: Narrow to wide.

Question 8Hard

A JSP uses <%= x %> where x is a request attribute holding user text like <script>alert(1)</script>. What is the risk, and the usual fix in JSTL?

  1. SQL injection; use PreparedStatement in the JSP
  2. Session fixation; call session.invalidate()
  3. No risk, since the JSP compiler escapes all output
  4. Cross-site scripting; output with <c:out value="${x}"/> which escapes XML characters

Answer: D. Cross-site scripting; output with <c:out value="${x}"/> which escapes XML characters

Explanation

Printing raw text lets the browser run injected script (XSS). c:out escapes <, >, & and quotes by default.

Step-by-step solution

  1. Raw <%= %> writes the string as is.
  2. The browser sees a script tag and runs it.
  3. c:out converts markup characters to entities.

Why the other options are wrong

  • A. The problem here is output, not SQL.
  • B. Session fixation is a different attack.
  • C. JSP does not escape expression output automatically.

Tip: Escape on output, validate on input.

Practise all 8 questions free

The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.

Open this topic in the app →

Related topics

See all Advanced Java topics · All topics