服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

手机与servlet的网络通信技巧

    随着越来越多手提电话和个人数字助理开始融入到信息高速公路之上,从移动设备上访问web站点变得越来越重要。只有你实现移动设备与非移动设备互相通信的功能后,你设计的移动设备应用程序才可以称的上是真正有用的。在本文中, 我们将通过一个简单的例子来学习如何实现手机与servlet的网络通信。该程序经tomcat4.0.6、j2me的midp简表及sun的j2me的无线应用程序开发工具包编译、配置和测试通过,同时在手机模拟器上中文显示正常。

    一些参考书都讲了j2me的网络编程,但我感觉大都有点雷同,并且很少有完整的手机客户端程序以及servlet服务器端程序,虽然手机与servlet通信的方法有多种,但经实践与比较,我觉得手机和servlet通信比较简单的方法就是datainputstream.readutf和dataoutputstream.writeutf的配对使用,比如手机发送数据可以在connection的输出流中用dataoutputstream.writeutf依次写入多个参数,相对应的,servlet打开请求的输入流(request.getinputstream)用datainputstream.readutf来依次读出。返回的参数也一样,servlet用response.getoutputstream打开输出流,并把返回值依次写入,手机端打开connection的输出流并读出即可。至于多个参数多个返回值都是没有问题的,只是多次使用writeutf和readutf罢了。读者可以参考程序中的注释,以及运行结果来理解手机与servlet是如何传递与返回参数的以及是如何来进行网络通信的。

  手机客户端:

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.io.*;
import java.io.*;
public class clientapp extends midlet implements commandlistener
{
display display; textfield tf1,tf2; string tf1str,tf2str;
form inputform,returnform;
command cmdsend,cmdback;
final static string defaulturl = "http://localhost:8080/examples/servlet/servletapp";
public clientapp()
{
display = display.getdisplay(this);
tf1 = new textfield("input first param:","卢东方",20,textfield.any);
tf2 = new textfield("input second param:","王桃群",20,textfield.any);
cmdsend = new command("send",command.screen,1);
cmdback = new command("back",command.screen,1);
inputform = new form("pls input the param:");
inputform.append(tf1); inputform.append(tf2);
inputform.addcommand(cmdsend);
inputform.setcommandlistener(this);
}
public void startapp() throws midletstatechangeexception
{
display.setcurrent(inputform);
}
public void invokeservlet(string url) throws ioexception
{
httpconnection hc = null; dataoutputstream dos = null;
datainputstream dis = null;
try{
hc = (httpconnection)connector.open(url,connector.read_write);
//设置请求属性
hc.setrequestmethod(httpconnection.post);
//设置为post请求方式,默认的请求方式是get
hc.setrequestproperty("if-modified-since","15 oct 2003 08:47:14 gmt");
hc.setrequestproperty("user-agent","profile/midp-1.0 configuration/cldc-1.0");
hc.setrequestproperty("content-language","en-ca");
hc.setrequestproperty("content-type","application/x-www-form-urlencoded");
hc.setrequestproperty("connection","keep-alive");
//connection头可以控制midlet和web服务器之间保持"keep alive"特色。
"keep alive"特色是指在midlet和web服务器间始终使用同一个http连接来多次传递数据
(在通常情况下,http是无连接的协议,每次数据传输完毕后都将断开连接,
而下次传递数据之前将重新建立连接)
//发送请求参数到servlet
dos = hc.opendataoutputstream();
dos.writeutf(tf1str); dos.writeutf(tf2str);
//用于发送请求参数给servlet
system.out.println("手机传递给servlet的第一个参数为:"+ tf1str);
//主要起调试的作用,调试的结果将显示在wtk的控制台中
system.out.println("手机传递给servlet的第一个参数为:"+ tf2str);
dos.flush(); dos.close();
//接收servlet响应数据
ds = new datainputstream(hc.openinputstream());
string return1str = dis.readutf(); string return2str = dis.readutf();
system.out.println("手机接收到servlet端传来的第一个参数为:" + return1str);
//主要起调试的作用,调试的结果将显示在wtk的控制台中
system.out.println("手机接收到servlet端传来的第二个参数为:" + return2str);
returnform = new form("返回的结果");
returnform.append(return1str); returnform.append(" ");
//将返回的结果append到resultform中
returnform.append(return2str); returnform.addcommand(cmdback);
returnform.setcommandlistener(this);
}finally{
if (dis != null) {dis.close();} if (dos != null) {dos.close();}
if (hc != null) {hc.close();}
}
display.setcurrent(returnform);
}
public void pauseapp(){}
public void destroyapp(boolean unconditional){}
public void commandaction(command c,displayable d){
if(c == cmdback){display.setcurrent(inputform);}
if(c == cmdsend){tf1str = tf1.getstring(); tf2str = tf2.getstring();
try{ invokeservlet(defaulturl);
}catch(exception e){system.out.println(e.getmessage());}
}
}
}



servlet服务器:

import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class servletapp extends httpservlet{
public void doget(httpservletrequest request,httpservletresponse response)
throws ioexception,servletexception
{
response.setcontenttype("text/html; charset=gbk");
//设置响应属性
//接收客户端的请求
inputstream is = request.getinputstream();
datainputstream dis = new datainputstream(is);
string tf1str = dis.readutf();string tf2str = dis.readutf();
system.out.println("servlet端接收到手机传来的第一个参数为:" + tf1str);
// 主要起调试的作用,调试的结果显示在tomcat的启动dos窗口中
system.out.println("servlet端接收到手机传来的第二个参数为:" + tf2str);
//对接收的参数进行处理
string return1str = tf1str.concat(":早上好!");
//在接收到的参数后连接字符串
string return2str = tf2str.concat(":晚上好!");
//发送处理后的参数给手机
dataoutputstream dos = new dataoutputstream(response.getoutputstream());
dos.writeutf(return1str);dos.writeutf(return2str);
system.out.println("servlet传递给手机的第一个参数为:" + return1str);
system.out.println("servlet传递给手机的第二个参数为:" + return2str);
}
public void dopost(httpservletrequest request,httpservletresponse response)
throws
servletexception,ioexception{
doget(request,response);
}
public void destroy(){}
//清除资源
}

扫描关注微信公众号