What is meant by scope of JSP objects? Explain different scopes of JSP objects briefly with example.
SCOPE OF JSP OBJECTS
The availability of a JSP object for use from a particular place of the application is defined as the scope of that JSP object. Every object created on a JSP page will have a scope. Object scope in JSP is segregated into four parts and they are page, request, session, and application.
a) Page Scope-page scope means, the JSP object can be accessed only from within the same page where it was created. JSP implicit objects out, exception, response, pageContext, config and page have page scope.
//Example of JSP Page Scope
<jsp:useBean id="employee" class="EmployeeBean" scope="page" />
b) Request Scope- A JSP object created using the request scope can be accessed from any pages that serves that request. More than one page can serve a single request. Implicit object request has the request scope.
//Example of JSP Request Scope
<jsp:useBean id="employee" class="EmployeeBean" scope="request" />
c) Session Scope-session scope means, the JSP object is accessible from pages that belong to the same session from where it was created. Implicit object session has the session scope.
//Example of JSP Session Scope
<jsp:useBean id="employee" class="EmployeeBean" scope="session" />
d)Application Scope- A JSP object created using the application scope can be accessed from any page across the application. The. Implicit object application has the application scope.
//Example of JSP application Scope
<jsp: useBean id="employee" class="EmployeeBean" scope="application" />
//Example---First JSP Example---first.jsp File
<html>
<head>
<title>First JSP Example</title>
</head>
<body>
<font size=3>
<br><br><center><h2>
<%-- Display by using expression tag --
<%= "First JSP Example" %>
<hr color=blue size=3>
<% Display by using scriptlet tag --%>
<%
out.println("Hello WebApp developer..... .....Welcome to JSP");
%>
<center>
</font>
</body>
</html>
Comments
Post a Comment