프로그래머/JAVA

[JSP] 내장 객체(application, config)

얼짱가면 2013. 3. 26. 10:15

application, javax.servlet.ServletContext

현재 작업 중인 JSP 페이지가 위치하고 있는 웹 어플리케이션(web context)의 정보를 담고 있는 객체로 주로 공통적으로 적용되어져야 할 데이터를 담고 있는 객체

 

config, javax.servlet.ServletConfig

application이 공통적으로 사용될 데이터를 담고 있는 객체라면, config는 해당 페이지 한 개의 서브클래스(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>config getInitParameter() test JSP</title>

</head>

<body>

        init parameter : <%=config.getInitParameter("tel") %> <br/>

        context parameter : <%=application.getInitParameter("tell") %>

</body>

</html>

 

 

 

[Web.xml]

<context-param>

        <param-name>tell</param-name>

        <param-value>1234-123</param-value>

</context-param>

 

<servlet>

<servlet-name>configTest</servlet-name>

<jsp-file>/configTest.jsp</jsp-file>

<init-param>

        <param-name>tel</param-name>

        <param-value>123-1234</param-value>

</init-param>

</servlet>

 

<servlet-mapping>

<servlet-name>configTest</servlet-name>

<url-pattern>/configTest.jsp</url-pattern>

</servlet-mapping>