ServletContext 인터페이스의 setAttribute, getAttribute, removeAttribute 메소드는 같은 웹 애플리케이션에 있는 웹 컴포넌트들끼리 데이터를 공유할 수 있도록 만드는 메소드입니다.
다음 예제를 각각 다른 컴퓨터에서 실행해도 결과는 같습니다.
사용 예제 구성은 다음과 같습니다.
StoreName.jsp
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<%
String name = request.getParameter("NAME");//값을 가져와서
application.setAttribute("NAME", name);//NAME이라는 TAG로 저장합니다.
%>
<!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>
저장 완료
</body>
</html>
ReadName.jsp
<%@ 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>
이름: <%= application.getAttribute("NAME") %>
</body>
</html>
DeleteName.jsp
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<% application.removeAttribute("NAME"); %>
<!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>
삭제되었습니다.
</body>
</html>
실행 화면
url뒤에 ?NAME=gakari를 붙여주는 것을 반드시 잊지맙시다.
ReadName.jsp로 가보면 제대로 이름이 나오네요.
DeleteName.jsp로 가보면 삭제가 되었다고 나오네요.
ReadName.jsp로 다시 가보면 Attribute가 삭제되었네요.
'프로그래밍 > JSP Servlet' 카테고리의 다른 글
JSP/Servlet - 익스프레션 언어 List 객체 사용 방법 (0) | 2016.01.10 |
---|---|
JSP/Servlet - 익스프레션 언어 initParam 내장 객체 사용 방법 (0) | 2016.01.10 |
JSP/Servlet - 익스프레션 언어 cookie 내장 객체 사용 방법 (0) | 2016.01.10 |
JSP/Servlet - 익스프레션 언어 (애트리뷰트 및 param, paramValues) (0) | 2016.01.06 |
JSP/Servlet - 웹애플리케이션 초기화 파라미터 (0) | 2016.01.05 |
JSP/Servlet - 서버 및 서블릿 환경 정보 가져오기(ServletContext) (0) | 2016.01.05 |
JSP/Servlet - jsp 페이지 초기화 파라미터 설정하기 (0) | 2016.01.04 |
JSP/Servlet - jspInit 메소드와 jspDestroy 메소드 활용 (0) | 2016.01.04 |