Java Unit 3 by Sana
Java Unit 3 by Sana
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
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
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