服务器端程序:
读取手机发送过来的两个参数信息,一个是用户账号account,一个是用户密码password,然后servlet
正确读取两个信息以后把两个参数返回给手机程序。
1: package com.example.servlets;
2:
3: import java.io.IOException;
4: import java.io.PrintWriter;
5:
6: import javax.servlet.ServletException;
7: import javax.servlet.http.HttpServlet;
8: import javax.servlet.http.HttpServletRequest;
9: import javax.servlet.http.HttpServletResponse;
10:
11: /**
12: * Servlet implementation class HelloServlet
13: */
14: public class HelloServlet extends HttpServlet {
15: private static final long serialVersionUID = 1L;
16:
17: /**
18: * @see HttpServlet#HttpServlet()
19: */
20: public HelloServlet() {
21: super();
22: // TODO Auto-generated constructor stub
23: }
24:
25: /**
26: * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
27: * response)
28: */
29: protected void doGet(HttpServletRequest request,
30: HttpServletResponse response) throws ServletException, IOException {
31: // response.getWriter().write("Hello, world!");
32: // 从MIDlet发送的信息中读取参数
33: String acct = request.getParameter("account"), pwd = request
34: .getParameter("password");
35: if (acct == null || pwd == null) {
36: response.sendError(HttpServletResponse.SC_BAD_REQUEST,
37: "Unable to read parameters");
38: return;
39: }
40:
41: // 返回信息给客户端
42: response.setContentType("text/plain");
43: // 以网页输出格式返回信息
44: PrintWriter out = response.getWriter();
45: out.print(this.getServletInfo() + "\n");
46: out.print("acct:" + acct + "\npwd:" + pwd);
47: out.close();
48: }
49:
50: /**
51: * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
52: * response)
53: */
54: protected void doPost(HttpServletRequest request,
55: HttpServletResponse response) throws ServletException, IOException {
56: doGet(request, response);
57: }
58:
59: // 返回服务器信息
60: public String getServletInfo() {
61: return "Hello,World\n\r";
62: }
63:
64: }
65:
GET方式与服务器交互(客户端):
1: import java.io.IOException;
2: import java.io.InputStream;
3: import javax.microedition.io.Connector;
4: import javax.microedition.io.HttpConnection;
5: import javax.microedition.lcdui.Command;
6: import javax.microedition.lcdui.CommandListener;
7: import javax.microedition.lcdui.Display;
8: import javax.microedition.lcdui.Displayable;
9: import javax.microedition.lcdui.Form;
10: import javax.microedition.midlet.MIDlet;
11: import javax.microedition.midlet.MIDletStateChangeException;
12:
13: public class HttpGetExample extends MIDlet implements CommandListener {
14:
15: private Display display;
16: private Command exitCommand;
17: private Form mainForm;
18: private String password = "newuser";// 用户名
19: private String account = "123456";// 用户密码
20:
21: public HttpGetExample() {
22: display = Display.getDisplay(this);
23: exitCommand = new Command("Exit", Command.EXIT, 1);
24: mainForm = new Form("Data from servlet");
25: mainForm.addCommand(exitCommand);
26: mainForm.setCommandListener(this);
27: }
28:
29: protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
30: // TODO Auto-generated method stub
31:
32: }
33:
34: protected void pauseApp() {
35: // TODO Auto-generated method stub
36:
37: }
38:
39: protected void startApp() throws MIDletStateChangeException {
40: display.setCurrent(mainForm);
41: // 访问服务器servlet
42: try {
43: callServlet();
44: } catch (IOException e) {
45: e.printStackTrace();
46: }
47: }
48:
49: private void callServlet() throws IOException {
50: HttpConnection http = null;
51: InputStream iStrm = null;
52: // 使用GET方式连接服务器
53: String url = "http://127.0.0.1:8080/ExampleWebProject/HelloServlet"
54: + "?" + "account=" + account + "&" + "password=" + password;
55: try {
56: http = (HttpConnection) Connector.open(url);
57: // 使用HttpConnection.GET方式
58: // 发送Gety请求
59: http.setRequestMethod(HttpConnection.GET);
60:
61: // 服务器响应
62: if (http.getResponseCode() == HttpConnection.HTTP_OK) {
63: iStrm = http.openInputStream();
64: // 获得数据字节数
65: int length = (int) http.getLength();
66: // 获得数据信息
67: if (length > 0) {
68: byte servletData[] = new byte[length];
69: iStrm.read(servletData);
70: // 显示返回信息
71: mainForm.append("验证通过:\n" + new String(servletData));
72: } else {
73: mainForm.append("不能访问数据!");
74: }
75: }
76: } catch (Exception e) {
77: mainForm.append("网络错误");
78: e.printStackTrace();
79: } finally {
80: // 关闭连接对像
81: if (iStrm != null) {
82: iStrm.close();
83: }
84: if (http != null) {
85: http.close();
86: }
87: }
88:
89: }
90:
91: public void commandAction(Command c, Displayable d) {
92: if (c == exitCommand) {
93: try {
94: destroyApp(false);
95: notifyDestroyed();
96: } catch (MIDletStateChangeException e) {
97: // TODO Auto-generated catch block
98: e.printStackTrace();
99: }
100: }
101: }
102:
103: }
104:
POST方式与服务器交互(客户端):
1: import java.io.IOException;
2: import java.io.InputStream;
3: import java.io.OutputStream;
4:
5: import javax.microedition.io.Connector;
6: import javax.microedition.io.HttpConnection;
line-height: 18px; color: #4f81b
分享到:
相关推荐
NULL 博文链接:https://2528.iteye.com/blog/1066483
J2me.js 的当前目标是:运行 MIDlets,模拟电话ME Feature MR4 (b01)实现方式保持 j2me.js 简单和精悍 标签:j2mejs 分享 window._bd_share_config ...
j2me 基于HttpConnect与KXML与服务器能讯.pdf j2me 基于HttpConnect与KXML与服务器能讯.pdf j2me 基于HttpConnect与KXML与服务器能讯.pdf j2me HttpConnect KXML webservice .net j2me ...
j2me http demo,几个小程序案例
分享:J2ME游戏代码 分享:J2ME游戏代码 分享:J2ME游戏代码分享:J2ME游戏代码 分享:J2ME游戏代码 分享:J2ME游戏代码分享:J2ME游戏代码 分享:J2ME游戏代码 分享:J2ME游戏代码分享:J2ME游戏代码 分享:J2ME...
J2ME与J2EE实现用户登录交互 j2me入门实例
java做的j2me与j2ee进行交互 用手机客户端进行登录服务器,然后返回消息进行交互.
J2ME文件上传到服务器,用dos,也只能传小文件,需要在服务器端写保存文件的程序。
J2ME开发 J2ME开发 J2ME开发 J2ME开发
J2ME Game Programming 英文版 from url:http://j2megame.atw.hu/
J2ME游戏编程:TP319:G899 W511.iso 随书光盘,欢迎大家下载 大家共享^A^
j2me实现用户登陆的交互界面,初学者可以借鉴,学习程序的结果及具体编译等
郭可华J2ME实战教程:ppt和对应源码,非常好的入门材料!
J2ME API J2ME API J2ME API J2ME API J2ME API
j2me手机基于服务器的网络象棋游戏源代码
有源代码,可是用的时候要修改地址!有完整的课程设计报告
j2me socket方式下载图片,附带源码工程 注释
j2me教程,比较基础,容易掌握,很容易上手,对初学者很有帮助。。
J2ME学习资料打包下载(j2me游戏开发.rar,J2ME中文教程.rar...)
J2ME,J2EE与Android开发学习资料;J2ME,J2EE与Android开发学习资料;J2ME,J2EE与Android开发学习资料