Top JSP (JavaServer Pages) Interview Questions
This section covers a wide range of JSP interview questions, suitable for both beginners and experienced professionals. It includes questions on JSP fundamentals, lifecycle, implicit objects, custom tags, and the Expression Language (EL).
What is JSP?
JSP (JavaServer Pages) is a server-side technology for creating dynamic web pages using HTML, Java, and JSP tags. A JSP page is essentially a servlet, simplifying web application development by allowing you to separate presentation logic (the JSP page) from business logic (Java code in beans or other classes).
JSP Lifecycle Methods
Method | Description |
---|---|
jspInit() |
Initialization (called once). Similar to a servlet's init() method. |
_jspService() |
Handles each request. Similar to a servlet's service() method. |
jspDestroy() |
Cleanup (called once before the JSP is unloaded). Similar to a servlet's destroy() method. |
Advantages of JSP
- Improved performance (pre-compilation).
- Access to Java APIs (including enterprise-level APIs).
- Easy maintenance (separation of presentation and business logic).
- Can be used with servlets.
JSP Comments
Syntax
<%-- This is a JSP comment --%>
Hidden vs. Output Comments
JSP comments (<%-- ... --%>
) are hidden from the client's browser. HTML comments (<!-- ... -->
) are included in the rendered HTML.
JSP Implicit Objects
JSP provides built-in objects:
Object | Type |
---|---|
out |
JspWriter |
request |
HttpServletRequest |
response |
HttpServletResponse |
config |
ServletConfig |
session |
HttpSession |
application |
ServletContext |
pageContext |
PageContext |
page |
Object |
exception |
Throwable |
More details on JSP implicit objects
`include` Directive vs. `include` Action
Feature | `include` Directive | `include` Action |
---|---|---|
Inclusion Time | Translation time | Request time |
Content | Includes the original content of the included file | Includes the result of the included page; does not include the original page content |
Best Use Case | Static content inclusion | Dynamic content inclusion |
Extensibility of JSP
JSP is extensible through custom tag libraries (which provide reusable custom tags).
Thread-Safe JSP Pages
Make a JSP thread-safe by implementing the SingleThreadModel
interface (using the <%@ page isThreadSafe="false" %>
directive). This ensures that only one thread can access the JSP at a time, preventing concurrency issues but impacting performance.
Disabling Browser Caching
JSP Code
<%
response.setHeader("Cache-Control", "no-store");
response.setHeader("Pragma", "no-cache");
response.setDateHeader("Expires", 0);
%>
Exception Handling in JSP
Handle exceptions in JSP pages using either the errorPage
attribute in the page
directive or by configuring an error page in web.xml
.
More details on JSP exception handling
Including Another Page in JSP
Two methods for including content from another page:
include
directive (at translation time)include
action (at request time)
Request Forwarding from JSP to Servlet
Use the jsp:forward
action to forward a request to a servlet (you need the servlet's URL pattern).
Syntax
<jsp:forward page="/myservlet" />
Using the exception
Object
The implicit exception
object is only available in error pages (specified using isErrorPage="true"
in the page
directive).
More details on the exception object
JSP in the MVC Model
JSPs typically serve as the view in the MVC (Model-View-Controller) architectural pattern. They display the data provided by the model (business logic) and managed by the controller.
More details on JSP and the MVC model
Context Initialization Parameters
Context initialization parameters (defined in web.xml
) provide configuration settings for the entire web application, not just individual servlets or JSPs.
More details on Context Initialization Parameters
Scope Values for the `jsp:useBean` tag
The jsp:useBean
tag can have four scope values (page
, request
, session
, application
), controlling the bean's lifespan.
JSP Literals
JSP literals include boolean, integer, floating-point, string, and null values.
`jsp:useBean` Action
The jsp:useBean
action finds or creates a bean instance.
`jsp:setProperty` Action
This action sets the properties of a bean.
`jsp:getProperty` Action
This action retrieves and displays the value of a bean property.
JSP Action Tag Scopes
JSP action tags can have the same four scope values as the `jsp:useBean` tag (page
, request
, session
, application
).
The out
Implicit Object
The out
object (a JspWriter
) is used to send output to the client's browser.
The session
Implicit Object
The session
object (an HttpSession
) tracks a user's session across multiple requests.
The exception
Implicit Object
The exception
object (a Throwable
) contains information about exceptions that occurred; it's only available in error pages.
`ServletContext` vs. `PageContext`
ServletContext
provides information about the entire web application. PageContext
provides information specific to the current request.
`request.getRequestDispatcher()` vs. `context.getRequestDispatcher()`
request.getRequestDispatcher()
uses a relative path; context.getRequestDispatcher()
uses an absolute path to include or forward to another resource.
EL (Expression Language) in JSP
EL simplifies accessing data in beans and other objects within JSPs.
JSP Custom Tags vs. JavaBeans
Feature | Custom Tags | JavaBeans |
---|---|---|
Content Manipulation | Can directly manipulate JSP content | Cannot directly manipulate JSP content |
Complexity | Simplify complex operations | Can become complex for intricate logic |
Setup | More complex to set up | Relatively easier to set up |
Implementing Interfaces in JSP Files
No, you cannot directly implement interfaces in JSP files.
JSTL (JSP Standard Tag Library)
JSTL is a set of predefined tags for common tasks, simplifying JSP development.
JSTL Tag Categories
- Core tags
- SQL tags
- XML tags
- Internationalization tags
- Function tags
JSP Custom Tag Directive
The taglib
directive is used to declare custom tag libraries in JSP.
JSP Bean Development Tags
jsp:useBean
jsp:setProperty
jsp:getProperty
Disabling Session in JSP
Directive
<%@ page session="false" %>
JSP Action Tags
[Describe the purpose of each action tag: jsp:forward
, jsp:include
, jsp:useBean
, jsp:setProperty
, jsp:getProperty
, jsp:plugin
, jsp:param
, jsp:fallback
]
Creating Custom Tags
- Create a tag handler class (extending
TagSupport
). - Create a Tag Library Descriptor (TLD) file.
- Use the tag in your JSP file using the
taglib
directive.
Displaying Applets in JSP
Use the jsp:plugin
action tag to embed an applet in your JSP.
Expression Language (EL) in JSP
EL simplifies accessing data from JavaBeans and other objects within JSP pages, making the code more readable and maintainable.
JSP Expression Language (EL) and Implicit Objects
This section focuses on the JSP Expression Language (EL) and its implicit objects, a key feature for simplifying data access within JSP pages.
Expression Language (EL) in JSP 2.0
The Expression Language (EL), introduced in JSP 2.0, simplifies accessing and manipulating data within JSPs. It provides a concise syntax for accessing values from various scopes and objects without needing complex Java code.
EL Implicit Objects
EL provides several implicit objects that you can use directly in your JSP expressions. These objects provide access to different scopes (page, request, session, application) and other contextual information.
Implicit Object | Description |
---|---|
pageScope |
Accesses attributes in the page scope. |
requestScope |
Accesses attributes in the request scope. |
sessionScope |
Accesses attributes in the session scope. |
applicationScope |
Accesses attributes in the application scope. |
param |
Accesses a single request parameter. |
paramValues |
Accesses an array of request parameter values. |
header |
Accesses a single HTTP request header. |
headerValues |
Accesses an array of HTTP request header values. |
cookie |
Accesses a cookie value. |
initParam |
Accesses a context initialization parameter. |
pageContext |
Provides access to other page-related objects. |
These implicit objects make it easier to access data and reduce the amount of Java code needed in your JSPs.