按过程来描述:
j2me
提交请求给
sevlet-->
sevlet
处理
POST
请求
doPost()-->
j2me
处理回应
只有Post才能提交给sevlet.
1) J2ME提交请求:
String [] endpoints = ChooseCourse.getCourseEndpointURL ();
//
准备URL[http://localhost:8080/WebModule1/server]
String url = endpoints[ChooseCourse.getIndex ()];//
取得长度赋予字符串
conn = (HttpConnection) Connector.open( url );//
设置CONN
连接的URL
conn.setRequestMethod(HttpConnection.POST);//
设置CONN
连接的POST
类型
dout = conn.openDataOutputStream();//
设置CONN
连接的发出请求
dout.writeInt(0); //
设置 opcode=0
,表示取得服务上答案的记录
dout.writeUTF( UpdateToken.getToken () );//
得到代号
dout.flush();//
提交发出请求
din = conn.openDataInputStream();//
设置CONN
连接的接受请求
boolean succ = din.readBoolean();
if ( succ ) {//
循环读入接受的请求
ShowQuestion.setQid ( din.readInt() );//得到ID
ShowQuestion.setQuestion ( din.readUTF() );//得到问题
int numchoices = din.readInt();//得到答案的个数
String [] choices = new String [numchoices];
for (int i=0; i < numchoices; i++) {
choices[i] = din.readUTF();//取得选择的答案列表
}
//
将请求显示出来,利用ShowQuestion
类
ShowQuestion.setChoices(choices);
ShowQuestion.setComment("");
2)
sevlet
处理
POST
请求
doPost()
public void
doPost(HttpServletRequest request,HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("application/binary");
try {
//
准备输入输出流
InputStream in =
(InputStream) request.getInputStream();
OutputStream out =
(OutputStream) response.getOutputStream();
DataInputStream din = new DataInputStream(in);
DataOutputStream dout =
new DataOutputStream(out);
//
按请求发出的顺序读入
int opcode = din.readInt();//读入
opcode
String securityToken = din.readUTF();//
读入安全代号
//通过验证
userid = securityToken.substring(0, securityToken.indexOf(":"));
if ( opcode == 0 ) {//当处理代码为0时,只是查询有什么问题
getQuestion(din, dout);
} else if ( opcode == 1 ) {//插入回答的问题
submitAnswer(din, dout);
}
} catch (Exception e) {
e.printStackTrace();
}
}