J2ME HTTP方式与服务器交互信息:GET方式和POST方式

服务器端程序:

读取手机发送过来的两个参数信息,一个是用户账号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;
  7:  import javax.microedition.lcdui.Command;
  8:  import javax.microedition.lcdui.CommandListener;
  9:  import javax.microedition.lcdui.Display;
 10:  import javax.microedition.lcdui.Displayable;
 11:  import javax.microedition.lcdui.Form;
 12:  import javax.microedition.midlet.MIDlet;
 13:  import javax.microedition.midlet.MIDletStateChangeException;
 14:   
 15:  public class HttpPostExamlpe extends MIDlet implements CommandListener {
 16:   
 17:      private Command exitcommand;
 18:      private Display display;
 19:      private Form mainForm;
 20:   
 21:      public HttpPostExamlpe() {
 22:          display = Display.getDisplay(this);
 23:          exitcommand = new Command("Exit", Command.EXIT, 1);
 24:   
 25:          mainForm = new Form("服器的信息");
 26:          mainForm.addCommand(exitcommand);
 27:          mainForm.setCommandListener(this);
 28:      }
 29:   
 30:      protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
 31:          // TODO Auto-generated method stub
 32:  
 33:      }
 34:   
 35:      protected void pauseApp() {
 36:          // TODO Auto-generated method stub
 37:  
 38:      }
 39:   
 40:      protected void startApp() throws MIDletStateChangeException {
 41:          display.setCurrent(mainForm);
 42:          // 访问服务器servlet
 43:          try {
 44:              postServlet();
 45:          } catch (IOException e) {
 46:              e.printStackTrace();
 47:          }
 48:      }
 49:   
 50:      private void postServlet() throws IOException {
 51:          HttpConnection http = null;
 52:          InputStream iStrm = null;
 53:          OutputStream oStrm = null;
 54:          // 使用Post方式访问服务器
 55:          String url = "http://127.0.0.1:8080/ExampleWebProject/HelloServlet";
 56:          // 要向服务器提供的信息
 57:          String rawData = "account=long&password=heihei";
 58:   
 59:          try {
 60:              http = (HttpConnection) Connector.open(url);
 61:              // 设置连接属性.
 62:              http.setRequestMethod(HttpConnection.POST);
 63:              // "User-Agent"头信息包含了向服务器请求数据的客户端的类型信息
 64:              http.setRequestProperty("User-Agent",
 65:                      "Profile/MIDP-2.0 Configuration/CLDC-1.1");
 66:              http.setRequestProperty("Content-Language", "en-US");
 67:              http.setRequestProperty("Content-Length", String.valueOf(rawData
 68:                      .length()));
 69:              // 按照属性/数值对应方式发送数据
 70:              http.setRequestProperty("Content-Type",
 71:                      "application/x-www-form-urlencoded");
 72:              oStrm = http.openOutputStream();
 73:              // 发送数据
 74:              oStrm.write(rawData.getBytes());
 75:              // 连接成功
 76:              if (http.getResponseCode() == HttpConnection.HTTP_OK) {
 77:                  iStrm = http.openInputStream();
 78:                  // 获得数据信息
 79:                  int length = (int) http.getLength();
 80:                  if (length > 0) {
 81:                      byte[] servletData = new byte[length];
 82:                      iStrm.read(servletData);
 83:   
 84:                      // 显示返回信息
 85:                      mainForm.append("使用Post方式验证通过: /n"
 86:                              + new String(servletData));
 87:                  } else {
 88:                      mainForm.append("不能访问数据!");
 89:                  }
 90:              }
 91:          } catch (Exception e) {
 92:              mainForm.append("网络出错");
 93:              e.printStackTrace();
 94:          } finally {
 95:              // 关闭连接对像
 96:              if (iStrm != null) {
 97:                  iStrm.close();
 98:              }
 99:              if (http != null) {
100:                  http.close();
101:              }
102:              if (oStrm != null) {
103:                  oStrm.close();
104:              }
105:          }
106:   
107:      }
108:   
109:      // 处理按键事件
110:      public void commandAction(Command c, Displayable d) {
111:          if (c == exitcommand) {
112:              try {
113:                  destroyApp(false);
114:                  notifyDestroyed();
115:              } catch (MIDletStateChangeException e) {
116:                  e.printStackTrace();
117:              }
118:          }
119:      }
120:   
121:  }
122:   
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值