0% found this document useful (0 votes)
13 views5 pages

Java Unit 3 by Sana

Uploaded by

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

Java Unit 3 by Sana

Uploaded by

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

Scope of JSP Objects

 In a JSP page, objects may be created using directives, actions, or scriptlets.


 Every object created in a JSP page has a scope. The scope of a JSP object is defined as
the availability of that object for use from a particular place of the web application.
 There are four object scope: page, request, session, and application.

Page Scope:
Objects having page scope can be accessed only from within the same page where they
were created. JSP implicit objects such as out, exception, response, pageContext, config, and
page have page scope.

Request Scope:
Objects having request scope can be accessed from any page that serves the same request.
A request can be served by more than one page. The implicit object request has request
scope.

Session Scope:
Objects having session scope are accessible from pages that belong to the same session in
which they were created. The implicit object session has session scope.

Application Scope:
Objects having application scope can be accessed from any page that belongs to the same
application. The implicit object application has application scope.

Directives
 A JSP page may contain instructions to be used by the JSP container to indicate how this
page is interpreted and executed. Those instructions are called directives.
 They are enclosed within the <%@ and %> tags

Types of JSP Directive Tags:


There are three types of directive tags in JSP:
1. Page Directive:
The page directive is used to provide instructions about the entire JSP page, such as setting
the character encoding, content type, importing classes, and enabling session management.
E.g
<%@ page language="java" contentType="text/html; charset=UTF-8" %>
<%@ page import="java.util.Date" %>
<%@ page session="true" %>

2. Include Directive:
The include directive is used to include a file (like another JSP page or static content) in the
current JSP page at the time of page translation (before the JSP is compiled).
Example:
<%@ include file="header.jsp" %
3. Taglib Directive:
The taglib directive is used to declare and define the usage of custom tag libraries, such as
JSTL (JavaServer Pages Standard Tag Library).
Example:
jsp
Copy code
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
IMPLICIT OBJECTS
JSP implicit objects are pre-defined objects that are automatically available in all JSP pages
without needing to explicitly declare or instantiate them.
The implicit objects contain information about request, response, session, configuration, etc.
1. request
 Type: javax.servlet.http.HttpServletRequest
 Purpose: Contains all the information related to the HTTP request. It allows you to
retrieve data submitted via form.
Example:
String username = request.getParameter("username");
2. response
 Type: javax.servlet.http.HttpServletResponse
 Purpose: Manages the HTTP response to the client.
 Typically used to redirecting the client to another page.
Example:
response.sendRedirect("home.jsp");
3. out
 Type: javax.servlet.jsp.JspWriter
 Purpose: Used to send output to the client
 Example:
out.println("Hello, World!");
4. session
 Type: javax.servlet.http.HttpSession
 Purpose: It allows you to store and retrieve user-specific data across multiple
requests.
Example:
session.setAttribute("user", "Sana");
String user = (String) session.getAttribute("user");

JSTL
JSTL abbreviated as Java Standard Tag Library, which is a further extension for JSP (Java
Server Pages).
JSTL simplifies common tasks like iteration, conditionals, formatting, and data handling using
the following tag
1. Core Tags (c prefix):
These tags are the most commonly used to provide basic functionality like iteration,
conditional operations, URL management.
Prefix:c
Example Tags:
o <c:forEach>: Iterates over a collection of items.
o <c:if>: Conditionally displays content.
o <c:set>: Sets a variable.
Uri : http://java.sun.com/jsp/jstl/core
2. Function Tags (fn prefix):
These tags provide functions to manipulate strings, collections, and other objects.
Prefix: fn
 Example Tags:
o fn:length(): Gets the length of a collection or string.
o fn:contains(): Checks if a string contains a substring.
Uri : http://java.sun.com/jsp/jstl/functions
3. Formatting Tags (fmt prefix):
These tags are used for formatting numbers, dates, and messages.
 Example Tags:
o <fmt:formatNumber>: Formats a number based on a locale.
o <fmt:message>: Displays localized messages.
Uri : http://java.sun.com/jsp/jstl/fmt
5. SQL Tags (sql prefix):
These tags are used to interact with databases directly from the JSP page.
 Example Tags:
o <sql:query>: Executes a database query.
o <sql:update>: Executes an SQL update statement.
Uri : http://java.sun.com/jsp/jstl/sql

4. XML Tags (x prefix):


These tags allow for manipulation and transformation of XML data.
. They are helpful when dealing with XML documents, XPath expressions
1.<x:parse> Tag
 Description: This tag parses an XML string and creates a document object that can be
manipulated.
 Example:
<x:parse var="xmlDoc" xml="${xmlString}" />
2.<x:out> Tag
 Description: This tag outputs the value of a specified XML element.
 Example:
<x:out select="$xmlDoc/root/element" />
3.<x:if> Tag
 Description: This tag checks a condition in XML data and displays content only if that
condition is true.
 Example:
<x:if test="$xmlDoc/root/element='value'">
<p>Element has the specified value!</p>
</x:if>
4.<x:choose> Tag
 Description: This tag enables conditional logic similar to a switch statement, allowing
you to define multiple conditions.
5.<x:when> Tag
 Description: Used inside <x:choose>, this tag specifies a condition, and if true, the
content inside <x:when> is displayed.

An interceptor in EJB is a special method that allows developers to add additional behavior
to EJB methods without modifying the original code.
Interceptors can run before (pre-interceptors) or after (post-interceptors) a method
/business execution,
They are used to handle common tasks like /Uses:
Validate parameters
Logging, security checks,
managing transactions

You might also like