如何在MIDlet中POST文本数据给Servlet

下边代码POST文本数据给Servlet,然后显示响应结果

import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
import javax.microedition.io.*;
import java.io.*;
import java.util.Vector;

public class MidletServlet extends MIDlet implements CommandListener {
    Display display = null;
    Form form = null;
    TextField tb = null;
    String str = null;
    String url = "http://localhost:8080/servlets-examples/servlet/getText";
    Command backCommand = new Command("Back", Command.BACK, 0);
    Command submitCommand = new Command("Submit", Command.OK, 2);
    Command exitCommand = new Command("Exit", Command.STOP, 3);
    private Test test;
   
    public MidletServlet() {}
   
    public void startApp() throws MIDletStateChangeException {
        display = Display.getDisplay(this);
        form = new Form("Request Servlet");
        tb = new TextField("Please input text: ","",30,TextField.ANY );
        form.append(tb);
        form.addCommand(submitCommand);
        form.addCommand(exitCommand);
        form.setCommandListener(this);
        display.setCurrent(form);
    }
   
    public void pauseApp() {}
   
    public void destroyApp(boolean unconditional) {}
   
    public void commandAction(Command c, Displayable d) {
        if (c == exitCommand) {
            destroyApp(true);
            notifyDestroyed();
        } else if (c == backCommand) {
            display.setCurrent(form);
        } else if (c == submitCommand) {
            str = tb.getString();
            test  = new Test(this);
            test.start();
            test.getServlet(str);
        }
    }
   
   
    class Test implements Runnable {
        MidletServlet midlet;
        private Display display;
        String text;
       
        public Test(MidletServlet midlet) {
            this.midlet = midlet;
            display = Display.getDisplay(midlet);
        }
       
        public void start() {
            Thread t = new Thread(this);
            t.start();
        }

        public void run() {
            StringBuffer sb = new StringBuffer();
            try {
                HttpConnection c = (HttpConnection) Connector.open(url);
                c.setRequestProperty(
                  "User-Agent","Profile/MIDP-1.0, Configuration/CLDC-1.0");    
                c.setRequestProperty("Content-Language","en-US");
                //ÉèÖÃÇëÇó·½·¨ÎªPOST
                c.setRequestMethod(HttpConnection.POST);
                c.setRequestProperty("Content-Length", Integer
     .toString((text.trim()).getBytes().length));
                DataOutputStream os =
                        (DataOutputStream)c.openDataOutputStream();
                os.writeUTF(text.trim());
                os.flush();
                os.close();        
                //»ñµÃÏìÓ¦
                DataInputStream is =(DataInputStream)c.openDataInputStream();
                int ch;
                sb = new StringBuffer();
                while ((ch = is.read()) != -1) {
                    sb.append((char)ch);
                }
                showAlert(sb.toString());
                is.close();
                c.close();
            } catch (Exception e) {
                showAlert(e.getMessage());
            }
        }
                /* ²É¼¯Êý¾ÝÈ»ºó´«µÝ¸øServlet */
        public void getServlet(String text) {
            this.text = text;
        }
       
        /* Display Error On screen*/
        private void showAlert(String err) {
            Alert a = new Alert("");
            a.setString(err);
            a.setTimeout(Alert.FOREVER);
            display.setCurrent(a);
        }
    };

//Servlet ´úÂë
import java.io.*;
import java.text.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class getText extends HttpServlet {
   
    public void init() {
    }
   
    public void doPost(HttpServletRequest request,
            HttpServletResponse response) throws ServletException,
            IOException {
       
        DataInputStream in =
                new DataInputStream((InputStream)request.getInputStream());
       
        String text = in.readUTF();
        String message;
        try {
            message = "100 ok";
        } catch (Throwable t) {
            message = "200 " + t.toString();
        }
        response.setContentType("text/plain");
        response.setContentLength(message.length());
        PrintWriter out = response.getWriter();
        out.println(message);
        in.close();
        out.close();
        out.flush();
    }
   
    public void doGet(HttpServletRequest request,
            HttpServletResponse response) throws ServletException,
            IOException {
       
        doPost(request,response);
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值