📄 手机与servlet的网络通信技巧.htm
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- saved from url=(0038)http://www.comprg.com.cn/titl_jp02.htm -->
<HTML><HEAD><TITLE>电脑编程技巧与维护</TITLE>
<META content="text/html; charset=gb2312" http-equiv=Content-Type><LINK
href="手机与Servlet的网络通信技巧.files/css_nofree.css" rel=stylesheet type=text/css>
<META content="MSHTML 5.00.2920.0" name=GENERATOR></HEAD>
<BODY class=BodyColor link=#000000>
<TABLE border=0 cellPadding=0 cellSpacing=0 height=129 width=755>
<TBODY>
<TR><!--left starts-->
<TD bgColor=#000033 height=129 width=2><IMG height=1
src="手机与Servlet的网络通信技巧.files/spacer.gif" width=1></TD>
<TD align=middle class=pageContent height=129 vAlign=top width=586><!--top title bar-->
<TABLE border=0 cellPadding=0 cellSpacing=0 width="100%">
<TBODY>
<TR>
<TD align=right class=sideBar width="100%"><IMG height=5
src="手机与Servlet的网络通信技巧.files/blank_img.gif"
width=5></TD></TR></TBODY></TABLE><!--end title bar--><!--page starts-->
<TABLE border=0 cellPadding=0 cellSpacing=1 height=1 width=588>
<TBODY>
<TR>
<TD width=578>
<P style="LINE-HEIGHT: 150%"><FONT color=#04438e size=2>
</FONT>
<TABLE border=0 cellPadding=2 height=1 width="100%">
<TBODY>
<TR>
<TD height=20 width="7%"></TD>
<TD height=20 width="93%"><IMG border=0 height=75
src="手机与Servlet的网络通信技巧.files/jpwz_tit.gif" width=449></TD></TR>
<TR>
<TD colSpan=2 height=20 width="100%"></TD></TR>
<TR>
<TD colSpan=2 height=1 width="100%">
<P align=center class=MsoNormal style="TEXT-ALIGN: center"><B
style="mso-bidi-font-weight: normal"><SPAN
style="FONT-FAMILY: 宋体; FONT-SIZE: 16pt; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">手机与</SPAN></B><B
style="mso-bidi-font-weight: normal"><SPAN lang=EN-US
style="FONT-SIZE: 16pt">Servlet</SPAN><SPAN
style="FONT-FAMILY: 宋体; FONT-SIZE: 16pt; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">的网络通信技巧</SPAN><SPAN
lang=EN-US style="FONT-SIZE: 16pt"><O:P> </O:P></SPAN></B></P>
<P align=center class=MsoNormal
style="TEXT-ALIGN: center"><SPAN lang=EN-US
style="FONT-SIZE: 9pt; mso-spacerun: yes"> </SPAN><SPAN
style="FONT-FAMILY: 宋体; FONT-SIZE: 9pt; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">卢东方</SPAN><SPAN
style="FONT-SIZE: 9pt"> </SPAN><SPAN
style="FONT-FAMILY: 宋体; FONT-SIZE: 9pt; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">王桃群</SPAN><SPAN
lang=EN-US style="FONT-SIZE: 9pt"><O:P> </O:P></SPAN></P>
<P align=left class=MsoNormal
style="TEXT-ALIGN: left; mso-layout-grid-align: none"><SPAN
lang=EN-US
style="FONT-SIZE: 9pt; mso-tab-count: 1">
</SPAN><SPAN
style="COLOR: black; FONT-FAMILY: 宋体; FONT-SIZE: 9pt; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt; mso-ansi-language: ZH-CN">随着越来越多手提电话和个人数字助理开始融入到信息高速公路之上,从移动设备上访问Web站点变得越来越重要。</SPAN><SPAN
style="FONT-FAMILY: 宋体; FONT-SIZE: 9pt; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">只有你实现移动设备与非移动设备互相通信的功能后,你设计的移动设备应用程序才可以称的上是真正有用的。</SPAN><SPAN
style="COLOR: black; FONT-FAMILY: 宋体; FONT-SIZE: 9pt; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt; mso-ansi-language: ZH-CN">在本文中,
我们将通过一个简单的例子来学习如何实现</SPAN><SPAN
style="COLOR: black; FONT-FAMILY: 宋体; FONT-SIZE: 9pt; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt">手机</SPAN><SPAN
style="COLOR: black; FONT-FAMILY: 宋体; FONT-SIZE: 9pt; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt; mso-ansi-language: ZH-CN">与</SPAN><SPAN
lang=EN-US
style="COLOR: black; FONT-FAMILY: 宋体; FONT-SIZE: 9pt; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt">S</SPAN><SPAN
style="COLOR: black; FONT-FAMILY: 宋体; FONT-SIZE: 9pt; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt; mso-ansi-language: ZH-CN">ervlet的网络通信。该程序经</SPAN><SPAN
lang=EN-US
style="COLOR: black; FONT-FAMILY: 宋体; FONT-SIZE: 9pt; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt">tomcat4.0.6、</SPAN><SPAN
style="COLOR: black; FONT-FAMILY: 宋体; FONT-SIZE: 9pt; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt; mso-ansi-language: ZH-CN">J2ME的MIDP简表及SUN的J2ME的无线应用程序开发工具包编译、配置和测试通过,同时在手机模拟器上中文显示正常。<O:P>
</O:P></SPAN></P>
<P align=left class=MsoNormal
style="TEXT-ALIGN: left; mso-layout-grid-align: none"><SPAN
style="COLOR: black; FONT-FAMILY: 宋体; FONT-SIZE: 9pt; mso-hansi-font-family: Times New Roman; mso-tab-count: 1; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt; mso-ansi-language: ZH-CN">
</SPAN><SPAN
style="COLOR: black; FONT-FAMILY: 宋体; FONT-SIZE: 9pt; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt">一些参考书都讲了<SPAN
lang=EN-US>J2ME的网络编程,但我感觉大都有点雷同,并且很少有完整的手机客户端程序以及Servlet服务器端程序,虽然手机与Servlet通信的方法有多种,但经实践与比较,</SPAN></SPAN><SPAN
style="FONT-FAMILY: 宋体; FONT-SIZE: 9pt">我觉得手机和<SPAN
lang=EN-US>Servlet通信比较简单的方法就是DataInputStream.readUTF和DataOutputStream.writeUTF的配对使用,比如手机发送数据可以在Connection的输出流中用DataOutputStream.writeUTF依次写入多个参数,相对应的,Servlet打开请求的输入流(request.getInputStream)用DataInputStream.readUTF来依次读出。返回的参数也一样,Servlet用response.getOutputStream打开输出流,并把返回值依次写入,手机端打开Connection的输出流并读出即可。至于多个参数多个返回值都是没有问题的,只是多次使用writeUTF和readUTF罢了。读者可以参考程序中的注释,以及运行结果来理解手机与Servlet是如何传递与返回参数的以及是如何来进行网络通信的。</SPAN></SPAN><SPAN
lang=EN-US
style="COLOR: black; FONT-FAMILY: 宋体; FONT-SIZE: 9pt; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt"><O:P>
</O:P></SPAN></P>
<P align=left class=MsoNormal
style="TEXT-ALIGN: left; mso-layout-grid-align: none"><SPAN
style="FONT-FAMILY: 'MS Sans Serif'; FONT-SIZE: 10pt; mso-bidi-font-weight: bold; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 'MS Sans Serif'; mso-font-kerning: 0pt; mso-ansi-language: ZH-CN; mso-fareast-font-family: 'MS Sans Serif'">手机客户端</SPAN><SPAN
style="FONT-FAMILY: 'MS Sans Serif'; FONT-SIZE: 10pt; mso-bidi-font-weight: bold; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 'MS Sans Serif'; mso-font-kerning: 0pt; mso-fareast-font-family: 'MS Sans Serif'">:<SPAN
lang=EN-US><O:P> </O:P></SPAN></SPAN></P>
<P align=left class=MsoNormal
style="TEXT-ALIGN: left; mso-layout-grid-align: none"><SPAN
lang=EN-US
style="FONT-FAMILY: 'MS Sans Serif'; FONT-SIZE: 9pt; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 'MS Sans Serif'; mso-font-kerning: 0pt; mso-fareast-font-family: 'MS Sans Serif'">import
javax.microedition.midlet.*;<O:P> </O:P></SPAN></P>
<P align=left class=MsoNormal
style="TEXT-ALIGN: left; mso-layout-grid-align: none"><SPAN
lang=EN-US
style="FONT-FAMILY: 'MS Sans Serif'; FONT-SIZE: 9pt; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 'MS Sans Serif'; mso-font-kerning: 0pt; mso-fareast-font-family: 'MS Sans Serif'">import
javax.microedition.lcdui.*;<O:P> </O:P></SPAN></P>
<P align=left class=MsoNormal
style="TEXT-ALIGN: left; mso-layout-grid-align: none"><SPAN
lang=EN-US
style="FONT-FAMILY: 'MS Sans Serif'; FONT-SIZE: 9pt; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 'MS Sans Serif'; mso-font-kerning: 0pt; mso-fareast-font-family: 'MS Sans Serif'">import
javax.microedition.io.*;<O:P> </O:P></SPAN></P>
<P align=left class=MsoNormal
style="TEXT-ALIGN: left; mso-layout-grid-align: none"><SPAN
lang=EN-US
style="FONT-FAMILY: 'MS Sans Serif'; FONT-SIZE: 9pt; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 'MS Sans Serif'; mso-font-kerning: 0pt; mso-fareast-font-family: 'MS Sans Serif'">import
java.io.*;<O:P> </O:P></SPAN></P>
<P align=left class=MsoNormal
style="TEXT-ALIGN: left; mso-layout-grid-align: none"><SPAN
lang=EN-US
style="FONT-FAMILY: 'MS Sans Serif'; FONT-SIZE: 9pt; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 'MS Sans Serif'; mso-font-kerning: 0pt; mso-fareast-font-family: 'MS Sans Serif'">public
class ClientApp extends MIDlet implements CommandListener
{<O:P> </O:P></SPAN></P>
<P align=left class=MsoNormal
style="TEXT-ALIGN: left; mso-layout-grid-align: none"><SPAN
lang=EN-US
style="FONT-FAMILY: 'MS Sans Serif'; FONT-SIZE: 9pt; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 'MS Sans Serif'; mso-font-kerning: 0pt; mso-fareast-font-family: 'MS Sans Serif'"><SPAN
style="mso-tab-count: 1">
</SPAN>Display display;<SPAN style="mso-spacerun: yes">
</SPAN>TextField tf1,tf2;<SPAN
style="mso-spacerun: yes"> </SPAN>String tf1Str,tf2Str;
Form inputForm,returnForm;<O:P> </O:P></SPAN></P>
<P align=left class=MsoNormal
style="TEXT-ALIGN: left; mso-layout-grid-align: none"><SPAN
lang=EN-US
style="FONT-FAMILY: 'MS Sans Serif'; FONT-SIZE: 9pt; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 'MS Sans Serif'; mso-font-kerning: 0pt; mso-fareast-font-family: 'MS Sans Serif'"><SPAN
style="mso-tab-count: 1">
</SPAN>Command cmdSend,cmdBack;<O:P> </O:P></SPAN></P>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -