0% found this document useful (0 votes)
24 views13 pages

Implicit Objects, Scope and EL Expression

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views13 pages

Implicit Objects, Scope and EL Expression

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 13

Implicit objects,

Scope and
EL expression

• BY NIDHI GAWAND
IMPLICIT OBJECTS
Implicit objects in JSP (JavaServer Pages) are predefined objects that are
automatically available in the JSP page, providing access to various aspects of
the request, response, and application.

Here are the 9 implicit objects in JSP:


1. *request*: HttpServletRequest object
- Represents the HTTP request.
- Methods: `request.getParameter()`, `request.getAttribute()`, etc.

2. *response*: HttpServletResponse object


- Represents the HTTP response.
- Methods: `response.getWriter()`, `response.sendRedirect()`, etc.

3. *pageContext*: PageContext object


- Provides access to the JSP page’s context.
- Methods: `pageContext.getAttribute()`, `pageContext.findAttribute()`,
etc.
4. *session*: HttpSession object
- Represents the user’s session.
- Methods: `session.getAttribute()`, `session.setAttribute()`, etc.

5. *application*: ServletContext object


- Represents the web application.
- Methods: `application.getAttribute()`, `application.getInitParameter()`,
etc.

6. *out*: JspWriter object


- Used to write output to the JSP page.
- Methods: `out.println()`, `out.write()`, etc.

7. *config*: ServletConfig object


- Represents the servlet configuration.
- Methods: `config.getInitParameter()`, `config.getServletContext()`, etc.
8. *page*: Object (the current JSP page)
- Represents the current JSP page.
- Methods: `page.getClass()`, `page.toString()`, etc.

9. *exception*: Throwable object (in error pages)


- Represents the exception that occurred.
- Methods: `exception.getMessage()`,
`exception.printStackTrace()`

 These implicit objects can be used directly in JSP code without


needing to declare or initialize them.
Example JSP code:

<%@ page language="java"


contentType="text/html; charset=UTF-8"
%>
<html><head>
<title>Implicit Objects</title>
</head>
<body> <h1>Implicit Objects</h1>
<p>Request Method:
<%=request.getMethod()%></p>
<p>Session ID: <%= session.getId()
%></p> <p>Application
Context Path: <%=
application.getServletContextName()
%></p>
</body>
</html>
SCOPE
 A powerful feature of JSP is that it can access, create and modify
data objects on the Web server.
 Objects, which are created as part of a JSP have a certain lifespan
and may or may not be accessible to other components or objects
in an application.
 The lifespan or the accessibility of the object is known as its
Scope.
 The JSP specification supports the following four levels of scope:
1. Application
2. Request
3. Session
4. Page
Application Scope :

 Application scope is the broadest or the least restrictive


scope and should only be used when necessary.
 Objects with application scope are accessible from JSP
pages that reside in the same application.
 The objects created with application scope are not thread
safe and access to them must be synchronized if there is
a chance that multiple requests will attempt to change
the object at the same time.
 Objects can be created at application level in JSPs that
are not session aware.
 Hence, application scope is useful for storing information
when using these types of JSPs application bound objects
can be used to share data among different sessions of the
same.
Request Scope:

 Objects with request scope are accessible from pages


processing the same request in which they were created.
 In other words, objects created to request scope are
available to pages in that same request.
 These objects are automatically released from memory
when request processing completes.
 Once the JSP engine has processed the request, the data is
released.
 Even if the request is forwarded to another page, the data
is still available though not if a redirect is required.
 This is advantageous because explicit cleanup is not
required and there is less risk of burdening the system
with needless memory consumption.
Session Scope :

 A session is the time the user spends using the application,


which ends when they close their browser, when they go to
another websites.
 Session scope is more commonly used than application
scope.
 Session scope allows creating and binding objects to a
session.
 Objects bound to the session are created in session aware
JSP pages. This makes them available to all JSP pages and
Servlets in the same session.
 Session scope is often used for managing security
credentials and for managing state among multiple pages
such as in a Web-based wizard etc.
Page Scope :

 Use page scope for objects created only for the current page.
 Objects with page scope are accessible only within the page in
which they are created.
 The data is valid only during the processing of the current
response,
 Once the response is sent back to the user browser the data is no
longer valid.
 If the request is forwarded to another page or the browser
makes another request as a result of a redirect, then such data is
also lost.
EL EXPRESSION

 Expression Language (EL) expressions are used in JavaServer Pages


(JSP) to access and manipulate data.

 Syntax: `${expression}`

 EL Expression Types:
 1. Literal Expressions: `${“Hello”}`
 2. Expression Statements: `${5 + 5}`
 3. Method Calls: `${user.getName()}`
 4. Property Access: `${user.name}`
 5. List/Array Access: `${users[0]}`

 Operators:
 1. Arithmetic: `+`, `-`, `*`, `/`, `%`
 2. Comparison: `==`, `!=`, `>`, `<`, `>=` , `<=`
3. Logical: `&&`, `||`, `!`
 Benefits:
 Simplifies JSP code
 2. Improves readability
 3. Reduces scripting
4. Keep expressions simple.

 Some common EL expressions:


 - `${param.username}`: Access request parameter
 - `${sessionScope.user}`: Access session attribute
 - `${applicationScope.config}`: Access application
attribute
 - `${header[‘User-Agent’]}`: Access request header
- `${cookie[‘JSESSIONID’]}`: Access cookie value
THANK YOU

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy