The last post of mine was about the basics of the scopes and focused on the application and the session among four scopes: Page, Request, Session, and Application.

2022.08.26 - [JSP] - JSP) Scopes - Basics / Application

 

JSP) Scopes - Basics / Application

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 Sh..

www.agilemeadow.com

 

In this post, we will cover the page and the request scopes. 

Page and Request

<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<title>Insert title here</title>
</head>
<body>
<%
pageContext.setAttribute("pageScope", "pageValue");
request.setAttribute("requestScope", "requestValue");
%>

pageValue = <%=pageContext.getAttribute("pageScope") %><br>
requestValue = <%=request.getAttribute("requestScope") %>
</body>
</html>

With a forward action tag

With the forward action tag, you will not see anything on a browser, but it will still forward the data to the designated page.

<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<title>Insert title here</title>
</head>
<body>
<%
pageContext.setAttribute("pageScope", "pageValue");
request.setAttribute("requestScope", "requestValue");
%>
<jsp:forward page="requestTest5Result.jsp"></jsp:forward>
</body>
</html>

Result page

<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<title>Insert title here</title>
</head>
<body>
pageValue = <%=pageContext.getAttribute("pageScope") %><br>
requestValue = <%=request.getAttribute("requestScope") %>
</body>
</html>

You cannot use the page scope anymore so it will turn out null.

 

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

JSP) Handling Exceptions  (0) 2022.08.30
JSP) Action tags - forward  (0) 2022.08.30
JSP) Session  (0) 2022.08.29
JSP) Cookies  (0) 2022.08.28
JSP) Encoding Korean 한글 인코딩  (0) 2022.08.27

+ Recent posts