달력

42024  이전 다음

  • 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
검색엔진 만들기 작성자 : hjnam93, 등록일 : 2004-08-22 23:28:30

 

지금 쓰는 내용은 "Modern Information Retrieval" (Ricardo Baeza-Yates, Berthier Ribeiro-Neto공저)과 Information Retrieval Data Structures & Algorithms(William B. Frakes, Ricardo Baeza-Yates공저)의 내용과 내가 생각하는 정보검색에 대한 사항들을 정리한 것입니다. 자유롭게 참조하세요.

 ..

내용 전체보기

'JAVA > JSP_Servlet' 카테고리의 다른 글

간단한 trackback ping 보내는 JSP ..  (0) 2005.02.04
[펌] Trackback CGI 설계  (0) 2005.02.03
[javamail] 제목 깨질때...  (0) 2005.01.18
[펌] 메일 헤더 구성 요소  (0) 2005.01.17
[펌] [MAIL] MIME 관련  (0) 2005.01.17
Posted by tornado
|

한글 제목 자체가 깨져서 들어올 경우가 있다.

 

헤더의 Subject 부분이 =?인코딩?B?안전한 문자 ?=  처럼 들어오면 인코딩이 가능하다.

 

그런데 Subject 부분에 그냥 한글을 쓸 경우 제목이 깨지게 되는데..

 

byte[] bytes = string.getBytes("iso-8859-1");

 

String subject = new String(bytes, "euc-kr");

 

과 같이 ISO-8859-1 로 풀어 헤쳐서 다시 스트링으로 조립해 줘야 한다는 ㅡㅡ;;

 

포털 메일에서 첨부 파일 있을때는 안전한 문자로 변형시켜 캐릭터 셋 포함시키고 전달하고..

 

첨부 파일이 없을때는 그냥 보내고 ㅡㅡ

 

어떤 포털은 또 반대로 보내서리... 경우의 수가 여러개 생기넹 ...

 

base64 로 온거 풀어 헤칠때두 어디는 제대로 파싱되는 반면..

 

어디는 깨지고 ㅡㅡ

 

야후에서 보낸거 다음에서 보면 제대로 안보이고 ㅡㅡ

 

 

개발자 맘대로 메일 보내는 부분을 만들면 어쩌냐고여...

 

RFC 규약은 괜히 있냐고여~~ 짱나네 -.-;;

 

 

 

'JAVA > JSP_Servlet' 카테고리의 다른 글

[펌] Trackback CGI 설계  (0) 2005.02.03
[펌] 검색엔진 만들기  (0) 2005.01.21
[펌] 메일 헤더 구성 요소  (0) 2005.01.17
[펌] [MAIL] MIME 관련  (0) 2005.01.17
[펌] 코드시스템및 한글문자체계  (0) 2005.01.17
Posted by tornado
|

헤더는 매우 다양한 구성요소가 있습니다. 메시지를 전달하기 위해 반드시 필요한 것도 있고 꼭 필요하지 않은 것도 있습니다. 또 어떤 헤더는 여러분이 입력하지 않아도 자동으로 입력되는 것도 있습니다. 자동으로 입력되는 헤더는 다시 메일/뉴스 프로그램이 직접 기록하는 것과 서버에서 직접 기록하는 것이 있습니다.

자신이 직접 입력할 수 있는 헤더

자신이 직접 입력할 수 있는 헤더는 주로 메일 프로그램에서 새 메시지를 작성할 때 입력할 수 있는 항목과 프로그램의 설정 항목에서 작성한 내용입니다. 프로그램의 설정 항목에 기록한 내용은 이메일을 보낼때마다 자동으로 헤더에 첨부됩니다. 자신의 소속, 이메일 주소, 진짜 이름등등은 이메일을 보낼때마다 매번 기록할 필요가 없을 것입니다. 그래서 그런 것들은 프로그램 설정 항목에 기록하도록 해서 자동으로 입력할 수 있도록 하는 것입니다. 그러나 받는 사람, 제목, 참조, 숨은 참조등은 이메일을 보낼때마다 매번 바뀌는 항목이라서 매번 새롭게 입력해야 합니다.

 

1. 받은 사람
헤더 - To:

메시지를 보내려면 당연히 받는 사람의 주소를 적어야겠지요. To: 헤더에 아무것도 입력하지 않으면 메시지를 보낼 수 없습니다. 메일 서버는 이곳에 있는 주소로 여러분의 메시지를 보냅니다. 프로그램에서 "수신인" 또는 "받는 사람" 입력창에 입력한 내용이 To: 헤더에 기록됩니다.

2. 참조
헤더 - Cc:

Cc는 "Carbon Copy"의 약자로써 원본 메시지를 참조하는 것을 말합니다. 참조는 어떤 메시지를 여러 사람에게 보낼 때 원본 메시지와는 별도로 다른 사람이 그 메시지를 참조삼아 읽으라고 보내는 경우에 사용합니다. A라는 사람에게 보내는 메시지를 B와 C가 참조할 필요가 있다고 생각한다면 A를 받는 사람으로 지정하고 B와 C는 참조로 지정하여 메시지를 보내면 됩니다. 참조형식으로 보낸 메시지의 헤더에는 Cc: 부분에 받는 사람의 주소가 모두 표시됩니다.

3. 숨은 참조
헤더 - Bcc:

Bcc는 "Blind Carbon Copy"의 약자인데, 숨은 참조라고 합니다. 숨은 참조는 여러 사람에게 메시지를 보낼 때 누구에게 그 메시지를 보냈는지 알 수 없도록 참조인의 주소를 숨길 때 사용합니다. 참조를 나타내는 Cc: 헤더와는 달리 숨은 참조란에 기록한 주소는 헤더에 표시되지 않습니다. 100명에게 메시지를 보낼 때 참조를 이용하는 것은 좋지 못합니다. 100명은 자신에게 필요하지 않은 99명의 이메일 주소가 헤더에 포함된 이메일을 받아야할 것이니까요.

4. 제목
헤더 - Subject:

보낼 메시지의 제목을 나타내는 헤더입니다. 설령 여러분이 제목을 적지 않고 이메일을 보냈더라도 헤더는 표시됩니다. 제목을 "안녕"이라고 적었다면 "Subject: 안녕"이라고 될 것이고 제목을 적지 않았다면 단지 "Subject:"라고만 헤더에 표시될 것입니다.

프로그램의 설정 메뉴에서 기록하는 헤더

사용하는 이메일 프로그램의 설정 메뉴에서 기록한 정보는 보내는 이메일 메시지의 헤더에 자동으로 기록됩니다. 이런 정보는 늘 입력할 필요가 없는 고정된 정보입니다. 이메일을 보내며 매번 자신의 이메일 주소와 이름, 소속, 답신받을 주소를 입력할 필요는 없을 것입니다. 그런 것들은 자동으로 입력하도록 설정해 두는 것이 훨씬 편할 것입니다.

 

1. 전자 우편 주소
헤더 - From:

자신의 주소가 없다면 보낸 메시지가 잘못되었을 때 어디로 반송되어 올지 알 수 없을 것입니다. 자신의 이메일 주소를 적지 않으면 메시지를 작성할 수 없습니다. 정 주소를 밝히기 싫다면 가짜 주소라도 적어야 합니다. somebody@nonono.com 처럼 정체불명의 주소라도 적지 않으면 메시지를 작성할 수 없습니다. 아웃룩 익스프레스에서 From: 헤더를 설정하는 곳은 [계정] 메뉴에서 사용자 정보 입력창입니다. [전자 우편 주소]라는 곳에 입력한 정보가 From: 헤더에 기록됩니다.

2. 이름
헤더 - From:


자신의 이름을 적는 곳입니다. 이곳을 비워두면 메시지를 작성할 수 없습니다. 자기 이름을 입력해도 좋고, 이름을 밝히기 싫다면 가짜 이름 - 'SkyHawk'와 같은 별명이나 가짜 이름을 적어도 무방합니다. 여기에 적은 이름은 From: 헤더에 기록되어 다음과 같이 나타납니다.

From: "Lee JunYoung"
leejuy@hyowon.pusan.ac.kr

3. 회신 주소
헤더 - Reply-To:


상대방이 여러분의 이메일에 대해 회신하기 위해 이메일 프로그램의 회신 기능을 사용할 때 이곳에 적어둔 주소로 답신이 보내집니다. 회신 주소 입력창에 굳이 주소를 적지 않아도 괜찮습니다. Reply-To 헤더가 없을 경우 '전자 우편 주소'로 답신이 보내지게 됩니다.

Reply-To: "Lee JunYoung" <leejuy@hyowon.pusan.ac.kr>

자동으로 붙는 헤더

여러분이 받은 이메일의 헤더를 잘 살펴 보십시오. 그곳에는 여러분이 설정할 수 없는 헤더들이 있을 것입니다. 이런 헤더들은 대부분 센드메일에서 이메일을 주고 받으며 붙인 헤더나 여러분이 사용중인 메일 프로그램에서 붙인 헤더입니다. 이런 헤더는 여러분이 어떻게 조정할 수 없는 것들입니다.

 

1. 메일이 전달된 경로
헤더 - Received:

여러분의 편지함에 도착한 메일이 어디를 거쳐서 왔는지 표시하는 헤더입니다. [from A by B for C]라는 문장으로 이루어져 있습니다. "B라는 메일 서버를 통해 A라는 메일 서버로 전송된 이메일이다. 그리고 그것은 C에게 전달되었다."라는 내용을 담고 있습니다. 이 부분의 헤더를 잘 살펴보면 메일이 어디를 거쳐서 여러분에게 전달되었는지 잘 알 수 있습니다. 여기에 그 사람이 사용하는 컴퓨터의 이름이 표시되기도 하고, 메일 서버로 어떤 프로그램을 사용하는지, 센드메일의 버전등이 표시 됩니다. 아래는 제가 받은 메일에 포함된 Received: 헤더입니다. 이것을 잘 분석해 보십시오.

Received: from hyowon.cc.pusan.ac.kr (hyowon.cc.pusan.ac.kr [164.125.9.3])
by bora.dacom.co.kr (8.8.6/8.8.6) with SMTP id BAA11063
for <social91@bora.dacom.co.kr>; Fri, 30 Jan 1998 01:47:06 +0900 (KST)

첫 라인에서 메일이 hyowon.cc.pusan.ac.kr이라는 서버에서 보내진 것임을 알 수 있습니다. 이곳의 IP 주소는 164.125.9.3이구요. 두 번째 라인에서 메일이 bora.dacom.co.kr이라는 서버로 전달되었음을 알 수 있습니다. 그리고 이 서버는 SMTP 서버로군요. 그리고, 세 번째 라인에서 1998년 1월 30일, 금요일 오전 1시 47분에 social91@bora.dacom.co.kr이라는 사용자에게 전달되었음을 알 수 있습니다. 날짜는 여러분의 메일 서버로 메일이 도착한 시간을 의미합니다. 뒤에 얘기할 Date: 헤더는 상대방이 메일을 보낸 시간을 의미하는데 이것을 보고 메일 서버간의 전달 시간을 체크해 볼 수 있습니다. "+0900 (KST)"는 한국 표준 시각을 말합니다. 그리니치 표준시보다 9시간 빠르다는 뜻이죠?

가끔 Received: 헤더가 여러개 포함된 메시지를 발견할 수 있습니다. 곧장 자신에게 전달된 메시지가 아니라 다른 몇 개의 서버를 거쳐 전달된 경우 Received: 헤더에는 거쳐온 메일 서버들이 모두 표시됩니다. 맨 아래의 Received: 헤더가 제일 처음 메일이 발송된 곳이고 그곳에서 차례대로 위쪽으로 지나온 것입니다. 제가 구독중인 온라인 잡지인 People에서 보내온 메일의 Received: 헤더를 보십시오.

(1) Received: from hyowon.cc.pusan.ac.kr (hyowon.cc.pusan.ac.kr [164.125.9.3])
by bora.dacom.co.kr (8.8.6/8.8.6) with SMTP id CAA13191
for <social91@bora.dacom.co.kr>; Fri, 30 Jan 1998 02:10:40 +0900 (KST)

(2) Received: from listserv.pathfinder.com ([204.71.242.6]) by hyowon.cc.pusan.ac.kr (8.6.11/8.6.8)
with ESMTP id CAA14694 for <leejuy@HYOWON.PUSAN.AC.KR>;
Fri, 30 Jan 1998 02:10:39 +0900

(3) Received: (from daemon@localhost) by pathfinder.com (*private*/SMI-SVR4) id
LAA21552 for peopledaily-text@listserv.pathfinder.com; Thu, 29 Jan
1998 11:44:40 -0500 (EST)

3개의 Received: 헤더가 있습니다. 여러분은 이것을 보고 어떤 정보를 얻을 수 있습니까?

(3) 의 Received: 헤더를 보세요. pathfinder.com이라는 컴퓨터에서 listserv.pathfinder.com이라는 컴퓨터에게 메일을 보내도록 지시했음을 알 수 있습니다. pathfinder.com이라는 컴퓨터는 People지의 메인 컴퓨터입니다. 여기서 온라인 신문을 구독하는 사람들을 위해 신문을 작성합니다. 그리고 실제로 메일을 보내는 컴퓨터인 listserv.pathfinder.com이라는 컴퓨터로 메시지를 보내면 이 컴퓨터가 메일 발송작업을 하는 것입니다. 메일은 미국 동부 표준시(EST) 1998년 1월 29일 오전 11시 44분에 전세계 온라인 People 구독자에게 발송되었습니다.

(2) 의 Received: 헤더에서 이 메시지가 한국 시각 1월 30일 오전 2시 10분 39초에 hyowon.pusan.ac.kr이라는 컴퓨터로 도착했음을 알 수 있습니다. 이 컴퓨터는 제가 학교에서 사용하는 계정이 있는 컴퓨터입니다. 이 주소로 도착한 메일은 즉시 bora.dacom.co.kr이라는 컴퓨터로 돌려 보내도록 설정해 두었죠.

(3)의 Received: 헤더에서 한국 시각 1월 30일 오전 2시 10분 40초에 bora.dacom.co.kr로 메시지가 도착했음을 알 수 있습니다.

메일을 보내고 받은 시각은 센드 메일에서 그리니치 표준시로 계산합니다. +0900 (KST)는 한국 표준 시각이 그리니치 표준시보다 9시간 빠르다는 말입니다. 결국 메일이 미국 동부의 People 메인 컴퓨터에서 social91@bora.dacom.co.kr까지 전달되는데 약 26분이 걸린 것을 알 수 있습니다.

Received: 헤더를 통해 여러분은 이렇게 메일이 전달되는 경로를 알 수 있을 뿐만 아니라 메일의 전달과정에 문제가 생겼을 때 도움을 받기도 합니다. 만약 메일이 전달되는 과정에서 매우 시간이 많이 걸렸거나 또는 데이터의 손실이 발생했다면 Received: 헤더를 참조하여 중간에 어떤 메일 서버를 경유했는지 확인할 수 있습니다. 그리고 그 메일 서버가 문제가 있는지 조사해 보거나 서버 담당자에게 문의할 수 있습니다. 만약 여러분이 메일을 주고 받는 메일 서버의 센드메일 버전이 너무 낮아 메일이 깨지는 문제가 발생한다면 다른 곳으로 서비스를 옮기는 것을 고려할수도 있습니다.

2. 메시지 아이디
헤더 - Message-ID

이 헤더는 메일 서버에서 메시지를 외부로 보내며 붙이는 일련번호입니다. 메시지 아이디는 다음과 같이 표시됩니다.

Message-ID: <199801291644.LAA21552@pathfinder.com>

여러분은 이것을 통해 단지 그 메시지가 어떤 컴퓨터에서 보내졌는지 알 수 있습니다. 그러나, '@' 앞에 적혀있는 이상한 문자열은 여러분을 위한 것이 아니라 컴퓨터가 그 메시지에 임의로 붙인 번호표입니다.

3. 보낸 날짜
헤더 - Date

메일을 보낸 날짜를 표시하며 다음과 같은 형식으로 표시됩니다.

Date: Thu, 29 Jan 1998 02:37:50 -0800 (PST)

시간을 표기하는 방식은 24시간 표기방식으로 마지막에 그리니치 표준시를 표시합니다. 이것을 보고 그 컴퓨터가 위치한 지역을 대략 예상해 볼 수 있습니다. 예문에 제시된 PST(Pacific Standard Timezone)는 태평양 표준시로써 미국과 캐나다에서 사용하는 표준 시간대를 의미합니다. Date: 헤더는 서버에서 직접 붙이는 경우가 있고, 사용자의 컴퓨터 시간을 따르는 경우가 있습니다. 계정에 접속하여 PINE을 통해 메일을 보내면 Date 헤더에 서버의 시계에 맞춰서 Date: 헤더가 붙습니다. 그러나, 아웃룩 익스프레스를 이용하여 메일을 보낼 경우 사용자의 컴퓨터에 있는 시계에 맞춰서 Date 헤더가 붙게 됩니다.

Received: 헤더에 나와있는 시각이 실제로 서버에 메일이 도착한 시간이라면 Date: 헤더는 메일 프로그램에서 메일이 보내진 시각을 의미합니다.

4. MIME 헤더

MIME이란 멀티미디어 메일을 위한 인터넷 표준 규약입니다. 여러분이 MIME 표준을 지원하는 메일 프로그램을 사용하며 'MIME을 사용하여 메일 보내기' 옵션을 설정하면 다음과 같은 헤더를 볼 수 있습니다.

가) MIME-Version: 1.0
MIME의 버전을 나타냅니다.

나) Content-Type:

Content-Type: 헤더는 메일 본문이 어떤 형식인지 알려 줍니다.
text/plain - 일반 문자열을 사용한 본문
multipart/ - 일반 문자열과 다른 여러 가지 인코딩 방식이 섞여 있을 경우 multipart/mixed는 일반 텍스트에 파일을 첨부했을 경우에 사용합니다. multipart/alternative는 똑같은 내용이 일반 텍스트와 html로 반복되어 있어서 둘 중 하나를 선택해서 읽을 수 있을 경우에 사용합니다. multipart/related 는 HTML 형식의 메시지를 보내며 배경그림을 첨부했을 때 사용됩니다.

다) charset="euc-kr"
charset= 부분은 본문이 어떤 언어로 작성되었는지를 나타냅니다. Content-Type 헤더에 함께 나타납니다. 한글 메일의 경우 euc-kr이란 문자셋이 표시되어야 합니다.

라) Content-Transfer-Encoding:
본문이 인코딩된 방식을 표시합니다. 한글 메시지에 8비트라고 표시되어 있으면 인코딩없이 본문을 그대로 보낸 것입니다. BASE64라든가 Quoted Printable라고 적혀 있으면 그런 방식으로 인코딩을 했다는 의미입니다.

제 멋대로 붙인 헤더

헤더는 철저히 규칙에 맞게 붙여야 합니다. 만약 헤더를 마음대로 붙인다면 컴퓨터간에 메시지 전송이 이루어질 수 없기 때문입니다. 어떤 컴퓨터는 O.K를 '좋다'는 뜻으로 이해하는데 다른 컴퓨터는 O.K를 '엉망이다'라는 뜻으로 이해하면 어떻게 되겠습니까?

그런데, 이런 엄격한 규칙을 가진 헤더지만 제 멋대로 붙일 수 있는 헤더도 있습니다. 'X-'라는 문자열로 시작하는 헤더가 그런 경우입니다. 이런 헤더는 메일 서버에서 신경을 쓰지 않고 그냥 넘겨 버립니다. 신경을 쓰지 않기 때문에 이런 헤더에 무슨 내용을 쓰더라도 메일의 전송에 영향을 끼치지 않습니다. 아웃룩 익스프레스를 사용하여 보낸 메일에는 이런 'X-'가 붙은 헤더를 몇 개 볼 수 있습니다.

X-Priority: 3
X-MSMail-Priority: Normal
X-Mailer: Microsoft Outlook Express 4.72.2106.4
X-MimeOLE: Produced By Microsoft MimeOLE V4.72.2106.4

이 헤더들은 아웃룩 익스프레스라는 프로그램이 임의로 붙인 헤더입니다. 이런 헤더들은 인터넷의 표준이 아닐뿐더러 표준으로 인정받을 필요도 없습니다. 그렇기 때문에 'X-'라는 헤더를 사용한 것이죠.

'X-' 헤더는 매우 다양한 목적으로 사용될 수 있는데, 예문에서 볼 수 있듯이 메일 프로그램을 헤더에 표시하는데 사용되기도 합니다. X-Mailer: 헤더를 통해 메일이 어떤 프로그램을 사용하여 보내졌는지 표시하고 있습니다. X-MimeOLE: 는 메일에 포함된 바이너리 파일을 윈도우의 OLE와 연결시키는 것에 대한 헤더입니다.

또는 그 메일 프로그램이 제공하는 특별한 기능을 표시하기 위해 사용되기도 합니다. 예문에 있는 X-Priority: 헤더와 X-MSMail-Priority: 헤더는 아웃룩 익스프레스를 사용하는 메일에만 붙습니다. 이것은 메일의 보내며 중요도를 설정할 때 나타납니다.

중요도 설정에 따라 헤더의 내용이 달라집니다.

중요도X-MSMail-PriorityX-Priority
높 음High1
보 통Normal3
낮 음Low5

중요도가 높게 설정된 메시지를 받으면 메시지 표시 창에 느낌표(!)가 표시되어 그 메시지가 중요한 것임을 표시합니다. 이것은 단지 아웃룩 익스프레스에서 제공하는 특별한 기능일 뿐 메일 암호화라든가 보안문제를 해결하는 기능은 아닙니다.

'JAVA > JSP_Servlet' 카테고리의 다른 글

[펌] 검색엔진 만들기  (0) 2005.01.21
[javamail] 제목 깨질때...  (0) 2005.01.18
[펌] [MAIL] MIME 관련  (0) 2005.01.17
[펌] 코드시스템및 한글문자체계  (0) 2005.01.17
[펌] http RFC  (0) 2005.01.17
Posted by tornado
|
. MIME

MIME(Multipurpose Internet Message Extension)은 말 그대로 Internet Message를 통해서 여러 가지 내용을 보낼 수 있도록 확장한 규격입니다.

한글같은 2바이트 Non-ASCII 문자가 여러 메일 서버를 무사히 통과하기 위해서는 7비트 ASCII 문자로 일단 변환되어야 합니다. 물론, 요즘의 대부분의 메일 시스템은 보통 8비트의 Non-ASCII 문자들도 손상시키지 않고 통과시키만 (8bit clean이라 하죠...), 그렇지 않은 경우도 아직 꽤 많습니다. 때문에, 그냥 8비트인 상태로 보내면, 메시지가 올바로 간다고 보장할 수 없게 됩니다. 게다가 단순한 텍스트 뿐만이 아니라, 요즘에는 바이너리 파일도 메일에 첨부하는 경우가 많죠?

그런 것들을 위해서 MIME에서는 Message Body에 있는 내용의 형태, Encoding 방식, 각 부분의 연관 관계 등을 지정할 수 있는 규약을 정하고 있습니다.



2. MIME Header

MIME 형태를 지정하기 위해서 RFC822에서 정의된 Header Field 외에 몇 가지 Header Field가 새로 정의되었습니다. 이 Header들은 message의 Top Level Header 혹은 Message Body Part의 Header에 들어갑니다.

MIME-Version Header Field
Content-Type Header Field
Content-Transfer-Encoding Header Field
Content-Disposition Header Field
Content-ID Header Field
Content-Description Header Field

MIME-Version Header Field는 Message의 주 Header에 있게 됩니다.
Content-Type, Content-Transfer-Encoding Header Field는 Top Level Header에 있을 수도 있고, Body Part의 Header에 있을 수도 있습니다.
Content-Disposition Header Field는 Top Level Header에 쓰일 수도 있지만, 대부분의 경우에는 Body Part의 Header에 있게 됩니다.


1. MIME-Version 헤더 필드

MIME 규약에 따르는 Message는 이 Header를 반드시 가지고 있어야만 합니다. 필수라고 RFC에서는 강조하고 있지만, 이 Header FIeld를 가지고 처리할 수 있는 건 아무 것도 없습니다. 그래서 실제로는 이 Header Field가 빠진 메시지도 상당히 많습니다.

MIME-Version: 1.0

지금 현재 MIME 버전은 현재까지는 무조건 1.0 입니다. 이 헤더를 가지고 있으면, 이후의 모든 내용은 MIME의 규격을 준수한다는 것을 의미하게 됩니다. 물론 실제로 보장되지는 않습니다.
EBNF Form으로 구성하면 아래와 같이 됩니다.

version := "MIME-Version" ":" 1Digit "." 1Digit

아래의 세 가지 예 모두 동일하며, 문제 없는 형식입니다.

MIME-Version: 1.0 (prodeced by MetaSend Vx.x)
MIME-Version: (prodeced by MetaSend Vx.x) 1.0
MIME-Version: 1.(prodeced by MetaSend Vx.x)0



2. Content-Type 헤더 필드

메시지 본문 혹은 본문의 각 부분의 내용의 종류를 지정하는 헤더 필드입니다. 보통 MIME Type이라고 부르는 것들은 바로 이걸 말하죠. 이 헤더 필드의 문법은 아래와 같습니다.

content := "Content-Type" ":" type "/" subtype *(";" parameter)

"Content-Type:" 이라는 헤더 이름 다음에, "타입/서브타입"과 같은 형태로 지정합니다.
그리고 여기에 parameter를 붙이고 싶으면 세미콜론으로 구분하면서 계속 적어나가면 됩니다.

타입은 크게 두 가지로 나뉩니다.
첫번째는 Discrete-Type인데, 이 것은 그 자체로 어떤 의미를 가지고 있는 Content-Type이 됩니다. 예를 들어, Text라든지, Image 같은 것들이 여기에 해당됩니다.
각각의 Type은 여러 가지 Subtype을 가질 수 있습니다. 예를 들어서, Text의 경우에는, 일반 ASCII 텍스트를 나타내는, text/plain, text/html 등의 Subtype을 가집니다.
Discrete-Type에 해당되는 것들은 아래와 같습니다.

"text" / "image" / "audio" / "video" / "application" / extension-token

두번째는 Composite-Type입니다. 이것은 Discrete-Type 혹은 Composite-Type의 개체 여러 개가 복합되어 만들어진 타입입니다. 이런... 재귀적인 설명이 되어 버렸군요. Composite-Type은 일단은 두 가지 입니다. 그 외에 확장된 타입을 둘 수 있다고 되어 있습니다. 이 중에서 MIME 규약이 점점 커지면서 다양한 서브 타입이 생겨나고 있는 것은 multipart 타입입니다.

"message" / "multipart"

Content-Type 헤더 필드가 없는 메시지도 많습니다. 이런 경우에는 어떻게 처리해야 할까요?
이 헤더가 없는 메시지는 MIME 규약이 생기기 전에 만들어진 메시지로 취급하면 됩니다. 즉, 단순한 ASCII 텍스트로만 이루어진 메시지로 처리하는 거죠...
아래가 Content-Type 헤더 필드가 없는 경우의 기본 Content-Type이 됩니다.

Content-Type: text/plain; charset=us-ascii

각 Content-Type에 대한 좀 더 자세한 내용은 다음에 설명하도록 하겠습니다.



3. Content-Transfer-Encoding 헤더 필드

이 헤더 필드는 메시지의 본문이나 본문의 각 부분이 어떤 방식으로 Encoding되었는지를 나타냅니다. 이 헤더 필드의 문법은 아래와 같습니다.

encoding := "Content-Transfer-Encoding" ":" mechanism

mechanism에는 다섯 가지가 있습니다.

"7bit" / "8bit" / "binary" / "quoted-printable" / "base64"

7bit, 8bit, binary를 먼저 보자면... 이 경우는 Encoding을 하지 않았다는 얘깁니다.

   7bit는 원래 ASCII 문자만 있다는 것을 의미하고,
   8bit는 8비트 문자들도 그래도 들어 있다는 것을 의미하고,
   binary는 8bit와 같은데, 한 줄의 길이의 제한이 없다는 것을 의미합니다. (안 쓰인다고 보면 됩니다).

물론, 한글을 7비트로 Encoding한 경우는 물론 Decoding이 필요합니다.
이 방식은 사라지고 있고, 요즘에는 한글은 base64로 Encoding하는 경우가 절대 다수이니까요.

그 다음에 특정한 알고리즘을 필요로 하는 두 개의 Encoding 방식이 있습니다.
Quoted-Printable하고 base64 Encoding이죠.

   Base64 Encoding은 간단히 말해서 3바이트의 데이타를 4바이트에 저장하는 겁니다. 24비트를 6비트씩 4개로 나누어서 따로 저장하고 각 바이트의 위쪽 두 비트를 0으로 하니까 무조건 ASCII 문자라는게 보장이 되는 거죠.
따라서 8비트 중에서 6비트만 사용하므로, 인코딩 결과로 나온 메시지는 모두 64가지의 Octet만을 가질 수 있게 됩니다. 그래서 Base64라고 부릅니다.
이 64개의 Octet는 ASCII와 같은 코드셋에도 독립적으로 설계되었습니다.
이 Encoding 방식의 장점이라고 하면, 원래의 데이타가 Encoding 후에 크기가 얼마가 될지 거의 정확하게 예측할 수 있다는 거죠. 약 33%정도가 늘어나죠. 또 다른 인코딩 방식에 비해 공간을 덜 차지하기 때문에, 효율적이라고 할 수 있습니다. 반면, Encoding을 하고 나면 원래 내용이 뭔지 도저히 알아볼 수 없다는 단점이 있죠.

   Quoted-Printable 방식은 ASCII 문자가 아닌 놈들만 "=XX" (X : 0...9, A...F)와 같은 모양으로 Encoding 하는 방식을 말합니다. 따라서 7Bit ASCII로 표현할 수 없는 문자는 한 바이트가 3바이트로 늘어나게 됩니다. 따라서, 이 방식은 크기가 최대 3배로 늘어날 수 있으니까 효율성 면에서는 거의 최악이라고 할 수 있습니다.

이 Encoding 방식은 영어 문화권에서 보면 나쁜 Encoding이 아닐 수 있습니다. 영어 문화권에서 만들어지는 텍스트는, 대부분의 문자가 7비트 ASCII 문자로 되어 있고, 그렇지 않은 문자가 훨씬 적습니다. 따라서, 이 방식으로 Encoding을 하면, 대부분의 글자가 Encoding이 되지 않고 일부분만이 Encoding이 됩니다.
그러니까, 일단 오히려 효율적일 수도 있고, 다양한 Decoding을 수행하지 못하는 MUA에서 메일을 보더라도, 대략적으로 내용을 파악할 수 있다는 거죠. 반면 한글 같이 거의가 Non-ASCII 문자인 Message나 바이너리 파일 같은 경우는 이 방식으로 Encoding 해봤자 좋은 점이 없습니다. 괜히 메모리나 디스크, 네트워크만 더 잡아먹을 뿐이지요.

정리하자면 Quoted-Printable Encoding은 Decoding이 안 되더라도 내용을 대략적으로 알아볼 수 있지만, 효율이 안 좋고, base64 Encoding은 효율이 좋지만, Decoding하기 전까지는 내용을 전혀 못 알아볼 수 없다는 겁니다.

그래서, 대부분의 ASCII 문자가 많은 텍스트나 HTML의 경우에는 Quoted-Printable로 Encoding 하는 경우가 많고, 한글이나 바이너리 파일은 거의 무조건 base64로 Encoding합니다.
이것은 그냥 그런 경우가 많이 있다는 것이고, 실제로 각 Body Part의 Encoding의 MUA 맘대로 (혹은 사용자의 선택으로) 하게 되겠죠.


4. Content-Disposition 헤더 필드

이 헤더 필드는 각각의 본문의 부분이 화면에 바로 보여야 하는지, 아니면 첨부된 파일로 보여서, 사용자가 따로 처리해야 하는지를 명시하기 위해서 사용됩니다. 문법은 아래와 같습니다.

disposition := "Content-Disposition" ":" disposition-type *(";" disposition-param)

disposition-type이 붙고 parameter가 붙을 수 있습니다.
disposition-type에는 두 가지가 있을 수 있습니다.

"inline" / "attachment"

inline은 사용자가 메시지를 보려고 할 때, 무조건 화면에 나타나야 한다는 걸 말하고, attachment는 메시지의 주 본문과는 별도로 처리될 수 있다는 걸 표시합니다. 물론 요즘 Windows용 MUA같은 경우에는 첨부된 그림 파일 같은 건 바로 화면에 보여주죠? attachment는 반드시 따로 처리해야 하는게 아니라, 반드시 화면에 바로 나타낼 필요는 없다는 걸 알려주는 겁니다.

parameter는 아래와 같은 형식으로 만들 수 있습니다. attachment같은 경우에는 "filename" parameter를 지정하는 것이 일반적입니다.

disposition-param := "filename" "=" value
    / "creation-date" "=" quoted-date-time
    / "modification-date" "=" quoted-date-time
    / "read-date" "=" quoted-date-time
    / "size" "=" 1*Digit

이 헤더가 필요한 이유는 Content-Type만을 보고 이 메시지 덩어리가 inline인지, attachment 인지 판단하기 힘든 경우가 많기 때문입니다.
예를 들어서, 어떤 사람이 텍스트로 메일을 열심히 쓴 다음에 참고할 만한 텍스트 파일을 첨부해서 메일로 보냈다고 합시다. 그러면 이 사람이 작성한 주 본문의 Content-Type은 text/plain이겠죠? 이 사람이 첨부한 파일의 Content-Type도 text/plain이겠죠?
그러면 메일을 받았을 때, 어느 부분이 진짜 주 본문인지 판단하기 힘들죠. 보통은 주 본문을 앞에 두니까, 별 상관없다고 할 수 있지만, 받은 메시지만 보고 판단할 방법은 없다는 겁니다.
어쨌든 보통의 MUA에서는 주 본문에 해당되는 부분에는 이 헤더를 굳이 설정하지 않고, attach에 해당되는 부분에는 이 헤더를 표기해 주는 경우가 많습니다.
MTA와는 전혀 관계없고, 순전히 MUA를 위해서 사용되는 Header Field라고 할 수 있습니다.



5. Content-ID, Content-Description 헤더 필드

이 두 헤더 필드의 문법은 아래와 같습니다.

id = "Content-ID" ":" "<" localpart @ domain ">"
description := "Content-Description" ":" *text

이 중 Content-ID와 같은 경우에는 평소에는 별로 중요하게 쓰이지 않지만, HTML Message 중 Embedded Image가 있는 경우에는 반드시 사용되는 중요한 Field입니다.
1. Content-Type의 종류들...

MIME의 Content-Type으로 공식적으로 등록된 것들은 얼마나 될까요? 엄청나게 많겠죠? Windows 탐색기의 파일 타입만 보더라도, 매우 많은 타입을 볼 수 있지만, 많은 응용 프로그램들이 생겨나고 있으니, 갈수록 더 늘어나겠죠.

"Text" 타입

인터넷 텍스트 메시지의 기본적인 타입입니다. 여기에 포함되는 서브 타입들을 보면...

text/plain, text/html 등등...

"Text" 타입은 charset이라고 부르는 문자 집합을 지정하는 parameter를 가질 수 있습니다.
기본 값은 앞에서도 말씀드렸듯이 US-ASCII죠.
Content-Type이 지정되지 않은 메시지 본문은 text/plain으로 처리합니다. 모든 메시지 본문의 기본 Content-Type은...

text/plain; charset=US-ASCII

요렇게 되는 거죠. charset에는 그밖에도 iso-8859-X (X : 1~10) 이라는 것이 있습니다. 한글을 비롯한 여러 국가에서 쓰이는 EUC (Extended Unix Characterset) 라는 것도 있죠. 한글 메세지의 실제로 많이 사용되는 표준 문자 집합은 EUC-KR라고 생각되네요.

"Image" 타입

이 타입은 GIF, JPEG 등의 이미지를 지원하기 위해서 처음 만들어졌습니다. 물론 Image에 해당되는 서브 타입도 아주 많지만, 거의 모든 곳에서 지원되는 타입이라면,
image/jpeg, image/gif

이 두 가지라고 할 수있겠네요.

"Audio" 타입

이 타입은 MUA가 동작하는 환경에서 이를 얼마나 지원하느냐가 문제겠죠? 그리고 Audio에 대해서는 특별한 표준이... 없습니다. MP3, Real Media 등과 같은 Audio 소스가 요즘에는 많지만, MIME에서는 공식적으로 사용하는 기본 규격은 8비트 PCM, 8KHz 규격입니다. -_-;

audio/basic

"Video" 타입

Vidoe 서브 타입도 Audio와 마찬가지로 MUA의 환경이 문제가 됩니다. 이 타입의 기본 서브타입은 MPEG으로 되어 있습니다.

video/mpeg

"Application" 타입

Application 타입은 다른 응용 프로그램이 이걸 처리해야 한다는 걸 의미합니다. 그래서 이 타입의 기본 동작은 사용자의 디스크에 저장하는게 되는 거죠. 여러 MUA에서 바로 이런 타입의 Content를 열 수 있는 기능을 지원하지만, 사실은 임시 공간에 파일을 저장하고 응용 프로그램을 부르는 방식이죠.

넘쳐나는 응용프로그램의 MIME Content-Type을 모두 인식하기란 사실상 힘들다고 볼 수 있습니다. 따라서 알 수 없는 파일의 기본 Content-Type은,

application/octet-stream

로 정의되고, 이 Content-Type에 대한 기본 동작은, "로컬 디스크에 저장"이 됩니다.

RFC에 언급되기로는 application/Postscript과 같은 서브타입이 있고, 보안 관련 이슈가 상당히 많지만, 별로 흥미가 없네요. ^^;

1. 기본 구조

"Multipart" 타입의 기본 구조를 보기 전에, Multipart 타입의 기본 서브 타입에 대해서 이야기를 해야겠네요.

Multipart Content-Type의 기본 Subtype은 "multipart/mixed"라는 서브 타입입니다. 각각의 부분은 연관관계가 없으며, 각 부분의 순서만 미미하게(?) 의미가 있는 거죠. 꼭 하나의 (특히 첫번째) 본문 부분이 inline 메시지고, 나머지는 attachment라는 보장은 없습니다. 또, 각각의 본문 부분도 multipart로 구성될 수 있습니다. 따라서 이론적으로 무한대의 중첩이 가능합니다. 실제로는 최대 세 단계 정도로 mulitpart가 구성되는 것이 일반적입니다. 어째서 세 단계까지인지는 이후에 설명하겠습니다.

맨 앞에서 메시지는 헤더와 본문으로 구성되고 둘의 구분은 NULL 라인으로 한다고 말씀드렸죠?
메시지 본문은 단순히 라인의 집합의 형태로 평면적인 구성을 하고 있기 때문에, 여러 부분으로 나누기 위해서는 각 부분의 경계를 표시할 수 있는 표식이 필요하고, 이것이 Multipart Content-Type의 기본 Parameter가 됩니다. 이 Parameter를 그걸 boundary라 부릅니다.

기본 구조를 보면 아래와 같습니다.

Content-Type: multipart/mixed; boundary=gc0p4Jq0M2Yt08j34c0p

boundary로 쓰일 수 있는 문자는 ASCII 문자 중에서 일부에 속합니다. 아래의 예는 올바른 boundary 구성이라고 볼 수 없습니다. 콜론이 들어가 있기 때문입니다. 콜론은 헤더 필드 이름과 필드 본문을 구분하기 위해서 사용되는 문자니까요.

Content-Type: multipart/mixed; boundary=gc0p4Jq0M:2Yt08j34c0p

그렇다고 완전히 틀린 건 아니고, 아래처럼 따옴표로 묶으면 적법한 boundary가 됩니다. 좀 애매한 기준이긴 합니다만, 실제로는 boundary를 Uniqueue하게 만들기 위해서 이런 식으로 쓰이는 경우도 많습니다.

Content-Type: multipart/mixed; "boundary=gc0p4Jq0M:2Yt08j34c0p"

boundary는 메시지 안에 이런 형태가 나타나지 않도록 신중하게 만들어져야 합니다. 특히, 다음에 설명할 "message" Content-Type과 같이 메시지를 가지고 있는 본문 부분의 경우에 문제가 발생할 소지가 많기 때문이죠.

multipart로 구성된 메시지의 예를 하나 보죠.

From: Nathaniel Borenstein
To: Ned Freed
Date: Sun, 21 Mar 1993 23:56:48 -0800 (PST)
Subject: Sample message
MIME-Version: 1.0
Content-type: multipart/mixed; boundary="simple boundary"

This is the preamble. It is to be ignored, though it
is a handy place for composition agents to include an
explanatory note to non-MIME conformant readers.

--simple boundary

This is implicitly typed plain US-ASCII text.
It does NOT end with a linebreak.
--simple boundary
Content-type: text/plain; charset=us-ascii

This is explicitly typed plain US-ASCII text.
It DOES end with a linebreak.

--simple boundary--

This is the epilogue. It is also to be ignored.

보면, 인자로 주어진 boundary에 두 가지 변형을 해서 boundary로 사용하고 있습니다. 첫번째는 앞에 "--"를 붙인 것입니다. 이것을 메시지 부분간의 실제 경계로 사용합니다. 그 담엔 앞 뒤에 "--"를 붙인 것이 있지요? 이건 multipart의 끝을 의미합니다.

일단 Content-Type이 multipart로 선언되면 첫번째 경계가 나타나기 전까지의 메시지는 "반드시" 무시해야 합니다. 그리고 경계가 나타나면 다음 경계가 나타나기 전까지의 부분을 하나의 본문 파트로 처리하면 됩니다. 그러다가 multipart의 끝을 나타내는 표식이 나타나면 실제 mulitpart 메시지가 끝난 걸로 처리합니다. 이후에 나타나는 모든 문자는 역시 "반드시" 무시해야 합니다.
뭐 생각보다 간단하다고 생각할 수도 있겠지만, 이 기본적인 구조를 지키지 않는 메일 시스템이 참 많습니다. boundary를 단순히 "mail-boundary"와 같이 표시하는 것들은 좀 문제를 많이 일으키죠. 또한 에필로그에 해당되는 경계를 제대로 넣어주지 않는 경우도 많이 있습니다.



2. "multipart" 타입의 각 서브 타입

이게 각각의 서브 타입들을 살펴보도록 하죠. 앞에서 본 기본 구조를 머리 속에 두고 각각의 서브 타입의 차이점들을 살펴보도록 하죠.

"Mixed" 서브 타입

앞서 말씀드린 것과 같이 mulitpart 타입의 기본 서브타입입니다. 내용은 앞과 마찬가지고, 중요한 것은, 알 수 없는 multipart의 어떤 서브 타입이 나타나더라도 처리를 못해서는 안 된다는 겁니다. 알 수 없는 multipart 서브 타입는 이 multipart/mixed 서브 타입으로 처리하도록 되어 있습니다.

"Alternative" 서브 타입

이 타입은 형식상으로는 multipart/mixed 타입과 완전히 동일합니다. 다른 점은, 이 타입은 같은 내용에 대한 서로 다른 버전을 담고 있다는 것이죠. 이 메시지를 처리하는 쪽 즉, MUA에서는 자신의 환경에 맞는 가장 좋은 버전 하나만을 선택하면 됩니다.
보통은 가장 훌륭한 버전을 맨 뒤에 놓습니다. 어떤 생각해 보면, 가장 좋은 버전이 맨 앞에 있어야 하는 것 같기도 하지만, 이런 타입을 제대로 처리하지 못하는 MUA 환경을 생각할 때, 가장 단순하고 일반적인 타입이 맨 앞에 있어야 그나마 처리가 쉬워집니다.

예를 하나 보죠.

From: Nathaniel Borenstein
To: Ned Freed
Date: Mon, 22 Mar 1993 09:41:09 -0800 (PST)
Subject: Formatted text mail
MIME-Version: 1.0
Content-Type: multipart/alternative; boundary=boundary42

--boundary42
Content-Type: text/plain; charset=us-ascii

... plain text version of message goes here ...

--boundary42
Content-Type: text/enriched

... RFC 1896 text/enriched version of same message goes here ...

--boundary42
Content-Type: application/x-whatever

... fanciest version of same message goes here ...

--boundary42--

각각의 내용은 모두 같지만, 서로 다른 포맷을 가지는 내용을 이렇게 구분할 수 있겠죠. 이 타입을 사용하는 중요한 예는 바로 HTML 문서를 메일로 보내는 경우가 됩니다. MUA마다 HTML를 처리할 수 있는지 없는지가 달라지기 때문에, 앞부분에는 그냥 일반적인 텍스트 즉, text/plain을 넣고 그 뒤에 text/html을 넣는 겁니다.

"Digest" 서브 타입

이 서브 타입의 기본 문법은 "multipart/mixed"와 동일합니다. 다른 점은 multipart/mixed 타입에서, 각 부분의 기본 Content-Type이 text/plain인 것과는 달리 이 타입의 기본 Content-Type은 "message/rfc822"라는 겁니다.

message/rfc822라는 타입은 헤더를 포함한 메시지 자체를 나타냅니다.
즉, 메일 메시지를 캡슐화하고 있는 거라고 보면 됩니다. 보통 되돌아온 메일에 attach로 달려 있는 원래 메시지가 이 타입을 가지고 있습니다.

실제로 아직 Multipart-Digest 타입으로 된 메시지는 보지 못했습니다. 이 형태를 Multipart-Mixed 타입으로 처리해도 큰 문제는 없습니다.

예를 한 번 보죠.

From: Moderator-Address
To: Recipient-List
Date: Mon, 22 Mar 1994 13:34:51 +0000
Subject: Internet Digest, volume 42
MIME-Version: 1.0
Content-Type: multipart/digest; boundary="---- main boundary ----"

------ main boundary ----

...Introductory text or table of contents...

------ main boundary ----
Content-Type: multipart/digest;
boundary="---- next message ----"

------ next message ----

From: someone-else
Date: Fri, 26 Mar 1993 11:13:32 +0200
Subject: my opinion

...body goes here ...

------ next message ----

From: someone-else-again
Date: Fri, 26 Mar 1993 10:07:13 -0500
Subject: my different opinion

... another body goes here ...

------ next message ------

------ main boundary ------


"Parallel" 서브 타입

"multipart/mixed"와 다른 점이라고 하자면, 이 타입에서는 각각의 본문 파트가, 하드웨어에 의하든 소프트웨어에 의하든 간에 병렬적으로 디스플레이가 가능해야 한다는 거죠. 예를 들어 프리젠테이션과 같이 텍스트와 이미지와 오디오가 동시에 나타나야 하는 경우를 들 수 있겠네요. 하지만, 상당히 플랫폼의 제약을 많이 받기 때문에, 실제로 사용되는 일은 거의 없다고 보면 되겠습니다.

"Report" 서브 타입

이 타입은 현재로는 메시지의 상태를 알리기 위한 것으로 사용되고 있습니다. 즉 되돌아오는 메일 등을 이 형태로 처리하게 됩니다. 좀 더 자세한 내용은 Notification Message에서 다루도록 하겠습니다.

이 타입은 어떤 본문 파트의 일부분으로는 사용될 수 없으며, "반드시" Top Level의 Content- Type으로만 쓰이도록 되어 있습니다. 그리고 이 메시지의 전체는 반드시 7비트 ASCII 문자로만 이루어져 있어야 합니다. (실제로는 8bit로 된 경우도 존재합니다.)

이 타입의 서브 파트는 크게 두 개 혹은 세 개이고, 이 순서로 나타나야 합니다. 세번째 Part는 없을 수도 있습니다.

첫번째 부분은 사람이 알아볼 수 있는 상태 설명 부분입니다. 보통 Content-Type이 지정되어 있지 않기 때문에, Content-Type을 text/plain으로 설정해서 보게 됩니다.

두번째 부분은 기계가 파싱해서 사용할 수 있는 상태 설명입니다. Content-Type은 "message/delivery-status"와 같은 형태로 나타납니다. 이 부분도 반드시 나타나야 합니다.

세번째 부분은 선택 사항으로 원래 메시지 전체 혹은 일부분을 가지고 있습니다. 보통은 헤더에 문제가 있어서 메시지가 되돌아 오는 경우가 많기 때문에, RFC에서는 메시지 헤더만을 가지는 "text/rfc822" (실제로는 message/rfc822-headers가 더 많습니다) 타입을 권장하고 있습니다. 하지만, 요즘 같이 네트워크 용량이 풍부한 세상에서는 앞서 말씀드린 "message/rfc822" 타입으로 메시지 전체가 포함되는 것이 일반적입니다.


"Related" 서브 타입

이 타입이 multipart 타입 중에서 가장 어려운 타입이라고 볼 수 있겠네요. 이 타입은 각 본문 파트가 연관을 가지고 있다는 것을 말합니다. 그런데, 각 부분을 연결하는 방식이 응용 프로그램마다 천차만별일 수밖에 없기 때문에, 이 타입은 뭐라고 설명하기가 어렵네요.

하지만, 역시 이 타입이 중요한 이유는 HTML 문서를 메시지로 전달하기 위한 것입니다. HTML 문서에는 단순히 텍스트 뿐만이 아니라, 이미지 등의 많은 객체가 포함되어야 하는데, 거기에 바로 각 Content들을 Content-ID로 연결해서 사용하는 겁니다. 자세한 내용은 HTML 메시지에서 알아보도록 하죠.
1. "Message" 타입의 각 서브 타입

"RFC822" Subtype

앞서서 말씀드렸듯이 가장 간단하게 메일 메시지를 포함하고 있는 타입을 뜻합니다.
되돌아온 메시지에 가장 많이 포함된다고 말씀드렸죠?
"Partial" 서브 타입

메일 시스템 용량이 아무리 빠방하다 하더라도, 어떤 큰 메시지라도 끄떡없이 보낼 수 있는 건 아닙니다. 한 번에 통과할 수 있는 메시지의 크기가 여러 가지 이유로 제한이 생기기 마련이죠. 그래서, 뭔가 큰 메시지를 보내기 위해서는 여러 개의 메시지로 잘라서 보내고 받는 쪽에서 합쳐서 봐어 합니다. 그것을 위한 타입입니다.

그래서, 각각의 메시지에 몇 번째 조각이며, 전체는 몇 조각으로 되어 있는지를 표시합니다. 물론 같은 메시지의 조각임을 나타내기 위해서 id라는 parameter를 사용합니다. 아래의 예를 보죠.

3조각 메시지의 두번째 조각
Content-Type: Message/Partial; number=2; total=3;
    id="oc=jpbe0M2Yt4s@thumper.bellcore.com"
혹은,
Content-Type: Message/Partial;
    id="oc=jpbe0M2Yt4s@thumper.bellcore.com";
    number=2

3조각 메시지의 세번째 조각 - 반드시 전체 조각수를 가지고 있어서, 자신이 마지막 조각임을 알려야 합니다.
Content-Type: Message/Partial; number=3; total=3;
    id="oc=jpbe0M2Yt4s@thumper.bellcore.com"

그렇다고 메시지를 조각낼 때, 아무렇게나 조각낼 수 있는 건 아니고, 몇 가지 원칙이 있습니다.

   일단 메시지는 "반드시" 라인이 끝나는 지점에서만 잘려야 합니다.
   각 메시지 마다 Content-*, Subject, Message-ID, Encrypted, MIME-Version를 제외한 모든 헤더 필드가 덧붙여져야 합니다.
   메시지가 합쳐질 때는, 헤더 중 Content-*, Subject, Message-ID, Encrypted, MIME-Version만이 살아남고, 나머지는 무시됩니다.
   두번째 조각 이후의 헤더 필드는 깡그리 무시됩니다.

어쨌든 쉽지 않은 모양이네요...


"External-Body" 서브 타입

이 타입은 실제 내용이 메시지 내부에 있는게 아니라 외부에 있다고 알리는 건데요. 그 외부 소스의 형태가 메일, FTP, HTTP, 로칼 디스크 등 워낙 다양하기 때문에, 사용하기도 어렵고, 설명하긴 더 힘드네요. ^^;
1. 헤더 필드의 확장

일단, 문법을 좀 보죠.

encoded-word := "=?" charset "?" encoding "?" encoded-text "?="

좀 복잡하죠? 간단히 쓰자면
=? (문자집합) ? (Encoding 방식) ? (인코딩된 문자열) ?=

'JAVA > JSP_Servlet' 카테고리의 다른 글

[javamail] 제목 깨질때...  (0) 2005.01.18
[펌] 메일 헤더 구성 요소  (0) 2005.01.17
[펌] 코드시스템및 한글문자체계  (0) 2005.01.17
[펌] http RFC  (0) 2005.01.17
[펌]Single Sign On...  (0) 2005.01.17
Posted by tornado
|

Report on Code system and Hangeul Character set

 

 

Table of Contents

1.  Introduction

2.  Code 체계

3.  한글 Character Set

4.  Problem Definitions

5.  Conclusion

 

1.      Introduction

Home Page, E-mail, Java 등을 사용하다 보면 Unicode (ISO/IEC 10646) 부호계를 알지 못하면 그런데서 한글을 사용하기가 불편해 진다. ISO/IEC 10646 국제 표준 부호계와 한글 부호계에 대하여 살펴 보고자 한다. 또한 E-mail에서 많이 쓰이는 Quoted Printable (QP) Base64 살펴 , UTF-16, UTF-8, UTF-7 각종 UTF 대하여 살펴본다. ISO/IEC 10646 KS X 1005-1 (과거 KS C 5700)으로 받아 들였는데, 이에 대한 소개도 더붙인다. 현재 Internet E-mail 등에서는 한글 완성형 KS X 1001 ASCII (KS X 1003) 같이 , MIME charset 이름으로 EUC-KR 쓰고 있는데, EUC-KR 이라는 MIME charset KS C 5601 대하여도 살펴보며, 이러한 code체계가 한글에 미치는 영향과 문제점들을 정리하였다.

 

2.      Code 체계

정보 처리 분야의 규격이 늘어나게 데는 국제 표준 기구의 영향이 크다. 1987년에 ISO(International Organization for Standardization) TC97 IEC(International Electrotechnical Commission ) TC83 공동으로 ISO/IEC JTC1 (Joint Technical Committee 1) 만들면서 정보 처리 분야의 국내 규격이 많이 늘어나게 되었다.

1992 6-7월에 열린 ISO/IEC JTC1/SC2/WG2 22 회의에서 ISO/IEC 10646-1 국제 표준 (IS)으로 확정함에 따라서, 거기에 들어 있던 한글 부호계도 확정되게 되었다. ISO/IEC 10646 글자를 4 Byte 나타내는 4 Byte 부호계 (UCS-4) 시작하였으며, 전세계의 주요한 글자계를 거의 모두 나타 있다. [경석]

 

2.1      용어 정의

2.1.1     code (부호계)

code codeword 집합이라고 있다. ISO/IEC 10646 code에서 첫가끝 조합형 첫소리 글자 ㄱ의 codeword 0x0000 1100 된다.

2.1.2     Codeword(부호값)

code codeword 집합이다. 표준에 따라서는 codeword라는 용어 대신 ISO/IEC 10646 에서는 code position이라는 용어를 사용하고, Unicode에서는 code point라는 용어를 쓰며, 다른 곳에서는 code element라는 용어를 사용하기도 하나 모두 같은 용어이다.

2.1.3     Plane

4 Byte code UCS-4에서 윗자리 16bit 값은 같고, 나머지 아래자리 16bit 다른 codeword 집합을 plane이라고 한다. Plane 하나에는 codeword 64K(65,536) 만큼 있게 되며, plane Group 번호와 Plane번호를 각각 hexadecimal 나타낸다. 예를 들면 BMP (Basic Multilingual Plane) 경우 G=00, P=00 된다.

 

BMP는 다시 4개의 영역으로 나누어 지는데 각 영역의 특징은 다음과 같다.

             A-zone: alphabetic and syllabic scripts together with various symbols

             I-zone: Chinese/Japanese/Korean (CJK) unified ideographs (unified East Asian ideographs)

             O-zone: reserved for future standardization

             R-zone: restricted use zone (private use characters, presentation forms, compatibility characters, etc.)

그림 1. BMP(Basic Multilingual Plane)

 

2.1.4     Group

4 Byte code UCS-4에서 윗자리 8bit 값은 같고, 나머지 아래자리 24bit 다른 codeword 집합을 group이라고 한다. group 하나에는 codeword 16M(1600) 만큼 있게 되며, group 번호는 hexadecimal 두개로 나타낸다. 우리가 알고 있는 대부분의 codeword 거의 모두 G=00이다.

 

2.2      ISO/IEC 10646-1

2.2.1     UCS-4 : 4Byte code

ISO/IEC 10646 표준의 이름은 Universal Multiple-Octet Coded Character Set (UCS)이다. UCS 처음에 4Byte code 시작하였는데, 이를 UCS-4라고 한다. , 글자는 4byte 쓰며, 따라서 hexadecimal 8개로 나타낸다.

한글 첫가끝 조합형 첫소리 글자 codeword 0x0000 1100이고, 가운뎃소리

'JAVA > JSP_Servlet' 카테고리의 다른 글

[펌] 메일 헤더 구성 요소  (0) 2005.01.17
[펌] [MAIL] MIME 관련  (0) 2005.01.17
[펌] http RFC  (0) 2005.01.17
[펌]Single Sign On...  (0) 2005.01.17
JSP 다운로드시 헤더 ㅡㅡ  (0) 2005.01.13
Posted by tornado
|

[펌] http RFC

JAVA/JSP_Servlet 2005. 1. 17. 21:30

RFC 2068 - Hypertext Transfer Protocol -- HTTP/1.1
rfc2068 :하이퍼텍스트 전송규약 1.1표준(안)
출처 : 한국전자통신연구소요약하이퍼텍스트 전송 규약(HTTP)은 분산 정보 시스템, 종합 정보시스템 및 하이퍼미디어 정보시스템에서사용하는 응용 계층 규약으로서 요구 방법의 확장을 통해서 네임 서버와 분산 객체 관리 시스템과 같은수많은 작업에 사용될 수 있는 보편적인 객체 지향형 규약이다. HTTP는 어떤 문서의 데이터 표현 형식을규정하고 협상하여 전송 중인 데이터와 무관하게 시스템을 구축할 수 있게 한다.HTTP는 1990년 이후 World-Wide Web 범 세계 정보 이니셔티브에 의하여 사용되고 있다.이 규격은 "HTTP/1.1"로 언급되는 규약을 정의하고 있다.
목차 1. 도입 1.1 목적 1.2 필요 사항 1.3 용어 1.4 전반적인 운영 2. 기호 관례 및 일반적인 문법 2.1 추가된 BNF 2.2 기본적인 규칙 3. 규약 파라미터 3.1 HTTP 버전 3.2 보편적 자원 식별자(Uniform Resource Identifiers) 3.2.1 일반적 형식 3.2.2 http URL 3.2.3 URI 비교 3.3 날짜/시간 형식 3.3.1 완전한 날짜 3.3.2 Delta Seconds 3.4 문자 집합 3.5 내용 코딩 (Content Coding) 3.6 전송 코딩 (Transfer Coding) 3.7 미디어 타입 3.7.1 정형화(Canonicalization) 및 텍스트 기본값 3.7.2 Multipart Type 3.8 제품 토큰 3.9 품질 등급 값 3.10 언어 태그 3.11 엔터티 태그 3.12 영역 단위 4. HTTP 메시지 4.1 메시지 유형 4.2 메시지 헤더 4.3 메시지 본문 4.4 메시지 길이 4.5 일반적인 헤더 필드 5. 요구 5.1 Request-Line 5.1.1 Method 5.1.2 Request-URI 5.2 요구에 의해 식별되는 자원 5.3 요구 헤더 필드 6. 응답 6.1 상태-라인 6.1.1 상태 코드 및 이유 문구 6.2 응답 헤더 필드 7 엔터티(Entity) 7.1 엔터티 헤더 필드 7.2 엔터티 본문 7.2.1 유형 7.2.2 길이 8. 접속 8.1 지속적인 접속 8.1.1 목적 8.1.2 전반적인 운영 8.1.3 프락시 서버(Proxy Servers) 8.1.4 실제적인 고려 사항 8.2 메시지 전송 필요 조건 9. Method 정의 9.1 안전 및 멱등원(冪等元) method 9.1.1 안전 method 9.1.2 멱등원 method 9.2 OPTIONS 9.3 GET 9.4 HEAD 9.5 POST 9.6 PUT 9.7 DELETE 9.8 TRACE 10. 상태 코드 정의 10.1 Informational 1xx (정보를 알려 주는 1xx) 10.1.1 100 Continue (계속) 10.1.2 101 Switching Protocols (규약 전환) 10.2 Successful 2xx (성공을 알려 주는 2xx) 10.2.1 200 OK 10.2.2 201 Created (생성 되었음) 10.2.3 202 Accepted (접수 되었음) 10.2.4 203 Non-Authoritative Information (비 인증 정보) 10.2.5 204 No Content (내용이 없음) 10.2.6 205 Reset Content (내용을 지움) 10.2.7 206 Partial Content (부분적 내용) 10.3 Redirection 3xx (방향을 재설정하는 3xx) 10.3.1 300 Multiple Choices (복수 선택) 10.3.2 301 Moved Permanently (영구 이동) 10.3.3 302 Moved Temporarily (임시 이동) 10.3.4 303 See Other (다른 것을 참조) 10.3.5 304 Not Modified (변경되지 않았음) 10.3.6 305 Use Proxy (프락시를 사용할 것) 10.4 Client Error 4xx (클라이언트 에러 4xx) 10.4.1 400 Bad Request (잘못된 요구) 10.4.2 401 Unauthorized (인증되지 않았음) 10.4.3 402 Payment Required (요금 지불 요청) 10.4.4 403 Forbidden (금지되었음) 10.4.5 404 Not Found (찾을 수 없음) 10.4.6 405 Method Not Allowed (Method를 사용할 수 없음) 10.4.7 406 Not Acceptable (접수할 수 없음) 10.4.8 407 Proxy Authentication Required (프락시 인증이 필요함) 10.4.9 408 Request Timeout (요구 시간 초과) 10.4.10 409 Conflict (충돌) 10.4.11 410 Gone (내용물이 사라졌음) 10.4.12 411 Length Required (길이가 필요함) 10.4.13 412 Precondition Failed (사전 조건 충족 실패) 10.4.14 413 Request Entity Too Large (요구 엔터티가 너무 큼) 10.4.15 414 Request-URI Too Long (Request-URI이 너무 김) 10.4.16 415 Unsupported Media Type (지원되지 않는 media type) 10.5 Server Error 5xx (서버 에러 5xx) 10.5.1 500 Internal Server Error (서버 내부 에러) 10.5.2 501 Not Implemented (구현되지 않았음) 10.5.3 502 Bad Gateway (불량 게이트웨이) 10.5.4 503 Service Unavailable (서비스를 사용할 수 없음) 10.5.5 504 Gateway Timeout (게이트웨이 시간 초과) 10.5.6 505 HTTP Version Not Supported (지원되지 않는 HTTP 버전) 11. 접속 인증 11.1 기본 인증 scheme 11.2 요약 인증 scheme 12. 내용 협상(Content Negotiation) 12.1 서버가 주도하는 협상 12.2 에이전트가 주도하는 협상 12.3 투명한 협상(Transparent Negotiation ) 13. HTTP에서의 캐시 13.1.1 캐시의 정확성 13.1.2 경고 13.1.3 캐시-제어 메커니즘 13.1.4 명백한 사용자 에이전트 경고 13.1.5 규칙 및 경고의 예외 사항 13.1.6 클라이언트가 제어하는 행태 13.2 유효일 모델 13.2.1 서버가 명시한 만기일 13.2.2 스스로 만기일을 찾음(Heuristic Expiration) 13.2.3 경과 시간 계산(Age Calculations) 13.2.4 만기일 계산 13.2.5 만기일 값을 명확하게 하기 13.2.6 복수의 응답을 명확하게 하기 13.3 검증 모델 13.3.1 최종 갱신 날짜 13.3.2 엔터티 태그 캐시 검증자(Validators) 13.3.3 약한/강한 검증자 13.3.4 엔터티 태그와 최종 갱신 날짜를 사용할 때를 결정하는 규칙 13.3.5 검증을 하지 않는 조건 법 13.4 응답을 캐시할 수 있는 정도(Cachability) 13.5 캐시에서 응답을 구축하기 13.5.1 End-to-end 및 Hop-by-hop 헤더 13.5.2 변경할 수 없는 헤더 13.5.3 헤더의 결합 13.5.4 바이트 영역(Byte Ranges)의 결합 13.6 협상을 통한 응답을 캐시하기 13.7 공유/비공유 캐시 13.8 에러 또는 불완전한 응답 캐시 행태 13.9 GET 및 HEAD의 부작용 13.10 갱신 또는 삭제 후의 무효화 13.11 의무적으로 서버를 통하여 기입 13.12 캐시 대체 13.13 History List 14. 헤더 필드 정의 14.1 Accept 14.2 Accept-Charset 14.3 Accept-Encoding 14.4 Accept-Language 14.5 Accept-Ranges 14.6 Age 14.7 Allow 14.8 Authorization 14.9 Cache-Control 14.9.1 무엇을 캐시할 수 있는가 14.9.2 캐시에 의해 무엇을 저장할 수 있는가 14.9.3 기본적인 만기일 메커니즘의 변경 14.9.4 캐시의 재검증 및 Reload 제어 14.9.5 비 변경 지시어 14.9.6 캐시 제어 확장 14.10 Connection 14.11 Content-Base 14.12 Content-Encoding 14.13 Content-Language 14.14 Content-Length 14.15 Content-Location 14.16 Content-MD5 14.17 Content-Range 14.18 Content-Type 14.19 Date 14.20 ETag 14.21 Expires 14.22 From 14.23 Host 14.24 If-Modified-Since 14.25 If-Match 14.26 If-None-Match 14.27 If-Range 14.28 If-Unmodified-Since 14.29 Last-Modified 14.30 Location 14.31 Max-Forwards 14.32 Pragma 14.33 Proxy-Authenticate 14.34 Proxy-Authorization 14.35 Public 14.36 Range 14.36.1 Byte Ranges 14.36.2 Range Retrieval Requests 14.37 Referer 14.38 Retry-After 14.39 Server 14.40 Transfer-Encoding 14.41 Upgrade 14.42 User-Agent 14.43 Vary 14.44 Via 14.45 Warning 14.46 WWW-Authenticate 15. 보안에 대한 고려 사항 15.1 클라이언트의 인증 15.2 인증 scheme을 선택할 수 있도록 함 15.3 서버 로그 정보의 남용 15.4 민감한 정보의 전송 15.5 파일 및 경로 이름에 기초한 공격 15.6 개인적인 정보 15.7 Accept 헤더와 연결된 사생활 보호의 이슈 15.8 DNS Spoofing(속이기) 15.9 Location 헤더와 Spoofing(속이기) 16. 감사의 말 17. 참고 문헌 18. 저자의 주소 19. 부록 19.1 Internet Media Type message/http 19.2 Internet Media Type multipart/byteranges 19.3 Tolerant Applications 19.4 HTTP 엔터티와 MIME 엔터티의 차이점 19.4.1 규범적인 폼으로 변환 19.4.2 날짜 형식의 변환 19.4.3 Content-Encoding 소개 19.4.4 No Content-Transfer-Encoding 19.4.5 Multipart Body-Part의 HTTP 헤더 필드 19.4.6 Transmit-Encoding 소개 19.4.7 MIME-Version 19.5 HTTP/1.0 이후 변경 사항 19.5.1 복수의 홈을 가진 웹 서버를 단순하게 하기 위한 변경 사항 및 IP 주소 보존 19.6 추가 기능 19.6.1 추가적인 요구 method 19.6.2 추가 헤더 필드 정의 19.7 이전 버전과의 호환성 19.7.1 HTTP/1.0 지속적인 연결과의 호환성
1. 서론1.1 목적하이퍼텍스트 전송 규약(HTTP)은 분산 정보시스템, 종합 정보시스템 및 하이퍼미디어 정보시스템에서사용하는 응용계층의 규약이다. HTTP는 1990년 이후 World-Wide Web 범 세계 정보 이니셔티브에의하여 사용되고 있다. "HTTP/0.9"로 언급되는 HTTP의 첫 버전은 인터넷 상에서 저장되어 있는 원래데이터(raw data)를 전송하기 위한 단순한 규약이었다. RFC 1945 [6]이 규정한 HTTP/1.0은 메시지를전송되는 문서 데이터에 대한 메타 정보 및 요구/응답 용어의 변경자를 포함하는 MIME과 유사한메시지의 형식으로 사용할 수 있도록 함으로써 규약을 향상시켰다. 그러나 HTTP/1.0은 계층적 프락시(hierarchical proxies), 캐시, 지속적인 연결의 필요성 및 가상 호스트(virtual host) 등의 영향을충분히 고려하지 않았다. 또한 "HTTP/1.0"을 지원한다고 하면서도 규약의 불완전 구현 및 오해에 의한잘못된 구현 등에 의해 응용 프로그램 사이에 종종 문제가 발생하였기에 상호 협상할 수 있는 응용프로그램이 상대방의 진정한 성능을 파악할 수 있도록 규약 버전을 갱신할 필요가 생겼다. 이 규격은 "HTTP/1.1"로 불리우는 하이퍼텍스트 전송 규약을 정의한다. 이 규약은 기능을 신뢰할 수있도록 구현하기 위해 HTTP/1.0보다 더 엄격한 필요 조건을 포함하고 있다.실제적인 정보 시스템은 단순한 조회보다 검색, 프런트-엔드(front-end) 갱신 및 주석 달기 등 많은기능을 필요로 한다. HTTP는 요구의 목적을 표시하는 일련의 개방된 method를 (open-ended setof methods) 허용한다. 이 규약은 보편적 자원 식별자(URI) [3][20], 자원 위치 (URL) [4] 또는 자원이름(URN)이 제공하는 참고 방법에 따라 method를 적용할 자원을 지칭하는 데 사용한다.메시지는 다용도 인터넷 메일 확장(MIME)에서 정의된 것처럼 인터넷 메일에서 사용되는 것과 유사한형식으로 전송된다. HTTP는 사용자 에이전트, 프락시/게이트웨이와 SMTP [16], NNTP [13], FTP [18], Gopher [2], 및 WAIS [10] 등을 지원하는 다른 인터넷 시스템 사이의 통신을 위한 범용 규약으로서 사용된다. 이러한 방식으로 HTTP는 기본적인 하이퍼미디어가 다양한 애플리케이션의 자원에 접근할 수 있도록한다. 1.2 필요 조건이 규격은 각각의 특별한 필요 조건의 중요도를 정의할 때 RFC 1123 [8]와 동일한 용어를 사용한다.이러한 용어는 다음과 같다. MUST이 단어 또는 "요구된"이라는 형용사는 해당 항목이 규격의 절대적인 필요 조건임을 의미한다. SHOULD이 단어 또는 "추천된"이라는 형용사는 특정 상황에서 해당 항목을 무시할 합당한 이유가 있을 수있다는 것을 의미한다. 그러나 충분히 함축적 의미를 이해해야 하고 다른 방법을 선택하기 전에 사례를충분히 검토해야 한다. MAY이 단어 또는 "선택적"이라는 형용사는 해당 항목이 진정으로 선택적이라는 것을 의미한다.한 판매회사는 특정 항목을 특정 시장이 요구하기 때문에 또는 예를 들어 제품의 기능을 향상시켜 주기때문에 다른 판매 회사와 달리 동일한 항목을 포함할 수 있다.구현 방법이 하나 또는 그 이상의 MUST 규약 필요 조건을 충족시켜 주지 못하면 규약에 따르지 않는것이다. 구현 방식이 모든 MUST 및 SHOULD 필요 조건을 충족한다면 "무조건적으로 충족한다"고 할 수있고, 모든 MUST 필요 조건을 충족하지만 모든 SHOULD 필요 조건을 충족하지 못한다면 "조건적으로충족한다"고 할 수 있다. 1.3 용어이 규격은 HTTP 통신의 참여자 및 객체가 수행하는 역할을 지칭하는 몇몇 용어를 사용하고 있다. connection(연결)통신을 목적으로 두 프로그램 간에 설정된 전송 계층의 가상적 회로 message(메시지)HTTP 통신의 기본 전송 단위. 4 장에 규정된 의미론을 따르는 구조적인 데이터 표현 형태이며, 일련의8 비트(octets)로 구성되어 있고 연결을 통하여 전송된다. request(요구) 5 장에 규정된 HTTP 요구 메시지. response(응답) 5 장에 규정된 HTTP 응답 메시지. resource(자원)3.2절에 규정되어 있는 URI에 의하여 식별되는 네트워크 데이터 객체 또는 서비스. 자원은 다양한 표현형태(예를 들어 언어, 데이터 형식, 크기 및 해상도)를 지닐 수 있으며 다양한 방법으로 변형될 수 있다. entity(엔터티)요구나 응답 메시지의 페이로드(payload)로서 전송되는 정보. 엔터티는 7 장에서 설명된 대로 Entity-Header필드 형태의 메타 정보 및 Entity-Body 형태의 내용으로 구성되어 있다. representation(표현)12 장에서 기술한 내용 협상의 통제를 따르는 응답에 포함된 엔터티. 특정한 응답 상태와 연관된 다수의표현 방법이 있을 수 있다. content negotiation(내용 협상)12 장에서 기술한 대로 요구를 처리할 때 적절한 표현 방법을 선택하는 메커니즘. 어떠한 응답에서는엔터티의 표현은 협상할 수 있다.(에러 응답 포함) variant(변형자)자원은 특정한 경우에 자원과 관련된 하나 이상의 표현 방식을 가질 수 있다. 이러한 각각의 표현 방식을"변형자"라고 부른다. "변형자"라는 용어를 사용한다고 해서 자원이 반드시 내용 협상의 대상인 것은아니다. client(클라이언트)요구 메시지를 전송할 목적으로 연결을 설정하는 프로그램. user agent(사용자 에이전트)요구 메시지를 시작하는 클라이언트. 이것은 종종 브라우저, 편집기, 스파이더(웹을 탐색하는 로봇) 또는다른 사용자 툴(tool)일 수 있다. server(서버)요구 메시지를 처리하기 위해 접속을 수신하는 애플리케이션으로서 응답 메시지를 전송한다.어떤 프로그램이든 동시에 클라이언트와 서버가 될 수 있다. 이 규격에서 이 용어를 사용하는 것은프로그램의 일반적인 능력을 참조하기보다는 특정한 연결을 위해 프로그램이 수행하는 역할만을 참조하는것이다.마찬가지로 어떠한 서버도 원서버, 프락시, 게이트웨이, 터널 등 각 요구의 성격에 따라 동작을 전환하는역할을 할 수 있다. origin server(원서버)해당 자원이 보관되어 있거나 자원을 생성할 수 있는 서버. proxy(프락시)다른 클라이언트를 대신하여 요구를 작성할 목적으로 서버와 클라이언트의 역할을 모두 수행하는 중간프로그램. 요구는 내부적으로 처리되어 가능하면 해석되어 다른 서버로 전달된다. 프락시는 이 규격의클라이언트와 서버의 필요 조건을 모두 구현해야만 한다. gateway(게이트웨이)다른 서버를 위해 중간 역할을 하는 서버. 프락시와는 달리 게이트웨이는 요구 메시지를, 요청받은자원을 서비스하는 최종적인 원서버처럼 수신한다. 요구한 클라이언트는 자신이 게이트웨이와 통신하고있다는 것을 알지 못할 수 있다. tunnel(터널)두 연결 사이를 무조건 중계하는 역할을 하는 중간 프로그램. 활성화되면 비록 HTTP 요구에 의하여시작되지만 터널은 HTTP 통신의 참여자로 간주되지 않는다. 터널은 중계하고 있는 양 쪽의 연결이종결되면 사라진다. cache(캐시)프로그램이 응답 메시지를 저장하는 로컬 저장소. 메시지 보관, 조회 및 삭제를 제어하는 하부 시스템이기도하다. 캐시는 응답 시간, 향후 네트워크 대역폭 소모 및 동일한 요구를 감소시킬 목적으로 캐시할수 있는 응답을 저장한다. 어떤 클라이언트나 서버도 캐시를 포함할 수 있다. 단지 터널 역할을 하는서버는 캐시를 사용할 수 없다. cachable(캐시할 수 있는)응답 메시지의 사본을 저장하여 계속적인 요구 응답에 사용할 수 있으면 응답을 캐시할 수 있다고 한다.HTTP 응답의 캐시 가능 여부를 결정하는 원칙은 13 장에 정의되어 있다. 자원을 캐시할 수 있다 하더라도 캐시가 특정 요구에 대하여 캐시 된 사본을 사용할 수 있는지 여부에대한 추가적인 제한 사항이 있을 수 있다. first-hand(직접)응답이 직접적으로 오며 원서버로부터 하나 또는 그 이상의 프락시를 거쳐옴으로써 발생하는 불필요한지연이 없을 경우 응답이 직접 온다고 할 수 있다. 또한 검증이 원서버에서 직접 이루어진다면 응답이직접 온다고 할 수 있다. explicit expiration time(명백한 유효 시간)원서버가 추가적인 검증 없이는 캐시에 의해 엔터티를 더 이상 되돌려 주지 않기로 한 시간. 즉, 원서버가캐시된 데이터의 유효성을 보장할 수 있는 시간. heuristic expiration time(자동으로 설정되는 유효 시간)분명한 유효 시간이 설정되어 있지 않을 때 캐시가 할당하는 유효 시간 age(경과 시간)응답 메시지의 경과 시간은 원서버로부터 전송된 후, 또는 성공적으로 검증된 후의 시간. freshness lifetime(신선한 기간)응답의 생성 시점과 유효시간 만기 시점 사이의 시간 길이 fresh(신선한)응답의 경과 시간이 신선한 기간을 넘어서지 않았을 때 응답이 신선하다고 할 수 있다. stale(낡은)응답의 경과 시간이 신선한 기간을 넘어섰다면 응답이 낡았다고 할 수 있다. semantically transparent(의미상으로 분명한)성능을 향상시키고자 하는 목적을 제외하고 캐시의 사용이 요구하는 클라이언트나 원서버에 영향을미치지 않을 때 특정한 요구에 대하여 캐시가 "의미상으로 분명하게" 작동한다고 할 수 있다. 캐시가의미상으로 분명할 때 클라이언트는 원서버가 직접 처리했을 때와 완전히 동일할 응답을 수신하게된다.( hop-by-hop 헤더는 제외). validator(검증자)캐시 엔트리가 엔터티의 복사본과 동일한지 알아내는 데 사용하는 규약 요소(예를 들면 엔터티태그나 Last-Modified 시간)1.4 Overall OperationHTTP 규약은 요구/응답 규약이다. 클라이언트는 요구 method, URI, 규약 버전의 형태로 서버에 요구메시지를 전송한다. 요구 변경자, 클라이언트 정보, 서버와의 접속에 사용되는 본문 내용을 포함하는MIME 유형의 메시지가 뒤따른다. 서버는 메시지의 규약 버전 및 성공 또는 실패 코드를 포함하는 상태정보로서 응답한다. 서버 정보, 엔터티 메타 정보, Entity-Body 내용을 포함하는 MIME 유형의 메시지도뒤따른다.대부분의 통신은 사용자 에이전트가 구동하며 특정 원서버에 적용할 요구로 구성되어 있다. 가장 단순한 경우 이것은 사용자 에이전트(UA)와 원서버(O) 사이의 단일 접속(v)에 의해 성취할 수있을 것이다. request chain ---------------------->UA ---------------- v ------------------- O <--------------------- response chain좀 더 복잡한 상황은 Request/Response chain에 하나 또는 그 이상의 중간 매개자가 있는 경우이다.프락시, 게이트웨이 및 터널의 세 가지 일반적인 중간 매개 형태가 있다. 프락시는 전송 에이전트로절대 표현 형태의 URI 요구를 수신하여 메시지의 전체 혹은 부분을 재작성한 후 URI가 표시하는 서버로재구성된 요구 메시지를 전달한다. 게이트웨이는 수신 에이전트로 다른 서버 위의 계층 역할을 수행하며필요하다면 원서버의 규약에 맞도록 요구를 해석하기도 한다. 터널은 메시지를 변경하지 않고 두 연결지점을 연결하는 중계역할을 수행한다. 터널은 통신(communication)이 중간 매개자가 메시지의 내용을이해할 수 없을 때라도 방화벽과 같은 중간 매개자를 통과할 필요가 있을 때 사용한다. request chain ------------------------------------->UA -----v----- A -----v----- B -----v----- C -----v----- O <------------------------------------ response chain위의 도표는 사용자 에이전트와 원서버 사이의 세 중간 매개자(A, B 및 C)를 보여 준다. 전체 고리를통과하는 요구 또는 응답 메시지는 네 개의 별도 연결을 통과하게 된다. 몇몇 HTTP 통신 선택 사항은최고 근접 거리의 비터널 이웃과의 통신, 연쇄적 연결 고리의 마지막 부분에만 또는 연결 고리에 따르는모든 연결에 적용되기 때문에 이러한 구분은 중요하다. 그림이 선형이지만 각 참여자는 복수의 동시통신에 참여할 수 있다. 예를 들어 B는 A의 요구를 처리함과 동시에 A를 제외한 복수의 클라이언트요구를 수신하고/수신하거나 C 이외의 서버에게 요구를 전송할 수 있다.터널 역할을 수행하는 것이 아닌 통신에 참여하는 어떤 것이라도 요구를 처리할 때 내부 캐시를 사용할수 있다. 캐시의 효과는 연결 고리를 따라 참가자 중 하나가 해당되는 요구에 적용할 수 있는 캐시된응답을 갖고 있다면 Request/Response chain이 짧아진다. 다음은 UA 또는 A가 캐시하지 않은요구에 대한 O (C를 경유) 초기 응답의 사본을 B가 가지고 있을 때의 결과 고리를 설명하고 있다. request chain --------->UA -----v----- A -----v----- B - - - - - - C - - - - - - O <-------- response chain보통 모든 응답을 캐시할 수 있는 것은 아니며 어떤 요구는 캐시 방식에 특별 요구를 하는 변경자를포함할 수 있다. 13 장에 캐시 방식과 캐시할 수 있는 응답에 대한 필요 조건이 기록되어 있다.사실상 World Wide Web에는 현재 실험되고 있거나 배포되고 있는 캐시와 프락시의 다양한 아키텍쳐와환경설정 방법이 있다. 이러한 것 중에는 대륙간 대역폭을 절약하기 위한 프락시 캐시의 국가적 계층,캐시 엔트리를 배포하거나 복수로 배포하는 시스템, CD-ROM 등을 통하여 캐시 된 데이터의 하부세트를 배포하는 조직 등이 있다. HTTP 시스템은 광대역 연결을 통한 기업 인트라넷, 저동력 무선연결의 PDA를 통한 연결 및 간헐적인 연결에 사용된다. HTTP/1.1의 목적은 고도의 신뢰성과 신뢰성을확보할 수 없다면 신뢰할 수 있는 실패의 표시 기능을 지닌 웹 응용프로그램을 개발하는 개발자의요구를 충족하는 규약 구조물을 새로 소개하면서도 이미 배포된 다양한 환경을 지원하는 것이다.HTTP 통신은 대개 TCP/IP 연결을 통하여 이루어진다. 기본 포트는 TCP 80 이지만 다른 포트를 사용할수도 있다. 그러나 이것은 HTTP가 인터넷 상의 다른 규약이나 다른 네트워크 위에서 구현될 수 없게하는 것은 아니다. HTTP는 단순히 신뢰할 수 있는 전송 수단을 가정할 뿐이며 이러한 보장을 해 줄 수있는 어떠한 규약을 사용해도 된다. HTTP/1.1의 요구 응답 구조를 적용하고자 하는 규약의 전송 데이터단위로 배치(mapping)하는 것은 이 규격의 범위 밖의 것이다.HTTP/1.0에서 대부분의 구현 방식은 각각의 요구/응답 교환에 새로운 접속을 사용하는 것이다.또한 HTTP/1.1에서는 하나의 접속을 하나 또는 그 이상의 요구/응답 교환에 사용할 수 있으나 연결이여러 가지 이유로 단절될 수 있다.( 8.1 절 참조)
2. 기호 관례 및 일반적인 문법2.1 추가된 BNF이 문서에서 명시된 모든 메커니즘은 설명형 문구로서 RFC 822 [9]에서 사용한 것과 유사한 추가된Backus-Naur Form (BNF)으로 설명되어 있다. 구현자는 이 규격을 이해하기 위하여 이러한 기호에익숙할 필요가 있다. 추가된 BNF는 다음의 구성 요소를 포함한다.name = definition규칙의 이름이 이름 그 자체(둘러싸는 "<" 및 ">"이 없는)이며 정의 부분과는 등호 문자("=")로 구별된다.계속되는 공백 문자는 규칙에 대한 규정이 한 줄 이상에 걸쳐 있음을 표시하는 들여쓰기의 경우에만의미가 있다. SP, LWS, HT, CRLF, DIGIT, ALPHA 등과 같은 몇몇 기본 규칙은 대문자로만 사용한다.정의문 내에서 소괄호는 규칙 이름의 사용 구별을 용이하게 해줄 경우에는 언제든지 사용한다."literal"인용 부호로 문자 텍스트 주위를 감싼다. 별도의 언급이 없으면 문자는 대소문자를 구별한다.rule1 | rule2막대 ("|")로 구분된 요소는 선택 사항이다. 예를 들어 "yes |no" 는 yes 나 no어느 것이든 가능하다.(rule1 rule2)괄호로 둘러싼 요소는 단일 요소로 취급한다. 따라서 "(elem (foo | bar) elem)"는 "elem foo elem"및 "elem bar elem"의 토큰 순서를 허용한다.*rule이것은 반복을 의미하는 것으로서 뒤이어서 나올 #rule과 혼동을 일으키는 표현 방식이므로 유의해야한다. 반복을 통해 이루어지는 결과는 하나의 단어나 수와 같이 한 개 요소의 표현 형태로 되는 것이며,#rule에서는 똑같은 반복이지만 여러 개 단어나 수의 열 형태와 같이 여러 개 요소의 나열 형태로 표현되는 것이다. *element와 같은 표기 방법으로 쓰인다. 이것은 적어도 n개와 최대 m개의 요소로구성되는 한 가지 결과를 의미한다. 즉, 1*2DIGIT 라는 표현은 숫자가 적어도 한 개 최대 두 개로 구성되어 한 개의 수를 나타낸다는 뜻이다. 4는 한 가지 예이며, 45도 한 가지 예가 된다. 그러나 345의 경우에는 숫자 세 개로 구성된 한 개 요소이므로 최대 갯수에 위배되어 적합하지 않다.n과 m은 생략될 수 있으며, 이 경우에 n의 기본값은 0이고 m의 기본값은 무한대이다. 그러므로 "*(element)"는 0개를 포함해서 어떤 개수라도 가능하고, "1*element"의 경우는 한 요소의표현에 있어 적어도 한 개는 있어야 하며 최대 갯수에는 제한이 없다.[rule]대괄호는 선택 요소를 둘러 싼다. "[foo bar]" 는 "*1(foo bar)"와 동일하다.N rule특정 횟수의 반복을 나타낸다. "(element)" 은 "*(element)"와 동일하다. 즉 요소(element)가 정확하게 번 표시된다. 따라서 2 DIGIT 는 2 자리 숫자, 3 ALPHA 는 세 개의알파벳 문자로 구성된 문자열이다.#rule앞서 설명한 것처럼 반복을 나타내긴 하지만 요소들의 나열로서 표현되는 것이다. 즉, 1#DIGIT 라고하면 여러 개의 수로 구성된 수열로서 표현되는데, 최소 한 개의 수는 있어야 하고 최대 갯수는 제한이없는 수열이 된다. 각 요소들 사이의 구분은 ","와 LWS를 이용하는데, 여러 개의 나열 형태를 쉽게표현할 수 있게 해준다. 예를 들어, (*LWS element *(*LWS "," *LWS element)) 이것을 간단하게1#element 이와 같이 표현할 수 있다. 또 다른 예를 들자면, 1#2(2DIGIT)이것은 숫자 두 개로 구성된수가 적어도 한 개가 있어야 하며 최대 두 개까지 가능하다는 것이다. 즉, 23 이렇게 표현될 수도 있고, 23, 56 이렇게 두 개로 표현될 수도 있다. 이것이 *rule과의 차이점이고, #rule 에서도 "#element" 의구성이 그대로 성립한다. 이에 대한 설명은 *rule 의 경우와 같다. ","를 이용하여 나열함에 있어, nullelement가 허용된다. 예를 들어, 1#3(2DIGIT)과 같은 표현식에 대해23, , 56, 34 이렇게 null element표시가 가능하지만, 실제 갯수는 세 개로서 간주된다. 따라서 최소 한 개 최대 세 개의 제한에 위배되지않는다.; comment규칙 문장에서 오른쪽으로 약간 떨어져 있는 세미콜론은 해당 라인의 끝에까지 계속되는 주석의 시작을의미한다. 이것은 규격과 병행하여 적절한 설명을 포함시키기 위한 방법이다.implied *LWS두 개의 인접한 단어 (token or quoted-string) 또는 인접한 토큰(tokens)과 식별자 (tspecials) 사이에LWS (linear whitespace)가 포함될 수 있다. 여기서 두 개의 토큰 사이에는 반드시 적어도 하나의식별자가 존재하여 각기 하나의 토큰으로 간주되지 않게끔 구별되어야 한다.2.2 기본 규칙다음의 규칙은 기본적인 분석 구조를 설명하기 위해 이 규격 전반에 걸쳐 사용되고 있다. US-ASCII로코드화 된 문자 집합은 ANSI X3.4-1986 [21]에 의하여 규정되었다. OCTET = <모든 8-bit 연속 데이터> CHAR = <모든 US-ASCII 문자 (octets 0 - 127)> UPALPHA = <모든US-ASCII 대문자 "A".."Z"> LOALPHA = <모든 US-ASCII 소문자 "a".."z"> ALPHA = UPALPHA | LOALPHA DIGIT = <모든 US-ASCII 숫자 "0".."9"> CTL = <모든 US-ASCII 제어 문자 (octets 0 - 31) 및 DEL (127)> CR = <US-ASCII CR, 캐리지 리턴(13)> LF = <US-ASCII LF, 라인피드 (10)> SP = <US-ASCII SP, 스페이스 (32)> HT = <US-ASCII HT, 수평 탭 (9)> <"> = <US-ASCII 이중 인용 부호(34)>HTTP/1.1은 연속적인 CR LF를 Entity-Body를 (부록 19.3 참조) 제외한 모든 규약 요소의 라인 마감부호로 정의한다. Entity-Body 내에서의 라인 마감 부호는 3.7 절에서 설명된 것처럼 연관된 mediatype에 의하여 정의한다. CRLF = CR LFHTTP/1.1 헤더는 계속되는 라인이 스페이스나 수평 탭으로 시작한다면 복수의 라인에 걸쳐 계속작성할 수 있다. 폴딩(folding)을 포함한 모든 선형 공백 스페이스는 SP와 동일한 의미를 가진다. LWS = [CRLF] 1*( SP | HT )TEXT 규칙은 메시지 분석기가 해석하지 않도록

'JAVA > JSP_Servlet' 카테고리의 다른 글

[펌] [MAIL] MIME 관련  (0) 2005.01.17
[펌] 코드시스템및 한글문자체계  (0) 2005.01.17
[펌]Single Sign On...  (0) 2005.01.17
JSP 다운로드시 헤더 ㅡㅡ  (0) 2005.01.13
헐.. 죽이넹  (0) 2004.12.21
Posted by tornado
|

'JAVA > JSP_Servlet' 카테고리의 다른 글

[펌] 코드시스템및 한글문자체계  (0) 2005.01.17
[펌] http RFC  (0) 2005.01.17
JSP 다운로드시 헤더 ㅡㅡ  (0) 2005.01.13
헐.. 죽이넹  (0) 2004.12.21
얼... 좋은걸~  (0) 2004.12.17
Posted by tornado
|

이거 원 만들때 마다 새로 만드니 당췌ㅡㅡ

 

Tomcat 4.1.30 + window XP SP2 단독 테스트

Tomcat 4.1.24 + Apache 2.0.43(맞나??) + linux  테스트.

 

IE 6.0 / Firefox 만 딱 두번씩 테스트 했네 ㅡㅡ

 


package com.javarush.util;

 

import java.io.File;
import java.io.FileInputStream;
import java.io.OutputStream;
import java.nio.channels.Channels;
import java.nio.channels.FileChannel;
import java.nio.channels.WritableByteChannel;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

 

/**
 * @author Tornado
 */
public class DownloadUtil {
   
    private File file;
    private HttpServletRequest req;
    private HttpServletResponse res;
   
    public DownloadUtil(){   }
   
    public DownloadUtil(File file, HttpServletRequest req, HttpServletResponse res ){
       
        this.file = file;
        this.req = req;
        this.res = res;
       
    }
     
    public void sendFile() throws Exception{
       
        if(!(file.isFile() && file.exists())){
           
            throw new Exception("file.not.found");
           
        }
 
        if(!initResponse()){
           
            throw new Exception("download.failure");
           
        }
       
        FileInputStream fin = null;
       
        FileChannel inputChannel = null;
       
        WritableByteChannel outputChannel = null;
       
        try{
           
            fin = new FileInputStream(file);
           
            inputChannel = fin.getChannel();
           
            outputChannel = Channels.newChannel(res.getOutputStream());
           
            inputChannel.transferTo(0, fin.available(), outputChannel);
           
        }catch(Exception e){
           
            throw e;
           
        }finally{
           
            try{
               
                if(fin != null) fin.close();
            
             if(inputChannel.isOpen()) inputChannel.close();
            
             if(outputChannel.isOpen()) outputChannel.close();
            
            }catch(Exception e){
               
            }
           
        }
       
    }
       
    public boolean initResponse() throws Exception{
        
        if(res == null || req == null) return false;
       
        if(file == null)  return false;
       
        res.setContentType("appliction/octet-stream");
       
        res.setHeader("Accept-Ranges","bytes");
       
  if (req.getHeader("User-Agent").indexOf("MSIE 5.5") > -1) {
      // IE 6.0 Test Ok
   res.setHeader("Content-Disposition", "filename=" + java.net.URLEncoder.encode(file.getName(), "UTF-8") + ";");
   
  } else if(req.getHeader("User-Agent").indexOf("Mozilla/5.0") > -1){
      // firefox Test Ok
      if(req.getServerPort() == 8080){
         
          res.setHeader("Content-Disposition", "filename=" + new String(file.getName().getBytes("KSC5601"), "ISO-8859-1") + ";");
         
      }else{
         
          res.setHeader("Content-Disposition", "filename=" + new String(file.getName().getBytes(), "KSC5601" ) + ";");
         
      }
     
  } else {
     
   res.setHeader("Content-Disposition", "attachment; filename=" + java.net.URLEncoder.encode(file.getName(), "UTF-8") + ";");
   
  }
  
  res.setContentLength((int)file.length());
       
        return true;
       
    }
   
}

 

 

사용법 :


<%@ page contentType="text/html; charset=euc-kr" import="java.io.*, java.net.*, com.javarush.util.*" %><%
        request.setCharacterEncoding("euc-kr");
        String fileName = request.getParameter("fileName");

        if(fileName != null){
                //fileName = URLDecoder.decode(fileName, "euc-kr");

                // get 으로 넘어오건.. POST 로 넘어오건.. .

                // DB 에서 파일이름을 읽어오건..

                // 한글로 된 파일 이름이 콘솔에 제대로 나와야

                // 다운로드가 제대로 됨

                System.out.println("fileName : " + fileName);


                File dir = new File(request.getContextPath() + "upload");
                File file = new File(dir, fileName);
                DownloadUtil downUtil = new DownloadUtil(file, request, response);

                downUtil.sendFile();
        }


%>

 


 

 

'JAVA > JSP_Servlet' 카테고리의 다른 글

[펌] http RFC  (0) 2005.01.17
[펌]Single Sign On...  (0) 2005.01.17
헐.. 죽이넹  (0) 2004.12.21
얼... 좋은걸~  (0) 2004.12.17
Converting JDBC Result Sets to XML  (0) 2004.12.09
Posted by tornado
|

헐.. 죽이넹

JAVA/JSP_Servlet 2004. 12. 21. 15:14

'JAVA > JSP_Servlet' 카테고리의 다른 글

[펌]Single Sign On...  (0) 2005.01.17
JSP 다운로드시 헤더 ㅡㅡ  (0) 2005.01.13
얼... 좋은걸~  (0) 2004.12.17
Converting JDBC Result Sets to XML  (0) 2004.12.09
[펌] requestmon - 톰캣과의 사투중 발견한 유용한 정보..  (0) 2004.11.10
Posted by tornado
|

얼... 좋은걸~

JAVA/JSP_Servlet 2004. 12. 17. 11:00
Posted by tornado
|
Posted by tornado
|

톰캣과 사투를 벌이던중 자바서비스넷에서 발견한 유용한 정보가 있어서 공유해 드리겠습니다.

requestmon은 자바서비스넷의 이원영씨가 제작한 것으로 서블렛엔진이나 WAS에서 서비스중인

servlet 이나 JSP의 목록을 실시간으로 보여주고, 필요한 경우, 응답이 느린 서비스들만 대상으로 하여 특별한 로그파일에 기록해 주는 기능을 가지고 있습니다.

장애진단및 디버깅을 하기 위해 각 서비스들을 관찰 하는데 유용하게 사용하고 있습니다.

다음과 같은 클래스를 만든후 컴파일해서

/WEB-INF/classes/filters 에 둔후..


package filters;

import java.io.IOException;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;

public class RequestMonFilter implements Filter {
    protected FilterConfig filterConfig = null;

    public void doFilter(ServletRequest request, ServletResponse response,
                         FilterChain chain)
throws IOException, ServletException {

        org.jdf.requestmon.ServiceTrace trace =
             org.jdf.requestmon.ServiceTrace.getInstance();
        trace.startTrace((HttpServletRequest)request);
        try {
        chain.doFilter(request, response);
        }finally {
         trace.endTrace((HttpServletRequest)request);
        }
    }

    public void destroy() {
        this.filterConfig = null;
    }
   
    public void init(FilterConfig filterConfig) throws ServletException { }
}

requestmon3.5.2-4.jar를 /WEB-INF/lib 폴더아래에 둡니다.

물론 jar파일은 filter 컴파일에도 필요합니다.

requestmon.jsp는 다른 JSP두는 적당한 곳에 두시구요.

/WEB-INF/web.xml을 열고 다음을 추가합니다.

    <filter>
        <filter-name>Request Monitor Filter</filter-name>
        <filter-class>filters.RequestMonFilter</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>Request Monitor Filter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

이제 톰캣을 구동시키고 /requestmon.jsp 를 호출해보면 결과를 볼 수 있습니다.

실행속도가 3초이상 걸리는 서비스인 경우에는

%tomcat_home%/bin 디렉토리에 trace.log에 로그가 남겨집니다.

일단 제가 톰캣에 적용시켜서 잘 돌아가는걸 확인했구요..

다른 서블릿엔진에서도 문제없이 돌아가리라고 생각합니다..

 

출처 : 자바서비스넷

Posted by tornado
|

Thomas Risberg
July, 2003


This is a step-by-step account of how to develop a web application from scratch using the Spring Framework. It is divided into a number of parts. You can read them in sequence or skip one if you are already familiar with the concepts covered in it.


Part 1 - Basic Application and Environment Setup


Part 2 – Developing and Configuring the Application


Part 3 – Adding Unit Tests and a Form to the Application


Part 4 – Implementing Database Persistence





Copyright © 2003, Thomas Risberg

Posted by tornado
|

나만 몰랐나?? 아래처럼 하니까 되네여 ㅋ

 

struts 제공 태그는 사용안함...

 

 

<input type="text" name="work" onclick="goCalendar('work','0')" style="cursor:hand" />

 

위와 같은 태그가 한개 있을 경우 Struts FormBean 에서는 아래와 같이 코딩하면 받을 수 있다.

 

private String work;

 

public void setWork(String work){ .... }

public String getWork(){  return work;  }

 

그런데 work 라는 TextField 가 여러개 있을 경우에는???

 

<input type="text" name="work[]" onclick="getDate('work','0')" style="cursor:hand" />

<input type="text" name="work[]" onclick="getDate('work','1')" style="cursor:hand" />

<input type="text" name="work[]" onclick="getDate('work','2')" style="cursor:hand" />

<input type="text" name="work[]" onclick="getDate('work','3')" style="cursor:hand" />

 

위와 같은 형식으로 사용해도 된다.

 

또는...

 

<input type="text" name="work" onclick="getDate('work','0')" style="cursor:hand" />

<input type="text" name="work" onclick="getDate('work','1')" style="cursor:hand" />

<input type="text" name="work" onclick="getDate('work','2')" style="cursor:hand" />

<input type="text" name="work" onclick="getDate('work','3')" style="cursor:hand" />

 

위에처럼 배열표시를 빼도.. 알아 듣는다..

 

배열로 쓸때의 Struts FormBean 은 아래처럼 배열처리해줘야 하겠쥐~~

 

private String[] work = new String[10];

 

public void setWork(String[] work){ this.work = work; }

public String[] getWork(){   return this.work; }

 

일케 하믄 되더만 ㅡㅡ 허무 ......

 

 

 

Posted by tornado
|

Best practices to improve performance in JDBC

This topic illustrates the best practices to improve performance in JDBC with the following sections:

 

Feed back

We appreciate and welcome your comments on this section. Email comments@precisejava.com

<@NHN@LINEBREAKER@NHN@>

Overview of JDBC

JDBC defines how a Java program can communicate with a database. This section focuses  mainly on JDBC 2.0 API.  JDBC API provides two packages they are java.sql and javax.sql . By using JDBC API, you can connect virtually any database, send SQL queries to the database and process the results.

JDBC architecture defines different layers to work with any database and java, they are JDBC API interfaces and classes which are at top most  layer( to work with java ), a driver which is at middle layer (implements the JDBC API interfaces that maps java to database specific language) and a database which is at the bottom (to store physical data). The following figure illustrates the JDBC architecture.

JDBC API provides interfaces and classes to work with databases. Connection interface encapsulates database connection functionality, Statement interface encapsulates SQL query representation and execution functionality and ResultSet interface encapsulates retrieving data which comes from execution of SQL query using Statement.

The following are the basic steps to write a JDBC program

        1. Import java.sql and javax.sql packages

        2. Load JDBC driver

        3. Establish connection to the database using Connection interface

        4. Create a Statement by passing SQL query

        5. Execute the Statement

        6. Retrieve results by using ResultSet interface

        7. Close Statement and Connection

We will look at these areas one by one, what type of driver you need to load, how to use Connection interface in the best manner, how to use different Statement interfaces, how to process results using ResultSet and finally how to optimize SQL queries to improve JDBC performance.

Note1: Your JDBC driver should be fully compatible with JDBC 2.0 features in order to use some of the suggestions mentioned in this section.

Note2: This Section assumes that reader has some basic knowledge of JDBC.

 

Choosing right Driver

Here we will walk through initially about the types of drivers, availability of drivers, use of drivers in different situations, and then we will discuss about which driver suits your application best.

Driver is the key player in a JDBC application, it acts as a mediator between Java application and database. It implements JDBC API interfaces for a database, for example Oracle driver for oracle database, Sybase driver for Sybase database. It maps Java language to database specific language including SQL.

JDBC defines four types of drivers to work with. Depending on your requirement you can choose one among  them.

Here is a brief description of each type of driver :

 

Type of driverTierDriver mechanismDescription
1TwoJDBC-ODBC This driver converts JDBC calls to ODBC calls through JDBC-ODBC Bridge driver which in turn converts to database calls. Client requires ODBC libraries.
2TwoNative API - Partly - Java driver This driver converts JDBC calls to database specific native calls. Client requires database specific libraries.
3ThreeJDBC - Net -All Java driverThis driver passes calls to proxy server through network protocol which in turn converts to database calls and passes through database specific protocol. Client doesn't require any driver.
4TwoNative protocol - All - Java driverThis driver directly calls database. Client doesn't require any driver.

 

Obviously the choice of choosing a driver depends on availability of driver and requirement. Generally all the databases support their own drivers or from third party vendors. If you don't have driver for your database, JDBC-ODBC driver is the only choice because all most all the vendors support ODBC. If you have tiered requirement ( two tier or three tier) for your application, then you can filter down your  choices, for example if your application is three tiered, then you can go for Type three driver between client and proxy server shown below. If you want to connect to database from java applet, then you have to use Type four driver because it is only the driver which supports that feature. This figure shows the overall picture of drivers from tiered perspective.

 

 

This figure illustrates the drivers that can be used for two tiered and three tiered applications. For both two and three tiered applications, you can filter down easily to Type three driver but you can use Type one, two and four drivers for both tiered applications. To be more precise, for java applications( non-applet) you can use Type one, two or four driver. Here is exactly where you may make a mistake by  choosing a driver without taking performance into consideration. Let us look at that perspective in the following section.

Type 3 & 4 drivers are faster than other drivers because Type 3 gives facility for optimization techniques provided by application server such as connection pooling, caching, load balancing etc and Type 4 driver need not translate database calls to ODBC or native connectivity interface. Type 1 drivers are slow because they have to convert JDBC calls to ODBC through JDBC-ODBC Bridge driver initially and then ODBC Driver converts them into database specific calls. Type 2 drivers give average performance when compared to Type 3 & 4 drivers because the database calls have to be converted into database specific calls. Type 2 drivers give better performance than Type 1 drivers.

Finally, to improve performance

1. Use Type 4 driver for applet to database communication.

2. Use Type 2 driver for two tiered applications for communication between java client and the database that gives better performance when compared to Type1 driver

3. Use Type 1 driver if your database doesn't support a driver. This is rare situation because almost all major databases support drivers or you will get them from third party vendors.

4.Use Type 3 driver to communicate between client and proxy server ( weblogic, websphere etc) for three tiered applications that gives better performance when compared to Type 1 & 2 drivers.

 

Optimization with Connection

java.sql package in JDBC provides Connection interface that encapsulates database connection functionality. Using Connection interface, you can fine tune the following operations :

        1. Set optimal row pre-fetch value

        2. Use Connection pool

        3. Control transaction

        4. Choose optimal isolation level

        5. Close Connection when finished

Each of these operations effects the performance. We will walk through each operation one by one.

1. Set optimal row pre-fetch value

We have different approaches to establish a connection with the database, the first type of approach is :

        1. DriverManager.getConnection(String url)

        2. DriverManager.getConnection(String url, Properties props)

        3. DriverManager.getConnection(String url, String user, String password)

        4. Driver.connect(String url, Properties props)

When you use this approach, you can pass database specific information to the database by passing properties using Properties object to improve performance. For example, when you use oracle database you can pass default number of rows that must be pre-fetched from the database server and the default batch value that triggers an execution request. Oracle has default value as 10 for both properties. By increasing the value of these properties, you can reduce the number of database calls which in turn improves performance. The following code snippet illustrates this approach.

        java.util.Properties props = new java.util.Properties();

        props.put("user","scott");

        props.put("password","tiger");

        props.put("defaultRowPrefetch","30");

        props.put("defaultBatchValue","5");

        Connection con = DriverManger.getConnection("jdbc:oracle:thin:@hoststring", props);

You need to figure out appropriate values for above properties for better performance depending on application's requirement. Suppose, you want to set these properties for search facility, you can increase defaultRowPrefetch so that you can increase performance significantly.

The second type of approach is to get connection from DataSource.

You can get the connection using javax.sql.DataSource interface. The advantage of getting connection from this approach is that the DataSource works with JNDI. The implementation of DataSource is done by vendor, for example you can find this feature in weblogic, websphere etc. The vendor simply creates DataSource implementation class and binds it to the JNDI tree. The following code shows how a vendor creates implementation class and binds it to JNDI tree.

        DataSourceImpl dsi = new DataSourceImpl();

        dsi.setServerName("oracle8i");

        dsi.setDatabaseName("Demo");

        Context ctx = new InitialContext();

        ctx.bind("jdbc/demoDB", dsi);

This code registers the DataSourceImpl object to the JNDI tree, then the programmer can get the DataSource reference from JNDI tree without knowledge of the underlying technology.

        Context ctx = new InitialContext();

        DataSource ds = (DataSource)ctx.lookup("jdbc/demoDB");

        Connection con = ds.getConnection();

By using this approach we can improve performance. Nearly all major vendor application servers like weblogic, webshpere implement the DataSource by taking connection from connection pool rather than a single connection every time. The application server creates connection pool by default. We will discuss the advantage of connection pool to improve performance in the next section.

2. Use Connection pool

Creating a connection to the database server is expensive. It is even more expensive if the server is located on another machine. Connection pool contains a number of open database connections with minimum and maximum connections, that means the connection pool has open connections between minimum and maximum number that you specify. The pool expands and shrinks between minimum and maximum size depending on incremental capacity. You need to give minimum, maximum and incremental sizes as properties to the pool in order to maintain that functionality. You get the connection from the pool rather directly .For example, if you give properties like min, max and incremental sizes as 3, 10 and 1 then pool is created with size 3 initially and if it reaches it's capacity 3 and if a client requests a connection concurrently, it increments its capacity by 1 till it reaches 10 and later on it puts all its clients in a queue. 

There are a few choices when using connection pool.

1. You can depend on application server if it supports this feature, generally all the application servers support connection pools. Application server creates the connection pool on behalf of you when it starts. You need to give properties like min, max and incremental sizes to the application server.

2. You  can use JDBC 2.0 interfaces, ConnectionPoolDataSource and PooledConnection if your driver implements these interfaces

3. Or  you can create your own connection pool if you are not using any application server or JDBC 2.0  compatible driver.

By using any of these options, you can increase performance significantly. You need to take care of properties like min, max and incremental sizes. The maximum number of connections to be given depends on your application's  requirement that means how many concurrent clients can access  your database and also it depends up on your database's capability to provide maximum number of connections.

3. Control transaction

In general, transaction represents one unit of work or bunch of code in the program that executes in it's entirety or none at all. To be precise, it is all or no work. In JDBC, transaction is a set of one or more Statements that execute as a single unit.

java.sql.Connection interface provides some methods to control transaction they are

        public interface Connection {

            boolean getAutoCommit();

            void        setAutoCommit(boolean autocommit);

            void        commit();

            void        rollback();

        }

JDBC's default mechanism for transactions:

By default in JDBC transaction starts and commits after each statement's  execution on a connection. That is the AutoCommit mode is true. Programmer need not write a commit() method explicitly after each statement.

Obviously this default mechanism gives good facility for programmers if they want to execute a single statement. But it gives poor performance when multiple statements on a connection are to be executed because commit is issued after each statement by default, that in turn reduces performance by issuing unnecessary commits. The remedy is to flip it back to AutoCommit mode as false and issue commit() method after a set of statements execute, this is called as batch transaction. Use rollback() in catch block to rollback the transaction whenever an exception occurs in your program. The following code illustrates the batch transaction approach.

try{

    connection.setAutoCommit(false);

    PreparedStatement ps = connection.preareStatement( "UPDATE employee SET Address=? WHERE name=?");

    ps.setString(1,"Austin");

    ps.setString(2,"RR");

    ps.executeUpdate();

    PreparedStatement ps1 = connection.prepareStatement( "UPDATE account SET salary=? WHERE name=?");

    ps1.setDouble(1, 5000.00);

    ps1.setString(2,"RR");

    ps1.executeUpdate();

    connection.commit();

    connection.setAutoCommit(true);

    }catch(SQLException e){ connection.rollback();}

    finally{

             if(ps != null){ ps.close();}

             if(ps1 != null){ps1.close();}

   if(connection != null){connection.close();}

    }

This batch transaction gives good performance by reducing commit calls after each statement's execution.

4. Choose optimal isolation level

Isolation level represent how a database maintains data integrity against the problems like dirty reads, phantom reads and non-repeatable reads which can occur due to concurrent transactions. java.sql.Connection interface provides  methods and constants to avoid the above mentioned problems by setting different isolation levels.

public interface Connection {

        public static final int  TRANSACTION_NONE                             = 0

        public static final int  TRANSACTION_READ_COMMITTED         = 2

        public static final int  TRANSACTION_READ_UNCOMMITTED     = 1

        public static final int  TRANSACTION_REPEATABLE_READ       = 4

        public static final int  TRANSACTION_SERIALIZABLE                 = 8

        int          getTransactionIsolation();

        void        setTransactionIsolation(int isolationlevelconstant);

}

You can get the existing isolation level with getTransactionIsolation() method and set the isolation level with setTransactionIsolation(int isolationlevelconstant) by passing above constants to this method.

The following table describes isolation level against the problem that it prevents :

 

Transaction Level                            Permitted Phenomena Performance impact
     Dirty reads   Non  Repeatable readsPhantom reads 
TRANSACTION_NONE N/AN/AN/AFASTEST
TRANSACTION_READ_UNCOMMITEDYESYESYESFASTEST
TRANSACTION_READ_COMMITEDNO YESYESFAST
TRANSACTION_REPEATABLE_READNONOYESMEDIUM
TRANSACTION_SERIALIZABLENONO NOSLOW

YES means that the Isolation level does not prevent the problem

NO means that the Isolation level prevents the problem

By setting isolation levels, you are having an impact on the performance as mentioned in the above table. Database use read and write locks to control above isolation levels. Let us have a look at each of these problems and then look at the impact on the performance.

Dirty read problem :

The following figure illustrates Dirty read problem  :

 

 

Step 1:     Database row has PRODUCT = A001 and PRICE = 10

Step 2:    Connection1 starts  Transaction1 (T1) .

Step 3:    Connection2 starts  Transaction2 (T2) .

Step 4:    T1 updates PRICE =20 for PRODUCT = A001

Step 5:    Database has now PRICE = 20 for PRODUCT = A001

Step 6:    T2 reads PRICE = 20 for PRODUCT = A001

Step 7:    T2 commits transaction

Step 8:    T1 rollbacks the transaction because of some problem

The problem is that T2 gets wrong PRICE=20 for PRODUCT = A001 instead of 10 because of uncommitted read. Obviously it is very dangerous in critical transactions if you read inconsistent data. If you  are sure about not accessing data concurrently  then you can allow this problem by setting TRANSACTION_READ_UNCOMMITED or TRANSACTION_NONE that in turn improves performance otherwise you have to use TRANSACTION_READ_COMMITED to avoid this problem.

 

Unrepeatable read problem :

The following figure illustrates Unrepeatable read problem  :

 

Step 1:     Database row has PRODUCT = A001 and PRICE = 10

Step 2:    Connection1 starts  Transaction1 (T1) .

Step 3:    Connection2 starts  Transaction2 (T2) .

Step 4:    T1 reads PRICE =10 for PRODUCT = A001

Step 5:    T2 updates PRICE = 20 for PRODUCT = A001

Step 6:    T2 commits transaction

Step 7:    Database row has PRODUCT = A001 and PRICE = 20

Step 8:    T1 reads PRICE = 20 for PRODUCT = A001

Step 9:    T1 commits transaction

Here the problem is that Transaction1 reads 10 first time and reads 20 second time but it is supposed to be 10 always whenever it reads a record in that transaction. You can control this problem by setting isolation level as TRANSACTION_REPEATABLE_READ.

Phantom read problem :

The following figure illustrates Phantom read problem  :

Step 1:     Database has a row PRODUCT = A001 and COMPANY_ID = 10

Step 2:    Connection1 starts  Transaction1 (T1) .

Step 3:    Connection2 starts  Transaction2 (T2) .

Step 4:    T1 selects a row with a condition SELECT PRODUCT WHERE COMPANY_ID = 10

Step 5:    T2 inserts a row with a condition INSERT PRODUCT=A002  WHERE

                   COMPANY_ID= 10

Step 6:    T2 commits transaction

Step 7:    Database has 2 rows with that condition

Step 8:    T1 select again with a condition SELECT PRODUCT WHERE COMPANY_ID=10            

                and gets 2 rows instead of 1 row

Step 9:    T1 commits transaction

Here the problem is that T1 gets 2 rows instead of 1 row up on selecting the same condition second time. You can control this problem by setting isolation level as TRANSACTION_SERIALIZABLE

Choosing a right isolation level for your program:

Choosing a right isolation level for your program depends upon your application's requirement. In single application itself the requirement generally changes, suppose if you write a program for searching a product catalog from your database then you can easily choose TRANSACTION_READ_UNCOMMITED because you need not worry about the problems that are mentioned above, some other program can insert records at the same time, you don't have to bother much about that insertion. Obviously this improves performance significantly.

If you write a critical program like bank or stocks analysis program where you want to control all of the above mentioned problems, you can choose TRANSACTION_SERIALIZABLE for maximum safety. Here it is the tradeoff between the safety and performance. Ultimately we need safety here.

If you don't have to deal with concurrent transactions your application, then the best choice is TRANSACTION_NONE to improve performance.

Other two isolation levels need good understanding of your requirement. If your application needs only committed records, then TRANSACTION_READ_COMMITED isolation is the good choice. If your application needs to read a row exclusively till you  finish your work, then TRANSACTION_REPEATABLE_READ is the best choice.

Note: Be aware of your database server's support for these isolation levels. Database servers may not support all of these isolation levels. Oracle server supports only two isolation levels, TRANSACTION_READ_COMMITED and TRANSACTION_SERIALIZABLE isolation level, default isolation level is TRANSACTION_READ_COMMITED.

5. Close Connection when finished

Closing connection explicitly allows garbage collector to recollect memory as early as possible. Remember that when you use the connection pool, closing connection means that it returns back to the connection pool rather than closing direct connection to the database.

 

Optimization with Statement

Statement interface represents SQL query and execution and they provide number of methods and constants to work with queries. They also provide some methods to fine tune performance. Programmer may overlook these fine tuning methods that result in poor performance. The following are the tips to improve performance by using statement interfaces

        1. Choose the right Statement interface

        2. Do batch update

        3. Do batch retrieval using Statement

        2. Close Statement when finished

1. Choose right Statement interface

There are three types of Statement interfaces in JDBC to represent the SQL query and execute that query, they are Statement, PreparedStatement and CallableStatement.

Statement is used for static SQL statement with no input and output parameters, PreparedStatement is used for dynamic SQL statement with input parameters and CallableStatement is used for dynamic SQL satement with both input and output parameters, but PreparedStatement and CallableStatement can be used for static SQL statements as well. CallableStatement is mainly meant for stored procedures.

PreparedStatement gives better performance when compared to Statement because it is pre-parsed and pre-compiled by the database once for the first time and then onwards it reuses the parsed and compiled statement. Because of this feature, it significantly improves performance when a statement executes repeatedly, It reduces the overload incurred by parsing and compiling.

CallableStatement gives better performance when compared to PreparedStatement and Statement when there is a requirement for single request to process multiple complex statements. It parses and stores the stored procedures in the database and does all the work at database itself that in turn improves performance. But we loose java portability and we have to depend up on database specific stored procedures.

2. Do batch update

You can send multiple queries to the database at a time using batch update feature of statement objects this reduces the number of JDBC calls and improves performance. Here is an example of how you can do batch update,

statement.addBatch( "sql query1");

statement.addBatch(" sql query2");

statement.addBatch(" sql query3");

statement.executeBatch();

All three types of statements have these methods to do batch update.

3. Do batch retrieval using Statement

You can get the default number of rows that is provided by the driver. You can improve performance by increasing number of rows to be fetched at a time from database using setFetchSize() method of the statement object.

Initially find the default size by using

Statement.getFetchSize();   and then set the size as per your requirement

Statement.setFetchSize(30);

Here it retrieves 30 rows at a time for all result sets of this statement.

4. Close Statement when finished

Close statement object as soon as you finish working with that, it explicitly gives a chance to garbage collector to recollect memory as early as possible which in turn effects performance.

Statement.close();

 

Optimization with ResultSet

ResultSet interface represents data that contains the results of executing an SQL Query and it provides a number of methods and constants to work with that data. It also provides methods to fine tune retrieval of data to improve performance. The following are the fine tuning tips to improve performance by using ResultSet interface.

        1. Do batch retrieval using ResultSet

        2. Set up proper direction for processing the rows

        3. Use proper get methods

        4. Close ResultSet when finished

1. Do batch retrieval using ResultSet

ResultSet interface also provides batch retrieval facility like Statement as mentioned above. It overrides the Statement behaviour.

Initially find the default size by using

ResultSet.getFetchSize(); and then set the size as per requirement

ResultSet.setFetchSize(50);

This feature significantly improves performance when you are dealing with retrieval of large number of rows like search functionality.

2. Setup proper direction of processing rows

ResultSet has the capability of setting the direction in which you want to process the results, it has three constants for this purpose, they are

FETCH_FORWARD, FETCH_REVERSE, FETCH_UNKNOWN

Initially find the direction by using

ResultSet.getFetchDirection(); and then set the direction accordingly

ResultSet.setFetchDirection(FETCH_REVERSE);

3. Use proper getxxx() methods

ResultSet interface provides lot of getxxx() methods to get and convert database data types to java data types and is flexibile in converting non feasible data types. For example,

getString(String columnName) returns java String object.

columnName is recommended to be a VARCHAR OR CHAR type of database but it can also be a NUMERIC, DATE etc.

If you give non recommended parameters, it needs to cast it to proper java data type that is  expensive. For example consider that  you select  a product's id from huge database which returns millions of records from search functionality, it needs to convert all these records that is very expensive.

So always use proper getxxx() methods according to JDBC recommendations.

4. Close ResultSet when finished

Close ResultSet object as soon as you finish working with ResultSet object even though Statement object closes the ResultSet object implicitly when it closes, closing ResultSet explicitly gives chance to garbage collector to recollect memory as early as possible because ResultSet object may occupy lot of memory depending on query.

ResultSet.close();

 

Optimization with SQL Query

This is one of the area where programmers generally make a mistake

If you give a query like

Statement stmt = connection.createStatement();

ResultSet rs = stmt.executeQuery("select * from employee where name=RR");

The returned result set contains all the columns data. you may not need all the column data and want only salary for RR.

The better query is "select salary from employee where name=RR"

It returns the required data  and reduces unnecessary data retrieval.

 

Cache the read-only and read-mostly data

Every database schema generally has read-only and read-mostly tables. These tables are called as lookup tables. Read-only tables contain static data that never changes in its life time. Read-mostly tables contain semi dynamic data that changes often. There will not be any sort of writing operations in these tables.

If an application reads data from these tables for every client request, then it is redundant, unnecessary and expensive. The solution for this problem is to cache the read-only table data by reading the data from that table once and caching the read-mostly table data by reading and refreshing with time limit. This solution improves performance significantly. See the following link for source code of such caching mechanism.

http://www.javaworld.com/javaworld/jw-07-2001/jw-0720-cache.html

You can tweak this code as per application requirement. For read-only data, you need not refresh data in its life time. For read-mostly data, you need to refresh the data with time limit. It is better to set this refreshing time limit in properties file so that it can be changed at any time.

 

Fetch small amount of data iteratively instead of fetching whole data at once

Applications generally require to retrieve huge data from the database using JDBC in operations  like searching data. If the client request for a search, the application might return the whole result set at once. This process takes lot of time and has an impact on performance. The solution for the problem is

1. Cache the search data at the server-side and return the data iteratively to the client. For example, the search returns 1000 records, return data to the client in 10 iterations where each iteration has 100 records.

2. Use Stored procedures to return data iteratively. This does not use server-side caching rather server-side application uses Stored procedures to return small amount of data iteratively.

Out of these solutions the second solution gives better performance because it need not keep the data in the cache (in-memory). The first procedure is useful when the total amount of data to be returned is not huge.

 

Key Points

  1. Use Type two driver for two tiered applications to communicate from java client to database that gives better performance than Type1 driver.
  2. Use Type four driver for applet to database communication that is two tiered applications and three tiered applications when compared to other drivers.
  3. Use Type one driver if you don't have a driver for your database. This is a rare situation because all major databases support drivers or you will get a driver from third party vendors.
  4. Use Type three driver to communicate between client and proxy server ( weblogic, websphere etc) for three tiered applications that gives better performance when compared to Type 1 &2 drivers.
  5. Pass database specific properties like defaultPrefetch if your database supports any of them.
  6. Get database connection from connection pool rather than getting it directly
  7. Use batch transactions.
  8. Choose right isolation level as per your requirement. TRANSACTION_READ_UNCOMMITED gives best performance for concurrent transaction based applications. TRANSACTION_NONE gives best performance for non-concurrent transaction based applications.
  9. Your database server may not support all isolation levels, be aware of your database server features.
  10. Use PreparedStatement when you execute the same statement more than once.
  11. Use CallableStatement when you want result from multiple and complex statements for a single request.
  12. Use batch update facility available in Statements.
  13. Use batch retrieval facility available in Statements or ResultSet.
  14. Set up proper direction for processing rows.
  15. Use proper getXXX() methods.
  16. Close ResultSet, Statement and Connection whenever you finish your work with them.
  17. Write precise SQL queries.
  18. Cache read-only and read-mostly tables data.
  19. Fetch small amount of data iteratively rather than whole data at once when retrieving large amount of data like searching database etc.

Posted by tornado
|

http://jakarta.apache.org/commons/dbcp/apidocs/org/apache/commons/dbcp/package-summary.html#package_description

 

여기 내용보고 열쉬미 멜링 리스트 부터.. cvs... google 을 뒤진 결과.... 아주 간단하게

풀을 사용하는 법을 알아냄...

 

커먼스 관련 lib 는 최신으로 도배함 ㅡㅡ

 

1. jocl 만들기...

  파일 이름은 intranet.jocl 로 했음.. 저장은 classes 에 함...

  각 설명은 해당 내용 옆의 주석을 참고하면 됨

 

<object class="org.apache.commons.dbcp.PoolableConnectionFactory" xmlns="http://apache.org/xml/xmlns/jakarta/commons/jocl">

  <object class="org.apache.commons.dbcp.DriverManagerConnectionFactory">
    <string value="jdbc:mysql://localhost:3306/test?useUnicode=true&amp;characterEncoding=euc-kr" /> <!-- jdbcurl -->
    <string value="root" />  <!-- user -->
    <string value="123456" />  <!-- password -->
  </object>
 
  <object class="org.apache.commons.pool.impl.GenericObjectPool">
    <object class="org.apache.commons.pool.PoolableObjectFactory" null="true" />
    <int value="80" />  <!-- max active -->
    <byte value="1" />  <!-- when exhausted action, 0 = fail, 1 = block, 2 = grow -->
    <long value="2000" />  <!-- max wait -->
    <int value="10" />  <!-- max idle -->
    <boolean value="true" />  <!-- test on borrow -->
    <boolean value="true" />  <!-- test on return -->
    <long value="10000" />     <!-- time between eviction runs -->
    <int value="5" />   <!-- number of connections to test per eviction run -->
    <long value="5000" />    <!-- min evictable idle time -->
    <boolean value="true" />   <!-- test while idle -->
  </object>
 
  <object class="org.apache.commons.pool.impl.StackKeyedObjectPoolFactory">
    <int value="10" />
  </object>
 
  <string value="select now()" />  <!-- validation query -->
  <boolean value="false" />  <!-- default read only -->
  <boolean value="true" /><!-- default auto commit -->
</object>

 

 

2. 해당 jocl 을 읽어 들여 서블릿에서 사용하기 위해 ServletListener 를 만듬..

 

/*
 * Created on 2004. 9. 7.
 * Package Name : com.gndsoft.action.admin
 * Version :
 * Author : 유희성(over30tornado@msn.com)
 *
 * History : 2004. 9. 7. 유희성(over30tornado@msn.com) 최초 작성
 *

 *
 */

 

package com.gndsoft.intranet.db;

 

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

 

/**
 * @author Tornado
 *
 */


public class DBCPPoolListener implements ServletContextListener {


 public void contextInitialized(ServletContextEvent sce) {
 
        try {
           
         Class.forName("com.mysql.jdbc.Driver");

 

         Class.forName("org.apache.commons.dbcp.PoolingDriver");
           
         System.setProperty( "org.xml.sax.driver",  "org.apache.xerces.parsers.SAXParser" );

 

        } catch(Exception ex) {
        
         ex.printStackTrace();
        
        }
 }


 public void contextDestroyed(ServletContextEvent sec) {

     // 커먼스 풀에서 뭔가 닫아줘야 할 경우 작성하면 된다.

 }

 

}

 

-------

Class.forName() 에서 뭘 적어줘야 할지 메일링리스트에서 찾다가 질문에 올라온거로 해서 성공함 ㅡㅡ

 

System.setProperty() 때문에 메일링 리스트 엄청 뒤짐 ㅜㅜ ... 저거 몰라서 열라 헤맴..

 

3. web.xml 에 해당 Listener 등록

 

<?xml version="1.0" ?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">

<web-app>

    <!-- Context Param -->

 <context-param>
      <param-name>poolName</param-name>
      <param-value>intranet</param-value>
    </context-param>
 
 <listener>
   <listener-class>com.gndsoft.intranet.db.DBCPPoolListener</listener-class>
 </listener>

</web-app>

 

 

-------

Pool 이름이 하드코딩될까 두려운 나머지.. 테스트 하는데 이름을 Init-param 으로 뺌 --

 

 

4. Test 를 하기 위한 JSP 작성

 

<%@ page contentType="text/html; charset=euc-kr" %>

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

<%
 
 Connection conn = null;
 Statement stmt = null;
 ResultSet rs = null;
 
 try{
 
  String name = application.getInitParameter("poolName");
  
  if(name == null || "".equals(name) )  name = "intranet";
  
  conn = DriverManager.getConnection("jdbc:apache:commons:dbcp:/" + name);
  
  stmt = conn.createStatement();
  
  rs = stmt.executeQuery("select now()");
  
  while(rs.next()){
  
   out.print( rs.getString(1) );
  }
  
 }catch(Exception e){
 
     e.printStackTrace();
    
 }finally{
 
  try{ if(rs != null) rs.close(); }catch(Exception e){ e.printStackTrace(); }
  
  try{ if(stmt != null) stmt.close(); }catch(Exception e){ e.printStackTrace(); }
  
  try{ if(conn != null) conn.close(); }catch(Exception e){ e.printStackTrace(); }    
  
 }
 

%>

 

끝~!

 

DriverManager.getconnection() 할 때 jdbc URL 이 아닌 커먼스 풀을 적어줘야 풀링된 자원을 가져온다고 함...

몇일 써보고 좋으면 바로 적용~~~~

 

 

Posted by tornado
|

dbcp

JAVA/JSP_Servlet 2004. 9. 6. 16:37
Posted by tornado
|

'JAVA > JSP_Servlet' 카테고리의 다른 글

Commons Pool + DBCP 사용법  (0) 2004.09.07
dbcp  (0) 2004.09.06
쓰알.... 플래쉬 게시판땜쉬...  (0) 2004.08.24
[struts] &lt;bean:size 태그...  (0) 2004.08.17
[eclipse 3.0] Ctrl + E 누르니까... ㅋ  (0) 2004.08.16
Posted by tornado
|

게시판을 플래쉬로 가잰다...

 

그래서.... 알아본 결과.... &val_1=xxx&val_2=yyy 와 같은 방식과..

 

XML 로 데이터를 날려서.. 플래쉬에서 xml 을 파싱하는 방법이 있더만...

 

결국 xml 로 하기로 했음.... xml 로 만드는 툴은 jdom 을 선택

 

그래서리... 급조하여 아래의 코드가 나옴 ㅡㅡ

 

덕분에 Action 클래스가 300 줄에 육박 ㅡㅡ 미챠~~~

 

플래쉬에서 파일 업로드는 어케 허나... 환장하겠네 ㅡㅡ

 

//------------------------------------------------------------
  //  게시판 리스트 XML 생성
  //------------------------------------------------------------
  
  Element rootEL = new Element("board-list");
  
  Document doc = new Document(rootEL);
  
  Element articles = new Element("articles");
  
  rootEL.addContent(articles);

  Element articleElement = null;
  
  int virtualNum = p.getVirtualNumber();
  
  while(iter.hasNext()){


   dyna = (DynaBean)iter.next();

 

   articleElement = new Element("article");
   
   Element[] ele = new Element[fieldNames.length];
   
   for(int i = 0; i <fieldNames.length; i++){
    
    ele[i]= new Element(fieldNames[i]);
    
    // 가상 번호 적어주기.
    if("virtualNum".equals(fieldNames[i])){
     
     ele[i].setText("" + virtualNum);
     
    }else if("regidate".equals(fieldNames[i])){
     
     ele[i].addContent(BoardUtil.modifyDate(dyna.get(fieldNames[i]).toString(), true));
     
    }else{
     
     ele[i].addContent(dyna.get(fieldNames[i]).toString());
     
    }

    articleElement.addContent(ele[i]);    
       
   }

 

 

Posted by tornado
|

에그... 이리 간단한걸.. ㅡㅡ;;

       

    <bean:size id="len" name="SearchEmp" />

 

    <!-- 내용 없을 때 -->
    <logic:equal name="len" value="0" >        
    <tr>
      <td height="150" ALIGN="CENTER" colspan="4">
        검색된 직원이 없습니다!!
      </td>
    </tr>
  </logic:equal>

 

Posted by tornado
|