Sample questions
Question 1Easy
Which JSP implicit object represents the HttpSession?
- application
- pageContext
- session
- 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
- The container creates implicit objects in the translated servlet.
session maps to HttpSession.- 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?
- The text null printed on the page
- An error page with a stack trace
- The literal word name
- 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
- EL searches page, request, session, application.
- Nothing found.
- 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" />?
- The directive runs at request time; the action merges at translation time into the servlet source
- The directive merges the file at translation time; the action includes the output at request time
- Both are identical in timing and behaviour, only the syntax and the tag library differ
- 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
- Directive: textual copy during translation.
- Action: runtime call of the included resource.
- 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" />?
- The directive runs at request time; the action merges the source at compile time, as one single unit
- Both merge source at compile time
- The directive merges the file at translation time; the action includes the output at request time
- 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
- Directive: source copied, one servlet generated.
- 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?
- <% ... %>
- <%! ... %>
- <%= ... %>
- <%-- ... --%>
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
- Declaration: class-level code.
- Scriptlet: inside _jspService.
- 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?
- <c:forEach items="${items}" var="it">
- <c:loop list="${items}" var="it" end="all">
- <c:for each="${items}">
- <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
- Import the core taglib with a prefix
c. - Use
items for the collection. - 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?
- page, request, session, application
- application, session, request, page
- request, page, application, session
- 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
- Check page scope.
- Then request.
- Then session.
- 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?
- SQL injection; use PreparedStatement in the JSP
- Session fixation; call session.invalidate()
- No risk, since the JSP compiler escapes all output
- 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
- Raw
<%= %> writes the string as is. - The browser sees a script tag and runs it.
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 →