프로그래밍/JSP Servlet

JSP/Servlet - 익스프레션 언어 Hash Map 객체를 출력하기

가카리 2016. 1. 11. 21:42
반응형

 

JSP/Servlet - 익스프레션 언어 Hash Map 객체를 출력하기

 

java.util.Map 객체는 자바의 표준 라이브러리에 있는 인터페이스 이름인데 이 인터페이스는 <이름, 값> 쌍으로 만들어서 관리합니다.

 

자바 프로그램에서 java.util.Map 객체의 항목을 가져오는 익스프레션언어는 다음과 같습니다.

 

    ${MAP객체이름["데이터이름"]}    또는    ${MAP객체이름.데이터이름}

 

 

Address.jsp

 

<%@ page language="java" contentType="text/html; charset=EUC-KR"

pageEncoding="EUC-KR"%>

<%@page import="java.util.*"%>

<%

    HashMap<String, String> map = new HashMap<String, String>();//해쉬맵 생성

    map.put("NAME", "gakari");//값을 넣어줌

    map.put("GENDER", "man");

    map.put("AGE", "22");

    request.setAttribute("ADDRESS", map);//애트리뷰트에 등록

    RequestDispatcher dispatcher = request.getRequestDispatcher("AddressRead.jsp");// 페이지로 넘어감

    dispatcher.forward(request, response);

%>

<!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>

 

</body>

</html>

 

AddressRead.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>

    이름 ${ADDRESS["NAME"] } <BR>

    성별 ${ADDRESS["GENDER"] } <BR>

    나이 ${ADDRESS["AGE"] } <BR>

</body>

</html>

 

 

실행 화면

다음과 같이 HashMap의 값을 가져올 수 있습니다.

반응형