⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 invokejspmidlet.java

📁 JAVA编程百例书中各章节的所有例子的源代码,包括套接字编程
💻 JAVA
字号:
package ch09.section10;

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

public class InvokeJSPMidlet
    extends MIDlet
    implements CommandListener {
  Display display = null;

// name 字段
  TextField name = null;

  Form form;
//jsp页面位于本地机
  String url = "http://127.0.0.1:8080/examples/jsp/today.jsp"; ;
  static final Command callCommand = new Command("date?", Command.OK, 2);
  static final Command clearCommand = new Command("clear", Command.STOP, 2);

  String myname;

  public InvokeJSPMidlet() {
    display = Display.getDisplay(this);
    name = new TextField("Name:", " ", 25, TextField.ANY);
    form = new Form("Invoke JSP");
  }

//程序启动时,创建窗体
  public void startApp() throws MIDletStateChangeException {
    form.append(name);
    form.addCommand(clearCommand);
    form.addCommand(callCommand);
    form.setCommandListener(this);
    display.setCurrent(form);
  }

  public void pauseApp() {
  }

  public void destroyApp(boolean unconditional) {
    notifyDestroyed();
  }

//访问JSP页面
  void invokeJSP(String url) throws IOException {
    HttpConnection c = null;
    InputStream is = null;
    OutputStream os = null;
    StringBuffer b = new StringBuffer();
    TextBox t = null;
    try {
      //建立连接
      c = (HttpConnection) Connector.open(url);
      c.setRequestMethod(HttpConnection.POST);
      //设置属性
      c.setRequestProperty("IF-Modified-Since", "29 Dec 2001 15:17:19 GMT");
      c.setRequestProperty("User-Agent",
                           "Profile/MIDP-1.0 Configuration/CLDC-1.0");
      c.setRequestProperty("Content-Language", "en-CA");
      c.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

      //写入数据
      os = c.openOutputStream();
      os.write( ("name=" + myname).getBytes());
      os.flush();

      is = c.openDataInputStream();
      int ch;
      //读取数据
      while ( (ch = is.read()) != -1) {
        b.append( (char) ch);
        System.out.print( (char) ch);
      }
      t = new TextBox("Date", b.toString(), 1024, 0);
      t.setCommandListener(this);
    }
    finally {
      if (is != null) {
        is.close();
      }
      if (os != null) {
        os.close();
      }
      if (c != null) {
        c.close();
      }
    }
    display.setCurrent(t);
  }

  //响应按钮事件
  public void commandAction(Command c, Displayable d) {
    String label = c.getLabel();
    if (label.equals("clear")) {
      destroyApp(true);
    }
    else if (label.equals("date?")) {
      myname = name.getString();
      try {
        invokeJSP(url);
      }
      catch (IOException e) {
      }
    }
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -