Java TM 2 Platform Standard Edition java lang

  • Slides: 30
Download presentation

Java. TM 2 Platform Standard Edition java. lang. Object java. applet java. awt java.

Java. TM 2 Platform Standard Edition java. lang. Object java. applet java. awt java. io Inet. Address Socket …… URL PL Lab java. net URLEncoder …… …… javax. swing Server. Socket 3

java. net의 classes과 interfaces Classes Interfaces Authenticator Content. Handler. Factory File. Name. Map Datagram.

java. net의 classes과 interfaces Classes Interfaces Authenticator Content. Handler. Factory File. Name. Map Datagram. Packet Datagram. Socket. Impl. Factory Socket. Options Datagram. Socket. Impl Http. URLConnection Inet. Address Jar. URLConnection Multicast. Socket Net. Permission Password. Authentication Server. Socket. Impl Socket. Permission URLClass. Loader URLConnection URLDecoder URLEncoder URLStream. Handler. Factory URLStream. Handle • jdk 1. 2 기준 PL Lab 4

java. net. URL의 클래스 Constructors URL( …) : 인수가 다른 6개의 constructor가 있다. Methods

java. net. URL의 클래스 Constructors URL( …) : 인수가 다른 6개의 constructor가 있다. Methods equals() get. Content() get. File() get. Host() get. Port() get. Protocol() get. Ref() hash. Code() open. Connection() open. Stream() same. File() set. URLStream. Handler. Factory() to. External. Form() to. String() PL Lab 5

URL 클래스의 생성자 n n n n URL(String spec) URL(String protocol, String host, String

URL 클래스의 생성자 n n n n URL(String spec) URL(String protocol, String host, String file) URL(String protocol, String host, int port, String file, URLStream. Handler handler) URL(URL context, String spec, URLStream. Handler handler) 생성방법 n n new URL(…) 모든 URL 생성자는 Malformed. URLException을 발생 n n PL Lab 자바가 지원하지 않는 프로토콜에 대한 URL을 생성하려고 할 경우 발생한다. HTTP, FTP, news, mailto, gopher, file 지원 7

URL 클래스의 생성자 n public URL(String url) throws Malformed. URLException n 예제 n import

URL 클래스의 생성자 n public URL(String url) throws Malformed. URLException n 예제 n import java. net. *; public class URLConstructor. Test 1 { public static void main(String[] args) { URL web. URL, ftp. URL; try { web. URL = new URL(“http: //www. macfaq. com/vendor. html”); System. out. println(web. URL); ftp. URL = new URL(“ftp: //ftp. macfaq. com/pub/”); System. out. println(ftp. URL); } Catch (Malformed. URLException e) { System. err. println(e); }} } n 결과 http: //www. macfaq. com/vendor. html java. net. Malformed. URLException: ftp//ftp. macfaq. com/pub/: java. lang. Exception PL Lab 8

URL 클래스의 생성자 n public URL(String protocol, String host, String file) throws Malformed. URLException

URL 클래스의 생성자 n public URL(String protocol, String host, String file) throws Malformed. URLException n 포트번호는 – 1로 할당하여 기본 값 포트 번호가 사용됨 n file을 나타내는 문자열은 슬래쉬(/)로 시작 n 예제 n PL Lab try { URL u = new URL(“http”, www. macfaq. com, ”/blueribbon. html#intro”); } catch (Malformed. URLException e) { System. out. println(e); } 9

URL 클래스의 생성자 n public URL(String protocol, String host, int port, String file) throws

URL 클래스의 생성자 n public URL(String protocol, String host, int port, String file) throws Malformed. URLException n n 기본값 포트 번호가 다를 경우 사용 예제 try { URL u = new URL(“http”, ”www. eff. org”, 80, ”/blueribbon. html#intro”); } catch ( Malformed. URLException e) { System. err. println(e); } PL Lab 10

URL 클래스의 생성자 n n public URL(URL u, String s) throws Malformed. URLException n

URL 클래스의 생성자 n n public URL(URL u, String s) throws Malformed. URLException n 상대 URL을 이용하여 절대 URL을 생성할 경우 사용 n 같은 디렉토리 안에 있는 여러 파일을 처리하는 경우 유용 예제 n n 디렉토리구조 user/public_html/examples. html user/public_html/vendor. html user/public_html/applet/URLConstructor. Test 4. class examples. html <html> <body> <applet code = “. . /applet/URLConstructor. Test 4. class”> </body> </html> PL Lab 11

URL 클래스의 생성자 n 예제 n n import java. net. *; import java. applet.

URL 클래스의 생성자 n 예제 n n import java. net. *; import java. applet. Applet; public class URLConstructor. Test 4 extends Applet { public void init() { URL u 1, u 2; u 1 = get. Document. Base(); System. out. println(u 1); try { u 2 = new URL(u 1, “vendor. html”); System. out. println(u 2); } Catch (Malformed. URLException e) { System. err. println(e); } } } 결과 n PL Lab File: user/public_html/examples. html File: user/public_html/vendor. html 12

URL 조각내기 n URL class의 Methods n n n PL Lab Protocol : Host

URL 조각내기 n URL class의 Methods n n n PL Lab Protocol : Host : Port : File : Ref : URLStream. Handler get. Protocol() get. Host() get. Port() get. File() get. Ref() 13

URL class의 Methods n public String get. Protocol() n URL의 프로토콜 부분을 String로 반환한다.

URL class의 Methods n public String get. Protocol() n URL의 프로토콜 부분을 String로 반환한다. n public String get. Host() n URL의 호스트 이름 부분을 String로 반환한다. n public String get. Port() n URL이 지정하고 있는 포트번호를 int로 반환한다. n 포트번호가 명시되지 않으면 – 1을 반환 PL Lab 14

URL class의 Methods 예제 예제 import java. net. *; public class get. URLParts {

URL class의 Methods 예제 예제 import java. net. *; public class get. URLParts { public static void main(String args[]) { try { URL u = new URL(args[0]); System. out. println( u. get. Protocol() ); System. out. println( u. get. Host() ); System. out. println( u. get. Port() ); System. out. println( u. get. File() ); System. out. println( u. get. Ref() ); } catch (Malformed. URLException e ) { System. out. println( e ); } } } 결과 $ java get. URLParts http: //www. ncsa. uiuc. edu/demoweb/html-primer. html#A 1. 3. 3 http www. ncsa. uiuc. edu -1 /demoweb/html-primer. html A 1. 3. 3 PL Lab 16

URL에서 데이터를 가져오기 위한 메소드 n n public final Input. Stream open. Stream() throws

URL에서 데이터를 가져오기 위한 메소드 n n public final Input. Stream open. Stream() throws IOException n URL이 가리키고 있는 자원에 연결 n 클라이언트와 서버간의 핸드셰이킹(handshaking) n 데이터를 읽어 들이기 위한 Input. Stream을 반환 n 파일의 원시 컨텐트를 반환하고 HTML의 헤더나 프로토콜 의 정보는 들어 있지 않다. 예제 URL u = new URL(“http: //www. hannam. ac. kr”); Data. Input. Stream the. HTML = new. Data. Input. Stream(u. open. Stream()); While ( ( this. Line = the. HTML. read. Line()) != null ) System. out. println(this. Line); PL Lab 17

URL에서 데이터를 가져오기 위한 메소드 n n public final Object get. Content() throws IOException

URL에서 데이터를 가져오기 위한 메소드 n n public final Object get. Content() throws IOException n URL에 연결된 데이터를 가져옴. n 특정한 객체의 유형으로 변환하는 특징을 갖는다. n 데이터가 ASCII 또는 HTML이면 Input. Stream을 반환 n 데이터가 GIF/JPEG 이면 Image. Producer을 반환 n MIME 헤더의 content-type을 사용 n 낯선Content-type을 보내면 Class. Notfound. Exception 을 발생 예제 URL u = new URL(“http: //www. hannam. ac. kr”); Object o = u. get. Content(); URL u = new URL(“http: //www. hannam. ac. kr/image/main. gif”); Object o = u. get. Content(); PL Lab 19

URL클래스의 유틸리티 메소드 n public boolean same. File(URL other) n n 두개의 URL이 같은

URL클래스의 유틸리티 메소드 n public boolean same. File(URL other) n n 두개의 URL이 같은 파일을 가리키고 있는지 조사 두 URL의 필드를 조사 n 같은 IP의 주소이고 도메인명이 다를 경우 n 포트번호의 명시의 차이가 날 경우 다른 URL로 취급 URL객체만 인자로 넘겨질수 있다. 예제 URL u 1 = new URL(“http: //www. ncsa. uiuc. edu/HTMLPrimer. html#GS”); URL u 2 = new URL(“http: //www. ncsa. uiuc. edu/HTMLPrimer. html#HD”); If ( u 1. samefile(u 2) ) { System. out. println(“the same”); } else { System. out. println(“not the same”); } PL Lab 20

java. net. URLEncoder 클래스 Constructors 없음. Methods static String encode(String s) PL Lab 22

java. net. URLEncoder 클래스 Constructors 없음. Methods static String encode(String s) PL Lab 22

URLEncoder 클래스의 예제 n 예제 System. out. println(URLEncoder. encode(“This String has spaces”)); System. out.

URLEncoder 클래스의 예제 n 예제 System. out. println(URLEncoder. encode(“This String has spaces”)); System. out. println(URLEncoder. encode(“This*string*has*stars”)); System. out. println(URLEncoder. encode(“This+string+has+plus”)); System. out. println(URLEncoder. encode(“This: string: has: colons”)); System. out. println(URLEncoder. encode(“This. string. has. periods”) ); n 결과 This+string+has+spaces This%2 astring%2 ahas%2 astars This%2 bstring%2 bhas%2 bplus This%3 astring%3 ahas%3 acolons This%2 estring%2 ehas%2 eperiods PL Lab 24

Query. String 클래스 import java. net. URLEncoder; public class query. String { String query;

Query. String 클래스 import java. net. URLEncoder; public class query. String { String query; public query. String(Object name, Object value) { query = URLEncoder. encode(name. to. String()) + “=” + URLencoder. encode(value. to. String()); } public void add(Object name, Object value) { query += “&” + URLEncoder. encode(name. to. String()) + “=” + URLEncoder. encode(value. to. String()); } public String to. String() { return query; } } PL Lab 27

QSTest 실행 public QSTest { public static void main(String[] args) { String name 1

QSTest 실행 public QSTest { public static void main(String[] args) { String name 1 = “username”; String value 1 = “Elliotte Rusty Harold”; String name 2 = “email”; String value 2 = “elahro@sunsite. edu”; query. String qs_ = new query. String(name 1, value 1); qs_. add(name 2, value 2); System. out. println( qs_. to. String() ); } } 결과 % java QSTest username=Elliotte+Rusty+Harold&email=elharo%40 sunsite%2 eedu PL Lab 28

lycos 프로그램 import java. net. *; import java. io. *; public class lycos {

lycos 프로그램 import java. net. *; import java. io. *; public class lycos { public static void main(String[] args) { String querystring = ""; for ( int i =0; i<args. length; i++ ) querystring += args[i] + " "; querystring = "query=" + URLEncoder. encode(querystring); try { String this. Line; URL u = new URL("http: //www. lycos. com/cgi-bin/pursuit? " + querystring); Data. Input. Stream the. HTML = new Data. Input. Stream(u. open. Stream()); while ((this. Line = the. HTML. read. Line()) != null) System. out. println(this. Line); } catch (Exception e) { System. err. println(e); } }} PL Lab 30