Notice
Recent Posts
Recent Comments
Link
«   2025/08   »
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31
Archives
Today
Total
관리 메뉴

개발기록

11 JSP 내장객체 본문

카테고리 없음

11 JSP 내장객체

옥수수수염챠 2020. 3. 19. 21:50

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 변동시 서버 재기동 필요

 

또한 모든 애플리케이션 내의 속성값을 읽어오고, 저장또한 할 수 있다.

출처 : 실전 JSP (renew ver.) - 신입 프로그래머를 위한 강좌 11강

 

 

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로 구동이 된다.