There are four scopes in JSP. 

Scope Description
Page Stores a value to be shared within one JSP page.
Request Stores values to be shared on all JSP pages used to process a single request.
Session Shares information related to a user.
Application Stores information to be shared with all users.

For your better understanding, please refer to this picture. Page is the narrowest scope, and the application is the widest scope. 

The basic objects related to JSP's scopes are pageContext, request, session, and application.

 

Here are some examples of scopes. With them, you will have a better understanding of which data will be saved in which scope. 

AtrributeTest1_Form.jsp

<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<html>
<head>
<title>Attribute Test Form</title>
</head>
<body>
<h2>Scopes and properties</h2>
<form action="attributeTest1.jsp" method="post">
<table border="1">
	<tr><td colspan="2">Contents for Application</td></tr>
	<tr>
		<td>Name</td>
		<td><input type="text" name="name"></td>
	</tr>
	<tr>
		<td>ID</td>
		<td><input type="text" name="id"></td>
	</tr>
	<tr>
		<td colspan="2"><input type="submit" value="전송"></td>
	</tr>
</table>
</form>
</body>
</html>

AttributeTest1.jsp

<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<html>
<head>
<title>Attribute Test</title>
</head>
<body>
<h2>Scopes and properties</h2>
<%
request.setCharacterEncoding("euc-kr");
String name=request.getParameter("name");
String id=request.getParameter("id");
if(name!=null&&id!=null){
	application.setAttribute("name",name);
	application.setAttribute("id",id);
}
%>
<h3>Welcome, <%=name %>.<br><%=name %>'s ID is <%=id %>.</h3>
<form action="attributeTest2.jsp" method="post">
<table border="1">
	<tr><td colspan="2">Contents for Session</td></tr>
	<tr>
		<td>E-mail</td>
		<td><input type="text" name="email"></td>
	</tr>
	<tr>
		<td>Address</td>
		<td><input type="text" name="address"></td>
	</tr>
	<tr>
		<td>전화번호</td>
		<td><input type="text" name="tel"></td>
	</tr>
	<tr>
		<td colspan="2"><input type="submit" value="전송"></td>
	</tr>
</table>
</form>
</body>
</html>

AttributeTest2.jsp

<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<html>
<head>
<title>Attribute Test</title>
</head>
<body>
<h2>Scopes and properties</h2>
<%
request.setCharacterEncoding("euc-kr");
String email=request.getParameter("email");
String address=request.getParameter("address");
String tel=request.getParameter("tel");
session.setAttribute("email",email);
session.setAttribute("address",address);
session.setAttribute("tel",tel);

String name=(String)application.getAttribute("name");
%>
<h3><%=name %>'s data is successfully saved.</h3>
<a href="attributeTest3.jsp">Click to check</a>
</body>
</html>

 

AttributeTest3.jsp

<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<%@page import="java.util.Enumeration"%>
<html>
<head>
<title>Attribute Test</title>
</head>
<body>
<h2>Scopes and properties</h2>
<table border="1">
	<tr><td colspan="2">Content for Application</td></tr>
	<tr>
		<td>Name</td>
		<td><%=application.getAttribute("name") %></td>
	</tr>
	<tr>
		<td>ID</td>
		<td><%=application.getAttribute("id") %></td>
	</tr>
</table>
<br>
<table border="1">
	<tr><td colspan="2">Content for Session</td></tr>
<%-- <%
Enumeration e=session.getAttributeNames();
while(e.hasMoreElements()){
	String attributeName=(String)e.nextElement();
	String attributeValue=(String)session.getAttribute(attributeName);
	%>
	<tr>
		<td><%=attributeName %></td>
		<td><%=attributeValue %></td>
	</tr>
	<%
}
%> -->

		<tr>
			<td>Email</td>
			<td><%=session.getAttribute("email")%></td>
		</tr>
		<tr>
			<td>Tel</td>
			<td><%=session.getAttribute("tel")%></td>
		</tr>
		<tr>
			<td>Address</td>
			<td><%=session.getAttribute("address")%></td>
		</tr>
</table>
</body>
</html>

Since the Name and the ID are shared by the application object, you can use that data in other scopse too, because the application object is the broadest scope. 

 

To set application Attribute

index.jsp

<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
</head>
<body>
My first JSP program <br>

Value shared by application object : 
<%=application.getAttribute("test")%>

</body>
</html>

setApplicationAttribute.jsp

<%@ page contentType = "text/html; charset=euc-kr" %>
<%
   // String name = request.getParameter("name");
   // String value = request.getParameter("value");
    
    String name = "test";
    String value = "1234";
    
    if (name != null && value != null) {
        application.setAttribute(name, value);
      //application.setAttribute("test", "1234");
    }
%>

<html>
<head><title>Set application attribute</title></head>
<body>
<%
    if (name != null && value != null) {
%>
Set attribute of application basic objects:
 <%= name %>  = <%= value %>
<%
    } else {
%>
Application basic objects attribute is not set.
<%
    }
%>
</body>
</html>

These examples are focused on the application scope. If you want to see other examples of the session scope, please refer to this post of mine: 2022.08.29 - [JSP] - JSP) Session

 

JSP) Session

A session is a concept used by servers and clients to maintain a connection state. Usually, if the login is successful, it is connected to the client as a session on the server side. At this time, m..

www.agilemeadow.com

 

'Java > JSP' 카테고리의 다른 글

HTML / JSP / Javascript) Implicit Objects 2 - Response Object and Page Link  (0) 2022.08.26
JSP) Implicit Objects 1 - Request Object  (0) 2022.08.26
JSP) Comment  (0) 2022.08.26
JSP) Importing / Classes  (0) 2022.08.25
JSP Tags  (0) 2022.08.25

+ Recent posts