用MIDlet激活Servlet

开发 后端
本文介绍用MIDlet激活Servlet,包括介绍用GET操作激活Servlet,并显示结果和Servlet接受用户由手机POST上来的数据。

用MIDlet激活Servlet,你可以象MIDlet激活一个CGI一样激活Servlet,本段将介绍两个例子:

第一个例子用GET操作激活Servlet,并显示结果。
第二个例子是Servlet接受用户由手机POST上来的数据

下面这个例子的内容是,FirstMidletServlet被GET方法激活并返回显示给手机。本例中并没有递交数据给Servlet, Servlet被激活后一会返回字符串“Servlet Invoked”和日期给客户端。

下面是MIDlet的代码FirstMidletServlet.java

  1. import java.io.*;  
  2. import javax.microedition.io.*;  
  3. import javax.microedition.lcdui.*;  
  4. import javax.microedition.midlet.*;  
  5.  
  6. /**  
  7. * An example MIDlet to invoke a CGI script.  
  8. */  
  9.  
  10. public class FirstMidletServlet extends MIDlet {  
  11. private Display display;  
  12. String url = "http://somesite.com/servlet/HelloServlet";  
  13.  
  14. public FirstMidletServlet() {  
  15. display = Display.getDisplay(this);  
  16. }  
  17.  
  18. //Initialization. Invoked when MIDlet activates  
  19. public void startApp() {  
  20. try {  
  21. invokeServlet(url);  
  22. } catch (IOException e) {  
  23. System.out.println("IOException " + e);  
  24. e.printStackTrace();  
  25. }  
  26. }  
  27.  
  28. //Pause, discontinue ....  
  29. public void pauseApp() { }  
  30.  
  31. //Destroy must cleanup everything.  
  32. public void destroyApp(boolean unconditional) { }  
  33.  
  34. //Prepare connection and streams then invoke servlet.  
  35. void invokeServlet(String url) throws IOException {  
  36. HttpConnection c = null;  
  37. InputStream is = null;  
  38. StringBuffer b = new StringBuffer();  
  39. TextBox t = null;  
  40. try {  
  41. c = (HttpConnection)Connector.open(url);  
  42. c.setRequestMethod(HttpConnection.GET);  
  43. c.setRequestProperty("IF-Modified-Since", "20 Jan 2001 16:19:14 GMT");  
  44. c.setRequestProperty("User-Agent", "Profile/MIDP-1.0 Configuration/CLDC-1.0");  
  45. c.setRequestProperty("Content-Language", "en-CA");  
  46. is = c.openDataInputStream();  
  47. int ch;  
  48.  
  49. // receive response and display it in a textbox.  
  50. while ((ch = is.read()) != -1) {  
  51. b.append((char) ch);  
  52. }  
  53. t = new TextBox("First Servlet", b.toString(), 1024, 0);  
  54. } finally {  
  55. if(is!= null) {  
  56. is.close();  
  57. }  
  58. if(c != null) {  
  59. c.close();  
  60. }  
  61. }  
  62. display.setCurrent(t);  
  63. }  

下面是返回“Servlet Invoked”和日期的HelloServlet代码HelloServlet.java

  1. import java.io.*;  
  2. import java.util.*;  
  3. import javax.servlet.*;  
  4. import javax.servlet.http.*;  
  5.  
  6. /**  
  7. * The simplest possible servlet.  
  8. */  
  9.  
  10. public class HelloServlet extends HttpServlet {  
  11. public void doGet(HttpServletRequest request 

以上是用MIDlet激活Servlet的两个例子。

【编辑推荐】

  1. 创建Servlet过滤器的向导
  2. 安装Servlet和JSP开发工具
  3. 扩展Future Response Servlet
  4. Servlet容器的匹配过程
  5. 浅谈user cache Servlet
责任编辑:佚名 来源: 中国IT实验室
相关推荐

2009-07-09 09:54:07

Servlet和CGI

2009-07-03 10:52:33

MIDletJSP页面

2011-05-10 10:25:22

MIDletBlackBerry

2011-05-04 09:29:22

2010-05-08 17:22:03

Black Berry

2010-09-30 09:29:45

J2MEServlet

2015-12-21 16:15:59

微软微软云物联网

2009-06-16 15:56:10

MIDlet生命周期J2ME程序测试

2009-07-06 14:05:50

Servlet编程Servlet显示图片

2009-07-08 09:35:53

Java ServleServlet 3.0

2013-09-02 09:18:59

2009-07-03 10:31:57

什么是ServletServlet API

2009-06-17 15:38:57

java软件安装

2009-07-09 11:27:59

Servlet容器

2009-07-09 15:05:45

Servlet实例

2022-03-31 15:17:04

JavaSocketServlet容器

2009-07-09 13:04:37

Servlet接口

2009-07-10 18:10:18

Jython编写SerJython

2009-07-08 10:12:04

Servlet Con

2009-07-09 13:45:06

Servlet基本结构
点赞
收藏

51CTO技术栈公众号