




已阅读5页,还剩74页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
782311027-0941Section 3 Web Interface DesignSection 3 Web Interface Design网络协议相关对象1网络协议1套件1Java Servlet程序设计21基本Servelt概念21HTTP服务器之设定23jsdk23resin24Tomcat5.526J2EE27Servlet的执行过程与同步处理30服务器的系统环境变量33URL数据传递与Cookies的处理37档案上传的处理44JSP程序设计48JSP基本概念与脚本元素48HTTP服务器之设定50resin50Tomcat5.550J2EE51JSP的指引元素与动作元素52JSP的隐含对象Session和Cookie的处理55Session的处理56Cookie的处理57JSP的窗体处理数据传递59档案上传62电子邮件寄送 JavaMail63建置JavaMail的开发环境63JavaMail API63实例测试以一般文字或HTML格式寄送电子邮件71实例测试寄送夹带档案之电子邮件75网络协议相关对象网络协议 TCP/IPInternet因特网使用的标准通讯协议,因为TCP/IP代表着十数种不同的通讯协议,只是以该组协议中最重要的两个协议TCP(Transmission Control Protocol)和IP(Internet Protocol)为名。TCP/IP是Internet成千上万台计算机问的共通语言,如同人类沟通时使用的英文一般,Internet计算机主机间彼此借着TCP/IP相互交换计算机数据,至于如何找到指定计算机,则是使用IP地址和网域名称。 IP地址(IP Address)在Internet连接的每部计算机都拥有唯一地址,Internet寻址系统采用的方式是指定每一台计算机一个号码,称为IP地址,长度为4个字节,以十进制格式表示,例如HiNet邮件服务器的IP地址是0。 网域名称(Domain Name)为了方便使用者记忆计算机的地址,给予IP地址对应的英文网域名称。为了管理Internet上成千上万台计算机,网域名称是使用树状阶层架构来分门别类,1983年网域名称系统(Domain Name System, DNS)使用树状阶层结构对映网域名称,在树状阶层结构的最上层分为九大类edu、com、gov、mil、org、net、int、apra和国码(依ISO标准定义,例如:tw代表台湾)。 网络的目的是为了资源共享,将储存在不同计算机的档案或数据进行交换,同样属于档案输入和输出的问题,所以Java将网络通讯视为一种档案I/O的串流,当建立好计算机间的联机后,使用档案串流传送和接收远程计算机的数据。套件 Java网络应用程序是使用套件的类别,可以分成高阶的URL类别与低阶的Socket类别2种层次。n 高阶的URL类别URL类别的对象是WWW(World Wide Web)的URL网址,全名是万用资源寻址器(Universal Resource Locator),提供多种方法剖析URL网址,并且支持HTTP(Hyper Text Transfer Protocol)通讯协议,能够联机Web网站且取得HTTP标头数据,或是直接下载网页档案。n 低阶的Socket类别Socket类别是用来建立不同程序的网络联机,以便Java程序可以透过网络联机到其它Java程序或Internet服务,建立主从架构(Client/Server)的网络应用程序。2个程序使用TCP/IP进行通讯,Java程序需要建立Socket对象来建立两端的联机,以使用输入和输出串流进行数据传输。 InetAddress对象Java的内建类别中,.*之InetAddress类别的对象是用来代表IP地址,它可以是一个由32-bit(IPv4的格式,4组数字)或128-bit(IPv6格式,16组数字)无负号整数组成的IP地址。提供网域名称和IP地址转换功能,可以查询DNS服务器的对照表来进行网域名称转换。InetAddress并无建构子,其类别建置如下java.lang.Object extended by .InetAddressn 常用的方法如下bytegetAddress()Returns the raw IP address of this InetAddress object.static InetAddressgetAllByName(String host) throws UnknownHostExceptionGiven the name of a host, returns an array of its IP addresses, based on the configured name service on the system.static InetAddressgetByAddress(byte addr) throws UnknownHostExceptionReturns an InetAddress object given the raw IP address .static InetAddressgetByAddress(String host, byte addr) Create an InetAddress based on the provided host name and IP address No name service is checked for the validity of the address.static InetAddressgetByName(String host) throws UnknownHostExceptionDetermines the IP address of a host, given the hosts name.StringgetHostAddress()Returns the IP address string in textual presentation.StringgetHostName()Gets the host name for this IP address.static InetAddressgetLocalHost() throws UnknownHostExceptionReturns the local host.StringgetCanonicalHostName()Gets the fully qualified domain name for this IP address.booleanequals(Object obj) Compares this object against the specified object.StringtoString()Converts this IP address to a String. Example:import java.lang.*;import .*;public class Ex03_InetAddress public static void main(String para) throws UnknownHostException InetAddress IP1 = InetAddress.getLocalHost(); /本地主机 System.out.println(IP1 = + IP1); / 1. System.out.println(=); InetAddress IP2 = InetAddress.getByName(); / 2. System.out.println(IP2 = +IP2); System.out.println(=); InetAddress IP3 = InetAddress.getByName(81); / 3. System.out.println(IP3 = +IP3); System.out.println(=); InetAddress IP4 = InetAddress.getByAddress( new byte(byte)216,(byte)239,(byte)53,(byte)101 );/ 4. System.out.println(IP4 = + IP4); System.out.println(=); InetAddress IP5 = InetAddress.getByAddress(, new byte(byte)216,(byte)239,(byte)53,(byte)101 ); / 5. System.out.println(IP5 = + IP5); System.out.println(=); InetAddress IPGroup = InetAddress.getAllByName();/ 6. System.out.println(印出 IPGroup:); for(int x=0;xIPGroup.length;x+) System.out.println(IPGroupx); InetAddress IP6 = InetAddress.getByName( );/ 7. System.out.println(IP6.getCanonicalHostName()= + IP6.getCanonicalHostName(); System.out.println(IP6.getHostAddress()= + IP6.getHostAddress(); System.out.println(IP6.getHostName()= + IP6.getHostName(); System.out.println(IP6.toString()= + IP6.toString(); System.out.println(=); InetAddress IP7 = InetAddress.getByAddress(IP6.getAddress();/ 8. System.out.println(IP7.getHostAddress()= + IP7.getHostAddress() ); System.out.println(IP7.getHostName()= + IP7.getHostName(); if(IP7.equals(IP6) System.out.println( IP7 equals IP6 ); else System.out.println( IP7 not equals IP6 ); /= InetAddress IP8 = InetAddress.getByName(1 ); / 9. /与 InetAddress.getByName() 代表的是同一个 IP System.out.println(IP8.getHostAddress()= + IP8.getHostAddress() ); System.out.println(IP8.getHostName()= + IP8.getHostName(); if(IP8.equals(IP6) System.out.println( IP8 equals IP6 ); else System.out.println( IP8 not equals IP6 ); System.out.println(=); /欲取得IP4地址的四组数字- Class A、Class B、Class C、Class D byte rawAddr = IP4.getAddress(); /建构时传入的 byte /先取得此 InetAddress 之 raw IP Address System.out.print(未加工处理之 rawAddr 数组元素 = ); for(int x=0;x0) System.out.print( 、); System.out.print( rawAddrx); System.out.println(); System.out.println(/ 10. The IP address:n Class A = + (rawAddr0& 0xff) + n Class B = + (rawAddr1& 0xff) + n Class C = + (rawAddr2& 0xff) + n Class D = + (rawAddr3& 0xff) ); /0xff = 255 批注:1. 取得本地主机的IP,相当于印出IP1.toString(),如Snow-NSC/0。2. 用主机名称格式的字符串取得sun的IP,如/95。3. 使用IP格式的字符串取得IP,如无法取得网站名称则显示名称的位置为空值,如/81。4. 用 byte 数组取得IP,如该IP无法取得网站名称则显示名称的位置为空值,如/01。5. 用byte 数组取得IP,若该IP 所指主机有DNS,请写出它正确的主机名称,如:IP为 01的主机名称就是 ,则显示/01。6. 列出DNS为之所有IP。7. 取得所有的IP信息8. 比较IP6与IP7两个 InetAddres信息9. 比较IP6与IP8两个 InetAddres信息10. int 的 216 = 0000 0000 0000 0000 0000 0000 1101 1000转成 byte 变 1101 1000 = -40byte 的 -40 转成int还是 40-40 1111 1111 1111 1111 1111 1111 1101 1000255 & 0000 0000 0000 0000 0000 0000 1111 1111 有效位数取末 8 位216 0000 0000 0000 0000 0000 0000 1101 1000 求得原来 int 之值 URL类别对象URL类别对象是WWW服务的资源对象,每一个URL对象就是URL地址,且需要处理MalformedURLException例外处理,其建置与建构子如下java.lang.Object .URLpublic final class URLextends Object implements SerializableURL(String spec) throws MalformedURLExceptionCreates a URL object from the String representation.URL(String protocol, String host, int port, String file) throws MalformedURLExceptionCreates a URL object from the specified protocol, host, port number, and file.URL(String protocol, String host, String file) throws MalformedURLExceptionCreates a URL from the specified protocol name, host name, and file name.n 常用的类别方法如下InputStreamopenStream()Opens a connection to this URL and returns an InputStream for reading from that connection.URLConnectionopenConnection()Returns a URLConnection object that represents a connection to the remote object referred to by the URL.intgetDefaultPort()Gets the default port number of the protocol associated with this URL.StringgetFile()Gets the file name of this URL.StringgetHost()Gets the host name of this URL, if applicable.StringgetPath()Gets the path part of this URL.intgetPort()Gets the port number of this URL.StringgetProtocol()Gets the protocol name of this URL.StringgetQuery()Gets the query part of this URL.StringgetRef()Gets the anchor (also known as the reference) of this URL.StringgetUserInfo()Gets the userInfo part of this URL. HttpURLConnection类别对象HttpURLConnection为URLConnection的子类别,可用来检查联机状态和取得标头字段的数据,例如浏览程序向服务器提出浏览网页的请求时,服务器响应请求的数据包含HTTP标头信息,HTTP标头提供网页档案的重要信息,如HTTP方法、档案最后修改的日期/时间等,只需检查响应状态,就可以知道网站的联机状态。其类别建置与建构子如下java.lang.Object .URLConnection .HttpURLConnectionpublic abstract class HttpURLConnection extends URLConnectionHttpURLConnection(URL u)Constructor for the HttpURLConnection.n 常用的类别方法如下abstract voiddisconnect()Indicates that other requests to the server are unlikely in the near future.InputStreamgetErrorStream()Returns the error stream if the connection failed but the server sent useful data nonetheless.static booleangetFollowRedirects()Returns a boolean indicating whether or not HTTP redirects (3xx) should be automatically followed.StringgetHeaderField(int n)Returns the value for the nth header field.longgetHeaderFieldDate(String name, long Default)Returns the value of the named field parsed as date.StringgetHeaderFieldKey(int n)Returns the key for the nth header field.StringgetRequestMethod()Get the request getResponseCode()Gets the status code from an HTTP response message.StringgetResponseMessage()Gets the HTTP response message, if any, returned along with the response code from a server.static voidsetFollowRedirects(boolean set)Sets whether HTTP redirects (requests with response code 3xx) should be automatically followed by this class.voidsetRequestMethod(String method)Set the method for the URL request, one of: GET POST HEAD OPTIONS PUT DELETE TRACE are legal, subject to protocol restrictions.n 继承自URLConnection类别的常用方法如下public longgetLastModified() Returns the value of the last-modified header field. The result is the number of milliseconds since January 1, 1970 GMT. public StringgetContentType() Returns the value of the content-type header field. public StringgetContentEncoding() Returns the value of the content-encoding header field. public longgetExpiration() Returns the value of the expires header field. public intgetContentLength() Returns the value of the content-length header field. Example: 建立对象撷取URL地址与网页档案,使用HttpURLConnection类别建立网站的HTTP联机,在检查HTTP标头数据的响应后,确认网站是否联机成功,然后显示最后修改日期/时间和档案类型的content-type标头字段。n 执行范例: 撷取.tw/snow/之网址信息及下载首页档案java Ex03_URLTest .tw/snow/ index.htmimport java.io.*;import .*;import java.util.*;/ 主类别public class Ex03_URLTest / 主程序 public static void main(String args) throws Exception / 取得命令列参数 if (args.length != 2) / 使用说明 String using = 使用: Ex03_URLTest ; System.out.println(using); return; try / 建立URL对象 URL url = new URL(args0);/ 1. System.out.println(主机: + url.getHost(); System.out.println(埠号: + url.getPort(); String protocol = url.getProtocol(); System.out.println(通讯协议: + protocol); System.out.println(档案: + url.getFile(); System.out.println(参考: + url.getRef(); HttpURLConnection.setFollowRedirects(false);/ 2. / 开启http联机 HttpURLConnection http; http = (HttpURLConnection)url.openConnection(); http.setRequestMethod(HEAD); / 取得标头数据 if (http.getResponseCode() = HttpURLConnection.HTTP_OK) / 成功 System.out.println(网站联机成功!); Date md; md = new Date(http.getLastModified(); System.out.println(最后修改日期时间: + md); String type = http.getContentType(); System.out.println(档案类型: + type); else System.out.println(网站联机失败!); catch ( Exception e ) e.printStackTrace(); / 开启URL的输入串流 URL url = new URL(args0);/ 3. DataInputStream in; in = new DataInputStream(url.openStream(); / 开启随机档案的输出串流 RandomAccessFile out;/ 4. out = new RandomAccessFile(args1, rw); try System.out.println(开始下载档案: + args1); byte data; / 复制档案 while ( true )/ 5. data = (byte)in.readByte(); out.writeByte(data); catch ( EOFException e ) System.out.println(档案下载成功.); in.close(); / 关闭串流 out.close(); 批注:1. 建立URL对象,URL建构子内之参数即为网址字符串,接着利用URL的类别方法取得相关信息2. 参数false设定不执行网站转址功能。使用openConnection()方法建立HTTP联机。使用setRequestMethod()方法设定URL请求方法为HEAD。if方法检查HTTP响应状态,以决定是否联机成功,如果联机成功,显示相关信息。3. 建立URL对象,URL建构子内之参数即为网址字符串,建立DataInputStream输入串流,数据来源是使用URL对象的openStream()方法取得,属于Web服务器的档案串流。4. 使用命令列第2个参数建立RanomAccessFile随机档案的输出串流。5. 使用while循环复制档案内容,可以将Web服务器的档案复制到本机计算机。 Socket类别Socket类别可以透过TCP/IP连接一些常用的Internet服务,然后向这些服务送出指令字符串,取得所需信息;常见的Internet服务、埠号及说明如下ServicePortDescriptionEcho7将客户端传送的数据原封不动送回Discard9直接舍弃所有客户端所传送的数据Daytime13传回服务器的日期和时间ftp21FTP服务telnet23Telnet服务Smtp25SMPT寄送邮件服务Whois43Whois服务Finger79Finger服务http80WWW服务pop3110POP3邮件接收服务nntp119News新闻服务n Socket类别的建置与建构子如下java.lang.Object extended by .Socketpublic class Socket extends ObjectSocket()Creates an unconnected socket, with the system-default type of SocketImpl.Socket(InetAddress address, int port) throws IOExceptionCreates a stream socket and connects it to the specified port number at the specified IP address.Socket(InetAddress address, int port, InetAddress localAddr, int localPort) throws IOExceptionCreates a socket and connects it to the specified remote address on the specified remote port.Socket(String host, int port) throws IOExceptionCreates a stream socket and connects it to the specified port number on the named host. Socket(String host, int port, InetAddress localAddr, int localPort) throws IOExceptionCreates a socket and connects it to the specified remote host on the specified remote port.n Note: the port number is in the range of 102565535n 常用来建立联机与取得串流的类别方法,以这些方法配合不同的传输埠号可撷取到不同的信息字符串voidclose()Closes this socket.InputStreamgetInputStream()Returns an input stream for this socket.OutputStreamgetOutputStream()Returns an output stream for this socket.n 取得服务器当地日期时间的方法在Socket建构子输入server网域字符串及13号端口,例如Socket socket = new Socket(server, 13) ;n 透过SMTP寄送电子邮件的方法在Socket建构子输入server网域字符串及25号端口,例如Socket socket = new Socket(server, 25) ;n 利用POP3检查电子邮件的类别方法在Socket建构子输入server网域字符串及110号端口,并可利用LIST指令显示邮件清单,以while循环计算邮件数,例如Socket socket = new Socket(server, 25) ;:int no = 0 ;while (str = in.readLine() != null)if (!(str.equals(“.”)no+ ; elsereturn no ;:n 建立HTTP联机下载网页档案的方法在Socket建构子输入URL地址对象及80号端口,例如Socket socket = new Socket(url.getHost(), 80) ; Example: 建立控制流程以三个case测试Daytime, SMPT, POP3三个埠,但须确认通讯端口之主机服务器已启动,n Case 1测试Daytime,执行时输入1 server,server为测试之服务器网址n Case 2测试SMPT,执行时输入2 SMPT_server email,SMPT_server为寄送邮件服务器地址,email为电子邮件账号n Case 3测试POP3,执行时输入3 POP3_server user pass,POP3_server为收信服务器地址,user和pass则分别为收信者账号与密码。import .*;import java.io.*;/ 主类别public class Ex03_SocketTest / 主程序 public static void main(String args) throws Exception / 取得命令列参数 int i = Integer.parseInt(args0) ; Ex03_SocketTest socketTest = new Ex03_SocketTest() ;/ 1. switch (i)/ 1. / 选择测试型态 case 1: socketTest.getDaytime(args1, args.length-1) ; break ; case 2: socketTest.sendMail(args1, args2, args.length-1); / 寄送邮件 break ; case 3: try / 检查邮件数 int no=socketTest.checkMail(args1, args2, args3, args.length-1); if ( no = 0 ) System.out.println(目前没有任何的邮件!); else System.out.println(目前共有+no+封邮件); catch ( Exception e ) break ; / DayTime Test / 读取时间数据 public void getDaytime(String server, int length)/ 2. if (length != 1) / 使用说明 System.out.println(使用: Ex03_SocketTest ); return; / 可利用ntp2a.mcc.ac.uk作为测试 try / 2. / 建立Socket对象, 端口号为13 Socket socket = new Socket(server, 13); InputStreamReader isr = new InputStreamReader( socket.getInputStream(); BufferedReader in = new BufferedReader(isr); System.out.println(在 + server +的时间: ); / 读取与显示日期时间数据 System.out.println(in.readLine(); socket.close(); / 关闭Socket对象 catch (Exception e) / SMPT Service Test / 寄送电子邮件 public
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
评论
0/150
提交评论