TA的每日心情 | 开心 2021-3-12 23:18 |
---|
签到天数: 2 天 [LV.1]初来乍到
|
1.下面的Servlet程序读取手机程序发送过来的两个参数信息,一个是用户账号,另一个是密码,然后将这两个信息返回到
手机程序。

package com.Test;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
- public class HelloWorld extends HttpServlet {
- protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
- // Read the parameters sent from MIDlet
- String acct = req.getParameter("account"), pwd = req
- .getParameter("password");
- if (acct == null || pwd == null) {
- res.sendError(HttpServletResponse.SC_BAD_REQUEST,
- "Unable to read parameters");
- return;
- }
- res.setContentType("text/plain");
- PrintWriter out = res.getWriter();
- out.print(this.getServletInfo() + "
- ");
- out.print("acct:" + acct + "
- pwd:" + pwd);
- out.close();
- }
- protected void doPost(HttpServletRequest req, HttpServletResponse res)
- throws ServletException, IOException {
- doGet(req, res);
- }
- /*--------------------------------------------------
- * Information about servlet
- *-------------------------------------------------*/
- public String getServletInfo() {
- return "Hello,World
";
- }
- }
- 2、下面的J2ME程序通过HTTP连接方式访问servlet,并发送POST的请求,然后将servlet程序的响应信息通过一个Form显示在手机屏幕上。
- /*
- * 使用POST方式访问Servlet
- */
- import javax.microedition.midlet.*;
- import javax.microedition.lcdui.*;
- import javax.microedition.io.*;
- import java.io.*;
- public class HttpPostExample extends MIDlet implements CommandListener {
- private Display display;
- private Form mainForm;
- private Command exitCommand;
- String account = "newuser";
- String password = "123456";
- public HttpPostExample() {
- display = Display.getDisplay(this);
- exitCommand = new Command("Exit", Command.EXIT, 1);
- mainForm = new Form("服务器的信息");
- mainForm.addCommand(exitCommand);
- mainForm.setCommandListener(this);
- }
- public void startApp() {
- display.setCurrent(mainForm);
- //访问服务器servlet
- try {
- postServlet();
- } catch (Exception e) {
- System.out.println(e.toString());
- }
- }
- public void pauseApp() {
- }
- public void destroyApp(boolean unconditional) {
- }
- public void postServlet() throws IOException {
- HttpConnection http = null;
- InputStream iStrm = null;
- OutputStream oStrm = null;
- // Data is passed at the end of url for Post
- String url = "http://127.0.0.1:8080/examples/hello";
- String rawData = "account=long&password=heihei";
-
- try {
- http = (HttpConnection) Connector.open(url);
- http.setRequestMethod(HttpConnection.POST);
- http.setRequestProperty("User-Agent","Profile/MIDP-2.0 Configuration/CLDC-1.1");
- http.setRequestProperty("Content-Language", "en-US");
- http.setRequestProperty("Content-Length",String.valueOf(rawData.length()));//设置参数长度
- http.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
-
-
- oStrm =http.openOutputStream();
- oStrm.write(rawData.getBytes());
- //oStrm.flush();
- if (http.getResponseCode() == HttpConnection.HTTP_OK) {
- iStrm = http.openInputStream();
- // 获得头信息,没有返回
- //获得数据信息
- int length = (int) http.getLength();
- if (length > 0) {
- byte servletData[] = new byte[length];
- iStrm.read(servletData);
- //显示返回信息
- mainForm.append("使用Post方式验证通过:
- " + new String(servletData));
- } else
- mainForm.append("不能访问数据!");
- }
- } catch (Exception e) {
- mainForm.append("网络出错");
- System.out.println(e.toString());
- } finally {
- //关闭连接对象
- if (iStrm != null)
- iStrm.close();
- if (http != null)
- http.close();
- if (oStrm != null)
- oStrm.close();
-
- }
- }
- public void commandAction(Command c, Displayable s) {
- if (c == exitCommand) {
- destroyApp(false);
- notifyDestroyed();
- }
- }
- }
复制代码
源码下载:http://203.93.208.26/kj/cwb/dir7/http1.zip |
|