JSP - chapter04(2)

zi_soom·2021년 7월 20일
0

JSP

목록 보기
7/12

🎃application 기본 객체

  • 웹 어플리케이션과 관련된 기본 객체
  • 특정 웹 어플리케이션에 포함된 모든 JSP 페이지는 하나의 application 기본 객체를 공유함
  • 초기 설정 정보를 읽어올 수 있음(서버 정보 등)
  • 서로 다른 JSP 페이지와 서로 다른 웹브라우저(크롬, IE 등)에서 동일한 application 기본 객체의 속성을 사용하는 이유는 웹 어플리케이션 내에 있는 모든 JSP가 한 개의 application기본 객체를 공유하기 때문이다.

🎃application 초기화 파라미터 읽어오기

<%@page import="java.util.Enumeration"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<title>초기화 파라미터 읽어오기(application 객체)</title>
</head>
<body>
	<h2>초기화 파라미터 목록</h2>
    <%
    //웹 어플리케이션 초기화 파라미터의 이름 목록을 리턴 ( 리턴 타입 : Enumeration<String> )
	Enumeration<String> initParamEnum = application.getInitParameterNames();
	while(initParamEnum.hasMoreElements()) { //있으면 실행
		String initParamName = initParamEnum.nextElement();
		out.print(initParamName + " = " 
					+ application.getInitParameter(initParamName) + "<br>");
	}
    %>
	서버 정보 : <%=application.getServerInfo() %><br>
	서블릿 규약 메이저 버전 : <%=application.getMajorVersion() %><br>
	서블릿 규약 마이너 버전 : <%=application.getMinorVersion() %><br>
   	<%
		//[톰캣설치폴더]\logs폴더
		application.log("(홍길동)로그 메시지 기록");
	%> 
  • 초기화 파라미터 목록

🎃application 기본 객체의 속성 보기

  • 속성 추가 폼
<%@page import="java.util.Enumeration"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<title>application 기본 객체의 속성 보기</title>
</head>
<body>
<form name="frm" id="frm" method="get" action="setApplicationAttribute.jsp">
	application 객체의 <br>
	setAttribute 속성의 name : <input type="text" name="name" value=""><br>
	setAttribute 속성의 value : <input type="text" name="value" value=""><br>
	<input type="submit" value="attribute추가">
</form>
<br>
<%
	//속성의 이름 목록을 구함 (리턴 타입 : java.util.Enumeration)
	Enumeration<String> attrEnum = application.getAttributeNames();
	while(attrEnum.hasMoreElements()) {
		//a001
		String name = attrEnum.nextElement();
		Object value = application.getAttribute(name);
		out.print("application 속성 : " + name + " = " + value + "<br>");
	}

%>
</body>
</html>
  • name : a001, value = 김은대 속성 추가
  • 속성 추가 결과
  • name : b001, value = 이쁜이 속성 추가
  • 속성 추가 결과
  • 속성 지정
<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<%
	request.setCharacterEncoding("UTF-8");
	//jsp?name=b001&value=이쁜이
	String name = request.getParameter("name"); //b001
	String value = request.getParameter("value"); //이쁜이
	//첫번째 name : a001, value : 김은대	
	//두번째 name : b001, value : 이쁜이	
	application.setAttribute(name, value);
%>
<!DOCTYPE html>
<html>
<head>
<title>application 속성 지정</title>
</head>
<body>

application 기본 객체의 속성 설정 :<br/>
<%=name %> = <%=value %>
<br><br>
<input type="button" id="btn" value="application의 attribute 보기" onclick="javascript:location.href='viewApplicationAttribute.jsp';">
</body>
</html>
  • 김은대 속성 지정 성공
  • 이쁜이 속성 지정 성공

0개의 댓글