2015년 9월 29일 화요일

JSP에서 initParam, cookie, getInitParameter 사용예제

JSP에서 initParam, cookie, getInitParameter 사용예제


1. /jsp/test3.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>
<%
String key = "age";
String value = "22";

Cookie cookie = new Cookie(key, value);
response.addCookie(cookie);
%>
age : ${cookie["age"].value} <br>
adminEmail_1 : ${initParam.adminEmail_1} <br>  <!-- web.xml의 context-param 값을 읽음 -->
adminEmail_2 : <%= getInitParameter("adminEmail_2") %> <br> <!-- 서블릿 설정의 init-param 값을 읽음 -->
</body>
</html>


2. web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
    <context-param>
        <param-name>adminEmail_1</param-name>
        <param-value>test@test.co.kr</param-value>
    </context-param>

<servlet>
<servlet-name>GetInitParam</servlet-name>
<jsp-file>/jsp/test3.jsp</jsp-file>
<init-param>
<param-name>adminEmail_2</param-name>
<param-value>test@test.co.kr</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>GetInitParam</servlet-name>
<url-pattern>/test3</url-pattern>
</servlet-mapping>

</web-app>


3. 결과

age : 22 
adminEmail_1 : test@test.co.kr 
adminEmail_2 : test@test.co.kr 



댓글 없음:

댓글 쓰기