개발기록
11 JSP 내장객체 본문
11-1 config 객체
웹 환경설정이라고 할 수 있는 web.xml에 어떠한 데이터를 명시해놓고 그것을 getInitParameter()라는 메서드를 이용하여
JSP에서 데이터를 공유하는 환경이다.
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>lec08Pjt001</display-name>
<servlet>
<servlet-name>servletEx</servlet-name>
<jsp-file>/jspEx.jsp</jsp-file>
<init-param>
<param-name>adminId</param-name>
<param-value>admin</param-value>
</init-param>
<init-param>
<param-name>adminPw</param-name>
<param-value>1234</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>servletEx</servlet-name>
<url-pattern>/jspEx.jsp</url-pattern>
</servlet-mapping>
</web-app>
서블릿에서 사용할 수 있는 데이터를 지정한다.
JSP파일
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%!
String adminId;
String adminPw;
%>
<%
adminId = config.getInitParameter("adminId");
adminPw = config.getInitParameter("adminPw");
%>
<p>adminId : <%=adminId %></p>
<p>adminId : <%=adminPw %></p>
</body>
</body>
</html>
서버 구동 후 실행결과 :
즉 이 서블릿 에서만 adminId와 adminPW를 초기화 하는 것이다.
11-2 application 객체
application 객체는 위 config객체와 다르게 하나의 서블릿이 아니라 전체 서블릿의 데이터를 공유한다.
web.xml파일
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>lec08Pjt001</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<context-param>
<param-name>imgDir</param-name>
<param-value>/upload/img</param-value>
</context-param>
<context-param>
<param-name>testServerIP</param-name>
<param-value>127.0.0.1</param-value>
</context-param>
<context-param>
<param-name>realServerIP</param-name>
<param-value>68.0.30.1</param-value>
</context-param>
<servlet>
<servlet-name>servletEx</servlet-name>
<jsp-file>/jspEx.jsp</jsp-file>
<init-param>
<param-name>adminId</param-name>
<param-value>admin</param-value>
</init-param>
<init-param>
<param-name>adminPw</param-name>
<param-value>1234</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>servletEx</servlet-name>
<url-pattern>/jspEx.jsp</url-pattern>
</servlet-mapping>
</web-app>
jsp파일
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%!
String adminId;
String adminPw;
String imgDir;
String testServerIP;
%>
<%
adminId = config.getInitParameter("adminId");
adminPw = config.getInitParameter("adminPw");
%>
<p>adminId : <%=adminId %></p>
<p>adminPw : <%=adminPw %></p>
<%
imgDir = application.getInitParameter("imgDir");
testServerIP = application.getInitParameter("testServerIP");
%>
<p>imgDir : <%=imgDir %></p>
<p>testServerIP : <%=testServerIP %></p>
</body>
</body>
</html>
실행 결과 :
** 주의 : web.xml 변동시 서버 재기동 필요
또한 모든 애플리케이션 내의 속성값을 읽어오고, 저장또한 할 수 있다.
11-3 out 객체
out.print를 제공하여 HTML코딩하듯이 작성할 수 있다.
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%!
String adminId;
String adminPw;
String imgDir;
String testServerIP;
String str;
%>
<!-- out 객체 -->
<%
out.print("<h1>Hello JAVA World</h1>");
out.print("<h1>Hello JSP World</h1>");
out.print("<h1>Hello Servlet World</h1>");
%>
</body>
</body>
</html>
11-4 exception 객체
에러를 감지하는 객체이다
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page errorPage = "errorPage.jsp" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%!
String adminId;
String adminPw;
String imgDir;
String testServerIP;
String str;
%>
<!-- out 객체 -->
<%
out.print("<h1>Hello JAVA World</h1>");
out.print("<h1>Hello JSP World</h1>");
out.print("<h1>Hello Servlet World</h1>");
%>
<!-- error 객체 -->
<%
out.print(str.toString());
%>
</body>
</body>
</html>
만일 위의 코드에서 str변수를 선언하지 않고 구동하게 되면,
str은 선언하지 않은 변수이기 때문에 바로 errorPage인 errorPage.jsp로 구동이 된다.