주요내용 : EL 내장객체, JSTL, Core Tag Library
@@@@@@@ 0813 실습
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>고객 검색 창</title>
</head>
<body>
<form method="post" action="listCustomer.jsp">
고객번호 : <input type="text" name="cust_id">
<input type="submit" value="조회하기">
<input type="button" value="고객추가" onClick="location.href='addEditCus.jsp?mode=add'">
</form>
</body>
</html>
=================
<%@ page language="java" contentType="text/html; charset=UTF-8"
import="java.util.*"
import="java.net.URLDecoder"
import="day0813.*"
pageEncoding="UTF-8"
%>
<!DOCTYPE html>
<html>
<head>
<style>
h1 {
text-align: center;
}
</style>
<meta charset="UTF-8">
<title>고객 정보 출력</title>
</head>
<body>
<h1>고객 정보 출력</h1>
<%
request.setCharacterEncoding("utf-8");
String _cust_id = request.getParameter("cust_id");
System.out.println(_cust_id);
CustomerBean customerVO = new CustomerBean();
customerVO.setCust_id(_cust_id);
CustomerDAO dao = new CustomerDAO();
List customerlist = dao.listCustomer(customerVO);
%>
<table border=1 width=800 align=center>
<tr align=center bgcolor="#FFFF66">
<td>고객번호</td>
<td>고객이름</td>
<td>고객주소</td>
<td>고객주</td>
<td>고객우편번호</td>
<td>고객국가</td>
<td>고객담당자</td>
<td>고객메일주소</td>
<td>수정</td>
</tr>
<%
for(int i=0; i < customerlist.size(); i++){
CustomerBean vo = (CustomerBean) customerlist.get(i);
String cust_id = vo.getCust_id();
String cust_name = vo.getCust_name();
String cust_address = vo.getCust_address();
String cust_state = vo.getCust_state();
String cust_zip = vo.getCust_zip();
String cust_country = vo.getCust_country();
String cust_contact = vo.getCust_contact();
String cust_email = vo.getCust_email();
%>
<tr align=center>
<td><%= cust_id %></td>
<td><%= cust_name %></td>
<td><%= cust_address %></td>
<td><%= cust_state %></td>
<td><%= cust_zip %></td>
<td><%= cust_country %></td>
<td><%= cust_contact %></td>
<td><%= cust_email %></td>
<td><a href="addEditCus.jsp?cust_id=<%= cust_id %>&cust_name=<%= cust_name %>%cust_address=<%= cust_address %>%cust_state=<%= cust_state %>%cust_country=<%= cust_country %>%cust_contact=<%= cust_contact %>%cust_email=<%= cust_email %>&mode=edit">수정</a></td>
</tr>
<%
}
%>
</table>
</body>
</html>
=========================
<%@ page language="java" contentType="text/html; charset=UTF-8"
import="java.util.*"
import="day0813.*"
pageEncoding="UTF-8"
%>
<!DOCTYPE html>
<%
request.setCharacterEncoding("utf-8");
String mode = request.getParameter("mode");
%>
<jsp:useBean id="m" class="day0813.CustomerBean" scope="request"/>
<jsp:setProperty property="*" name="m" />
<html>
<head>
<meta charset="UTF-8">
<title>고객 정보 수정 및 추가</title>
</head>
<body>
<form action="../customer" method="post" encType="UTF-8">
<%
if("edit".equals(mode)){
%>
<table>
<th>고객정보 수정</th>
<tr>
<td>고객번호</td>
<td><input type="text" name="cust_id" value=<jsp:getProperty name="m" property="cust_id" />></td>
</tr>
<tr>
<td>고객성명</td>
<td><input type="text" name="cust_name" value=<jsp:getProperty name="m" property="cust_name" />></td>
</tr>
<tr>
<td>고객주소</td>
<td><input type="text" name="cust_address" value=<jsp:getProperty name="m" property="cust_address" />></td>
</tr>
<tr>
<td>고객주</td>
<td><input type="text" name="cust_state" value=<jsp:getProperty name="m" property="cust_state" />></td>
</tr>
<tr>
<td>고객우편번호</td>
<td><input type="text" name="cust_zip" value=<jsp:getProperty name="m" property="cust_zip" />></td>
</tr>
<tr>
<td>고객국가</td>
<td><input type="text" name="cust_country" value=<jsp:getProperty name="m" property="cust_country" />></td>
</tr>
<tr>
<td>고객담당자</td>
<td><input type="text" name="cust_contact" value=<jsp:getProperty name="m" property="cust_contact" />></td>
</tr>
<tr>
<td>고객이메일</td>
<td><input type="text" name="cust_email" value=<jsp:getProperty name="m" property="cust_email" />></td>
</tr>
</table>
<input type='submit' name="submit" value='수정'>
<input type="hidden" name="mode" value="edit"/>
<% } %>
<%
if("add".equals(mode)){
%>
<table>
<th>고객정보 추가</th>
<tr>
<td>고객번호</td>
<td><input type="text" name="cust_id"></td>
</tr>
<tr>
<td>고객성명</td>
<td><input type="text" name="cust_name"></td>
</tr>
<tr>
<td>고객주소</td>
<td><input type="text" name="cust_address"></td>
</tr>
<tr>
<td>고객주</td>
<td><input type="text" name="cust_state"></td>
</tr>
<tr>
<td>고객우편번호</td>
<td><input type="text" name="cust_zip"></td>
</tr>
<tr>
<td>고객국가</td>
<td><input type="text" name="cust_country"></td>
</tr>
<tr>
<td>고객담당자</td>
<td><input type="text" name="cust_contact"></td>
</tr>
<tr>
<td>고객이메일</td>
<td><input type="text" name="cust_email"></td>
</tr>
</table>
<input type='submit' name="submit" value='추가'>
<input type="hidden" name="mode" value="add"/>
<% } %>
</form>
</body>
</html>
=======================
package day0813;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class CustomerServlet
*/
@WebServlet("/customer")
public class CustomerServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doHandle(request, response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doHandle(request, response);
}
protected void doHandle(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
String p_cust_id = request.getParameter("cust_id");
String p_mode = request.getParameter("mode");
CustomerDAO dao=new CustomerDAO();
List customerList;
String cust_id = request.getParameter("cust_id");
String cust_name = request.getParameter("cust_name");
String cust_address = request.getParameter("cust_address");
String cust_state = request.getParameter("cust_state");
String cust_zip = request.getParameter("cust_zip");
String cust_country = request.getParameter("cust_country");
String cust_contact = request.getParameter("cust_contact");
String cust_email = request.getParameter("cust_email");
if(p_mode != null && p_mode.equals("edit")) {
dao.editCustomer(cust_id, cust_name, cust_address, cust_state, cust_zip, cust_country, cust_contact, cust_email);
}else if(p_mode != null && p_mode.equals("add")) {
dao.addCustomer(cust_id, cust_name, cust_address, cust_state, cust_zip, cust_country, cust_contact, cust_email);
}
RequestDispatcher dispatch = request.getRequestDispatcher("day0813/listCustomer.jsp");
dispatch.forward(request, response);
}
}
========================
package day0813;
import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;
public class CustomerDAO {
private Connection con;
private PreparedStatement pstmt; // 실무에선 PreparedStatement를 더 많이씀.
private DataSource dataFactory;
public CustomerDAO()
{
try
{
Context ctx = new InitialContext();
Context envContext = (Context)ctx.lookup("java:/comp/env");
dataFactory = (DataSource)envContext.lookup("jdbc/oracle");
} catch (Exception e)
{
e.printStackTrace();
}
}
public List listCustomer(CustomerBean customerBean)
{
List customerlist = new ArrayList();
String _cust_id = customerBean.getCust_id();
System.out.println(_cust_id);
try
{
con = dataFactory.getConnection();
String query = "select cust_id, cust_name, cust_address, cust_state, cust_zip, cust_country, cust_contact, cust_email ";
query += "from customers ";
query += "where cust_id like '%" + _cust_id + "%'";
System.out.println("preparedStatement : " + query);
pstmt = con.prepareStatement(query); // preparedStatement 메소드에 sql문을 전달해 prepareStatement객체를 생성.
ResultSet rs = pstmt.executeQuery(); // sql문으로 회원 정보를 조회
while(rs.next())
{
// 조회한 레코드의 각 컬럼 값을 받아옴.
// -- 고객번호로 조회
// -- 고객번호, 고객이름, 고객주소, 고객주, 고개우편번호, 고객국가, 고객담당자, 고객메일주소, 수정
// select cust_id, cust_name, cust_address, cust_state, cust_zip, cust_country, cust_contact, cust_email
String cust_id = rs.getString("cust_id");
String cust_name = rs.getString("cust_name");
String cust_address = rs.getString("cust_address");
String cust_state = rs.getString("cust_state");
String cust_zip = rs.getString("cust_zip");
String cust_country = rs.getString("cust_country");
String cust_contact = rs.getString("cust_contact");
String cust_email = rs.getString("cust_email");
// 각 컬럼 값을 다시 MemberVO 객체의 속성에 설정.
CustomerBean vo = new CustomerBean();
vo.setCust_id(cust_id);
vo.setCust_name(cust_name);
vo.setCust_address(cust_address);
vo.setCust_state(cust_state);
vo.setCust_zip(cust_zip);
vo.setCust_country(cust_country);
vo.setCust_contact(cust_contact);
vo.setCust_email(cust_email);
customerlist.add(vo); // 설정된 MemberVO 객체를 다시 ArrayList에 저장.
}
rs.close();
pstmt.close();
con.close();
} catch (Exception e)
{
e.printStackTrace();
}
return customerlist; // 조회한 레코드의 개수만큼 MemberVO객체를 저장한 ArrayList를 반환.
}
public void editCustomer(String cust_id, String cust_name, String cust_address, String cust_state, String cust_zip, String cust_country, String cust_contact, String cust_email)
{
try
{
// 수정창
// 고객번호, 고객이름, 고객주소, 고객주, 고개우편번호, 고객국가, 고객담당자, 고객메일주소, 수정
// cust_id, cust_name, cust_address, cust_state, cust_zip, cust_country, cust_contact, cust_email
Connection con = dataFactory.getConnection();
System.out.println("1");
String query = "UPDATE Customers";
query += " SET cust_address = ?";
query += ", cust_state = ?";
query += ", cust_zip = ?";
query += ", cust_country = ?";
query += ", cust_contact = ?";
query += ", cust_email = ?";
query += " WHERE cust_id = '" + cust_id + "'";
query += " AND cust_name ='" + cust_name + "'";
System.out.println("prepareStatement: " + query);
pstmt = con.prepareStatement(query);
pstmt.setString(1, cust_address);
pstmt.setString(2, cust_state);
pstmt.setString(3, cust_zip);
pstmt.setString(4, cust_country);
pstmt.setString(5, cust_contact);
pstmt.setString(6, cust_email);
pstmt.executeUpdate();
pstmt.close();
System.out.println("수정실행로그확인");
} catch (Exception e)
{
e.printStackTrace();
}
}
public void addCustomer(String cust_id, String cust_name, String cust_address, String cust_state, String cust_zip, String cust_country, String cust_contact, String cust_email) // 고객추가 메소드
{
try
{
// 고객번호, 고객이름, 고객주소, 고객주, 고개우편번호, 고객국가, 고객담당자, 고객메일주소, 수정
// cust_id, cust_name, cust_address, cust_state, cust_zip, cust_country, cust_contact, cust_email
Connection con = dataFactory.getConnection();
// String cust_id = customerBean.getCust_id();
// String cust_name = customerBean.getCust_name();
// String cust_address = customerBean.getCust_address();
// String cust_state = customerBean.getCust_state();
// String cust_zip = customerBean.getCust_zip();
// String cust_country = customerBean.getCust_country();
// String cust_contact = customerBean.getCust_contact();
// String cust_email = customerBean.getCust_email();
String query = "insert into customers";
query += " (cust_id,cust_name,cust_address,cust_state,cust_zip,cust_country,cust_contact,cust_email)";
query += " values(?,?,?,?,?,?,?,?)"; // 순서대로 값을 assign 해야함.
System.out.println("prepareStatement: " + query);
pstmt = con.prepareStatement(query);
pstmt.setString(1, cust_id);
pstmt.setString(2, cust_name);
pstmt.setString(3, cust_address);
pstmt.setString(4, cust_state);
pstmt.setString(5, cust_zip);
pstmt.setString(6, cust_country);
pstmt.setString(7, cust_contact);
pstmt.setString(8, cust_email);
pstmt.executeUpdate();
pstmt.close();
System.out.println("추가실행로그확인");
} catch (Exception e)
{
e.printStackTrace();
}
}
}
=========================
package day0813;
public class CustomerBean {
// cust_id, cust_name, cust_address, cust_state, cust_zip, cust_country, cust_contact, cust_email
private String cust_id;
private String cust_name;
private String cust_address;
private String cust_state;
private String cust_zip;
private String cust_country;
private String cust_contact;
private String cust_email;
public CustomerBean(){System.out.println("CustomerBean 생성자 호출");}
public String getCust_id() {
return cust_id;
}
public void setCust_id(String cust_id) {
this.cust_id = cust_id;
}
public String getCust_name() {
return cust_name;
}
public void setCust_name(String cust_name) {
this.cust_name = cust_name;
}
public String getCust_address() {
return cust_address;
}
public void setCust_address(String cust_address) {
this.cust_address = cust_address;
}
public String getCust_state() {
return cust_state;
}
public void setCust_state(String cust_state) {
this.cust_state = cust_state;
}
public String getCust_zip() {
return cust_zip;
}
public void setCust_zip(String cust_zip) {
this.cust_zip = cust_zip;
}
public String getCust_country() {
return cust_country;
}
public void setCust_country(String cust_country) {
this.cust_country = cust_country;
}
public String getCust_contact() {
return cust_contact;
}
public void setCust_contact(String cust_contact) {
this.cust_contact = cust_contact;
}
public String getCust_email() {
return cust_email;
}
public void setCust_email(String cust_email) {
this.cust_email = cust_email;
}
}
===========================
@@@@@@@ 0813 진도
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="day0813.Thermometer" %>
<%
day0813.Thermometer thermometer = new day0813.Thermometer();
request.setAttribute("t", thermometer);
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>온도 변환 예제</title>
</head>
<body>
${t.setCelsius('서울', 27.3)}
서울 온도 : 섭씨 ${t.getCelsius('서울')}도 / 화씨{t.getFahrenheit('서울')}
<br/>
정보 : ${t.info}
</body>
</html>
=============
package day0813;
import java.util.HashMap;
import java.util.Map;
public class Thermometer {
private Map<String, Double> locationCelsiusMap = new HashMap<String, Double>();
public void setCelsius(String location, Double value) {
locationCelsiusMap.put(location, value);
}
public Double getCelsius(String location) {
return locationCelsiusMap.get(location);
}
public Double getFahrenheit(String location) {
Double celsius = getCelsius(location);
if(celsius== null) {
return null;
}
return celsius.doubleValue() * 1.8 + 32.0 ;
}
public String getInfo() {
return "온도계 변환기 1.1";
}
}
================
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>회원가입 창</title>
</head>
<body>
<form action="member6.jsp" method="post"> // action에 member1~6.jsp 각각 수정
<h1 style="text-align:center">회원 가입창</h1>
<table align="center">
<tr>
<td width="200">
<p align="right">아이디
</td>
<td width="400"><input type="text" name="id"></td>
</tr>
<tr>
<td width="200">
<p align="right">비밀번호
</td>
<td width="400"><input type="password" name="pwd"></td>
</tr>
<tr>
<td width="200">
<p align="right">이름
</td>
<td width="400"><input type="text" name="name"></td>
</tr>
<tr>
<td width="200">
<p align="right">이메일
</td>
<td width="400"><input type="text" name="email"></td>
</tr>
<tr>
<td width="200">
<p> </p>
</td>
<td width="400">
<input type="submit" value="가입하기">
<input type="reset" value="다시입력">
</td>
</tr>
</table>
</form>
</body>
</html>
=======================
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"
isELIgnored="false"
%>
<%
request.setCharacterEncoding("UTF-8");
String id = request.getParameter("id");
String pwd = request.getParameter("pwd");
String name = request.getParameter("name");
String email = request.getParameter("email");
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>회원 정보출력창</title>
</head>
<body>
<table border="1" align="center">
<tr align=center bgcolor="#99ccff">
<td width="20%"><b>아이디</b></td>
<td width="20%"><b>비밀번호</b></td>
<td width="20%"><b>이름</b></td>
<td width="20%"><b>이메일</b></td>
</tr>
<tr align=center>
<td><%= id %></td>
<td><%= pwd %></td>
<td><%= name %></td>
<td><%= email %></td>
</tr>
<tr align=center>
// param 객체를 이용해 getParameter 메소드를 이용하지 않고 바로 회원정보를 출력.
<td>${param.id}</td>
<td>${param.pwd}</td>
<td>${param.name}</td>
<td>${param.email}</td>
</tr>
</table>
</body>
</html>
=====================
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
request.setCharacterEncoding("utf-8");
request.setAttribute("address", "서울시 강남구");
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>forward</title>
</head>
<body>
<jsp:forward page="member2.jsp"></jsp:forward>
</body>
</html>
==========================
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"
isELIgnored="false"
%>
<%
request.setCharacterEncoding("UTF-8");
String id = request.getParameter("id");
String pwd = request.getParameter("pwd");
String name = request.getParameter("name");
String email = request.getParameter("email");
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>회원 정보출력창</title>
</head>
<body>
<table border="1" align="center">
<tr align=center bgcolor="#99ccff">
<td width="20%"><b>아이디</b></td>
<td width="20%"><b>비밀번호</b></td>
<td width="20%"><b>이름</b></td>
<td width="20%"><b>이메일</b></td>
<td width="20%"><b>주소</b></td>
</tr>
<tr align=center>
<td>${param.id}</td>
<td>${param.pwd}</td>
<td>${param.name}</td>
<td>${param.email}</td>
<td>${requestScope.address}</td> // requestScope를 이용해 바인딩된 주소 정보를 출력.
</tr>
</table>
</body>
</html>
======================
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"
isELIgnored="false"
%>
<%
request.setCharacterEncoding("UTF-8");
%>
<jsp:useBean id="m" class="sec01.ex01.MemberBean"/> // 회원 정보를 저장할 빈을 생성
<jsp:setProperty name="m" property="*"/> // 전송된 회원 정보를 빈의 속성에 설정.
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>회원 정보출력창</title>
</head>
<body>
<table border="1" align="center">
<tr align=center bgcolor="#99ccff">
<td width="20%"><b>아이디</b></td>
<td width="20%"><b>비밀번호</b></td>
<td width="20%"><b>이름</b></td>
<td width="20%"><b>이메일</b></td>
</tr>
</tr>
<tr align=center>
<td><%= m.getId() %></td>
<td><%= m.getPwd() %></td>
<td><%= m.getName() %></td>
<td><%= m.getEmail() %></td>
</tr>
<tr align=center>
// ${빈이름.속성이름}
<td>${m.id}</td>
<td>${m.pwd}</td>
<td>${m.name}</td>
<td>${m.email}</td>
</tr>
</table>
</body>
</html>
=======================
<%@ page language="java" contentType="text/html; charset=UTF-8"
import="java.util.*, sec01.ex01.*"
pageEncoding="UTF-8"
isELIgnored="false"
%>
<%
request.setCharacterEncoding("UTF-8");
%>
<jsp:useBean id="m1" class="sec01.ex01.MemberBean"/>
<jsp:setProperty name="m1" property="*"/>
<jsp:useBean id="membersList" class="java.util.ArrayList" />
<jsp:useBean id="membersMap" class="java.util.HashMap" />
<%
//hashMap에 key, value 쌍으로 회원정보를 저장.
membersMap.put("id", "park2");
membersMap.put("pwd", "4321");
membersMap.put("name", "박지성");
membersMap.put("email", "park2@test.com");
MemberBean m2 = new MemberBean("son", "1234", "손흥민", "son@test.com");
membersList.add(m1);
membersList.add(m2);
membersMap.put("membersList", membersList);
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>회원 정보출력창</title>
</head>
<body>
<table border="1" align="center">
<tr align=center bgcolor="#99ccff">
<td width="20%"><b>아이디</b></td>
<td width="20%"><b>비밀번호</b></td>
<td width="20%"><b>이름</b></td>
<td width="20%"><b>이메일</b></td>
</tr>
<tr align=center>
// key를 사용하여 value를 가져옴.
<td>${membersMap.id}</td>
<td>${membersMap.pwd}</td>
<td>${membersMap.name}</td>
<td>${membersMap.email}</td>
</tr>
<tr align=center>
// arrayList에 접근한 후 다시 각각의 속성에 접근하여 회원정보 출력.
<td>${membersMap.membersList[0].id}</td>
<td>${membersMap.membersList[0].pwd}</td>
<td>${membersMap.membersList[0].name}</td>
<td>${membersMap.membersList[0].email}</td>
</tr>
<tr align=center>
<td>${membersMap.membersList[1].id}</td>
<td>${membersMap.membersList[1].pwd}</td>
<td>${membersMap.membersList[1].name}</td>
<td>${membersMap.membersList[1].email}</td>
</tr>
</table>
</body>
</html>
===========================
package sec01.ex02;
import java.sql.Date;
public class MemberBean {
private String id;
private String pwd;
private String name;
private String email;
private Date joinDate;
private Address addr;
public MemberBean()
{
System.out.println("MeberVO 생성자 호출");
}
public MemberBean(String id, String pwd, String name, String email) {
this.id = id;
this.pwd = pwd;
this.name = name;
this.email = email;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Date getJoinDate() {
return joinDate;
}
public void setJoinDate(Date joinDate) {
this.joinDate = joinDate;
}
public Address getAddr() {
return addr;
}
public void setAddr(Address addr) {
this.addr = addr;
}
}
======================
package sec01.ex02;
public class Address {
private String city;
private String zipcode;
public Address() {
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getZipcode() {
return zipcode;
}
public void setZipcode(String zipcode) {
this.zipcode = zipcode;
}
}
======================
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"
isELIgnored="false"
%>
<%
request.setCharacterEncoding("UTF-8");
%>
<jsp:useBean id="m" class="sec01.ex02.MemberBean"/>
<jsp:setProperty name="m" property="*"/>
<jsp:useBean id="addr" class="sec01.ex02.Address" />
<jsp:setProperty name="addr" property="city" value="서울"/>
<jsp:setProperty name="addr" property="zipcode" value="07654"/>
<%
m.setAddr(addr);
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>회원 정보출력창</title>
</head>
<body>
<table border="1" align="center">
<tr align=center bgcolor="#99ccff">
<td width="7%"><b>아이디</b></td>
<td width="7%"><b>비밀번호</b></td>
<td width="5%"><b>이름</b></td>
<td width="5%"><b>이메일</b></td>
<td width="5%"><b>도시</b></td>
<td width="5%"><b>우편번호</b></td>
</tr>
<tr align=center>
<td>${m.id}</td>
<td>${m.pwd}</td>
<td>${m.name}</td>
<td>${m.email}</td>
// getter를 두번호출하는방법 -> 불편
<td><%=m.getAddr().getCity() %></td>
<td><%=m.getAddr().getZipcode() %></td>
</tr>
<tr align=center>
// ${부모빈이름.자식속성이름.속성이름} -> 빈이름만 이용해 주소정보 표시
<td>${m.id}</td>
<td>${m.pwd}</td>
<td>${m.name}</td>
<td>${m.email}</td>
<td>${m.addr.city }</td>
<td>${m.addr.zipcode }</td>
</tr>
</table>
</body>
</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 cookieName = "id";
Cookie cookie = new Cookie(cookieName, "hongkd");
cookie.setMaxAge(60*2);
cookie.setValue("kimkd");
response.addCookie(cookie);
%><p>
<%=cookieName %>쿠키가 생성되었습니다.<br>
<input type="button" value="쿠키의 내용확인" onClick="location.href='useCookie.jsp'">
</p>
</body>
</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>
<%
Cookie[] cookies = request.getCookies();
if(cookies!=null){
for(int i=0; i<cookies.length; ++i){
if(cookies[i].getName().equals("id")){ // id란 key 로 저장된 Cookie value를 찾는다.
%>
쿠키의 이름은 "<%=cookies[i].getName() %>" 이고
쿠키의 값은 "<%=cookies[i].getValue() %>" 입니다.
<%
}
}
}
%>
<br>
id에 대한 쿠키의 값은 ${cookie.id.value} 입니다.
</body>
</html>
=========================
package day0813;
import java.text.DecimalFormat;
public class FormatUtil {
public static String number(long number, String pattern) {
DecimalFormat format = new DecimalFormat(pattern);
return format.format(number);
}
}
==========================
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="day0813.FormatUtil" %>
<%
request.setAttribute("price", 12345L);
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>EL 함수 호출</title>
</head>
<body>
가격은 <b>${FormatUtil.number(price, '#,##0')}</b>원입니다.
</body>
</html>
=====================================
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"
isELIgnored="false"
%>
// core 태그 라이브러리 사용위해 반드시 선언해야함.
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
request.setCharacterEncoding("UTF-8");
%>
// <c:set var="변수명" value="변수값" [scope="scope 속성 중 하나"]/>
<c:set var="id" value="hong" scope="page"/>
<c:set var="pwd" value="1234" scope="page"/>
<c:set var="name" value="${'홍길동'}" scope="page"/>
<c:set var="age" value="${22}" scope="page"/>
<c:set var="height" value="${177}" scope="page"/>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>회원 정보출력창</title>
</head>
<body>
<table border="1" align="center">
<tr align=center bgcolor="#99ccff">
<td width="7%"><b>아이디</b></td>
<td width="7%"><b>비밀번호</b></td>
<td width="7%"><b>이름</b></td>
<td width="7%"><b>나이</b></td>
<td width="7%"><b>키</b></td>
</tr>
<tr align=center>
<td>${id}</td>
<td>${pwd}</td>
<td>${name}</td>
<td>${age}</td>
<td>${height}</td>
</tr>
</table>
</body>
</html>
======================
<%@ page language="java" contentType="text/html; charset=UTF-8"
import = "java.util.*, sec01.ex02.MemberBean"
pageEncoding="UTF-8"
isELIgnored="false"
%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
request.setCharacterEncoding("UTF-8");
%>
<jsp:useBean id="membersList" class="java.util.ArrayList"/>
<jsp:useBean id="membersMap" class="java.util.HashMap"/>
<%
membersMap.put("id", "park2");
membersMap.put("pwd", "4321");
membersMap.put("name", "박지성");
membersMap.put("email", "park2@test.com");
MemberBean m1 = new MemberBean("son", "1234", "손흥민", "son@test.com");
MemberBean m2 = new MemberBean("ki", "2234", "기성용", "ki@test.com");
membersList.add(m1);
membersList.add(m2);
membersMap.put("membersList", membersList);
%>
<c:set var="membersList" value="${membersMap.membersList}" />
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>회원 정보출력창</title>
</head>
<body>
<table border="1" align="center">
<tr align=center bgcolor="#99ccff">
<td width="7%"><b>아이디</b></td>
<td width="7%"><b>비밀번호</b></td>
<td width="7%"><b>이름</b></td>
<td width="7%"><b>이메일</b></td>
</tr>
<tr align=center>
<td>${membersMap.id}</td>
<td>${membersMap.pwd}</td>
<td>${membersMap.name}</td>
<td>${membersMap.email}</td>
</tr>
</tr>
<tr align=center>
<td>${membersList[0].id}</td>
<td>${membersList[0].pwd}</td>
<td>${membersList[0].name}</td>
<td>${membersList[0].email}</td>
</tr>
</tr>
<tr align=center>
<td>${membersList[1].id}</td>
<td>${membersList[1].pwd}</td>
<td>${membersList[1].name}</td>
<td>${membersList[1].email}</td>
</tr>
</table>
</body>
</html>