Light  Rain serena

«October 2025»
1234
567891011
12131415161718
19202122232425
262728293031


公告

期待

超级好友 Rachel


我的分类(专题)

首页(262)
知识积累(40)
乐瑟温柔(108)
经验杂谈(20)
良辰吉日(2)
杂七杂八(57)
天景共赏(10)
感触文字(23)


最新日志
sunshine girl
过云雨
summer whisper
边走边唱
岛歌
不必在乎我是谁
如果有来生
Fing Fing 下
大明宫
我没有魅力

最新回复
回复:边界类、控制类、实体类
回复:边界类、控制类、实体类
回复:边界类、控制类、实体类
回复:《极地特快》插曲《When Chr
回复:边界类、控制类、实体类
回复:不必在乎我是谁
回复:边界类、控制类、实体类
回复:如果有来生
回复:Fing Fing 下
回复:我没有魅力

留言板
签写新留言

老大&老师,我想你.....
好文好多阿
886电台 好听
cai
您的子域名已开通
hi
祝福
祝福

你好

统计
blog名称:小雨
日志总数:262
评论数量:1273
留言数量:15
访问次数:4686680
建立时间:2005年1月8日

链接

有事?
音乐,永不厌倦
宁静的心情驿站
我的声音
TIPOblog
tipo bbs
appleVB
网页设计
英语学习2
英语学习3
FLASH
网络书籍
网络电台



我的博客朋友
最有技术的blog
xenium
花差花差
瓜少
span
a lai
琦少
陈少
v
阿当
http://www.ypb.cc/
痞子若鱼
另一个同行
年轻人bcims

 




W3CHINA Blog首页    管理页面    写新日志    退出

[知识积累]j2me and servlet
小雨 发表于 2007/3/29 1:07:01

1 j2me /** HttpMidlet.java*/import javax.microedition.midlet.*;import javax.microedition.lcdui.*;import javax.microedition.io.*;import java.io.*; public class test extends MIDlet implements CommandListener,Runnable {//使用默认的URL。用户可以从图形用户接口改变这个值private static String defaultURL = "http://localhost:7001/applications/testServlet"; // 主MIDP 显示private Display myDisplay = null; // 输入URL的图形用户接口组件private Form requestScreen;private TextField requestField; // 用于提交请求的图形用户接口组件private List list;private String[] menuItems; // 用于显示服务器响应的图形用户接口组件private Form resultScreen;private StringItem resultField; //用于requestScreen的"send"按钮Command sendCommand;// 用于requestScreen的"exit"按钮Command exitCommand;// 用于requestScreen的"back"按钮Command backCommand; public test(){// 初始化图形用户接口组件myDisplay = Display.getDisplay( this );sendCommand = new Command( "SEND", Command.OK, 1 );exitCommand = new Command( "EXIT", Command.OK, 1 );backCommand = new Command( "BACK", Command.OK, 1 ); //显示请求的URLrequestScreen = new Form( "Type in a URL:" );requestField = new TextField( null, defaultURL, 100, TextField.URL );requestScreen.append( requestField );requestScreen.addCommand( sendCommand );requestScreen.addCommand( exitCommand );requestScreen.setCommandListener( this ); // 选择想要的HTTP请求方法menuItems = new String[] {"GET Request", "POST Request"}; list = new List( "Select an HTTP method:", List.IMPLICIT, menuItems, null );list.setCommandListener( this ); // 先是从服务器上收到的信息resultScreen = new Form( "Server Response:" );resultScreen.addCommand( backCommand );resultScreen.setCommandListener( this ); }//结束HttpMidlet() public void startApp() {myDisplay.setCurrent(requestScreen);}//结束 startApp()public void run(){   String result; if (list.getSelectedIndex() == 0 ) // 发送一个 GET 请求到服务器 result = sendHttpGet( requestField.getString()); else // 发送一个 POST 请求到服务器 result = sendHttpPost( requestField.getString()); resultField = new StringItem( null, result ); resultScreen.append(resultField); myDisplay.setCurrent(resultScreen);}void start()  {   //display( "Starting to Connect to Server..." );   Thread thread1 = new Thread( this );   try    {    thread1.start();   }   catch( Exception e )   {//    done( "Exception " + e + " trying to start thread." );   }  }public void commandAction( Command com, Displayable disp ) {// 当用户点击"send"按钮if ( com == sendCommand ) {myDisplay.setCurrent( list );} else if ( com == backCommand ) {requestField.setString( defaultURL );myDisplay.setCurrent( requestScreen );} else if ( com == exitCommand ) {destroyApp( true );notifyDestroyed();}//结束 if ( com == sendCommand ) if ( disp == list && com == List.SELECT_COMMAND ) { this.start();}//结束if ( dis == list && com == List.SELECT_COMMAND )}//结束 commandAction( Command, Displayable ) private String sendHttpGet( String url ){HttpConnection hcon = null;DataInputStream dis = null;StringBuffer responseMessage = new StringBuffer(); try {//使用READ权限的标准的 HttpConnectionhcon = ( HttpConnection )Connector.open( url ); //从HttpConnection取得一个 DataInputStreamdis = new DataInputStream( hcon.openInputStream() ); // 从服务器上取回响应int ch;while ( ( ch = dis.read() ) != -1 ) {responseMessage.append( (char) ch );}//结束while ( ( ch = dis.read() ) != -1 )}catch( Exception e ){e.printStackTrace();responseMessage.append( "ERROR" );} finally {try {if ( hcon != null ) hcon.close();if ( dis != null ) dis.close();} catch ( IOException ioe ) {ioe.printStackTrace();}//结束try/catch }//结束try/catch/finallyreturn responseMessage.toString();}//结束sendHttpGet( String ) private String sendHttpPost( String url ){  HttpConnection hcon = null; DataInputStream dis = null; DataOutputStream dos = null; StringBuffer responseMessage = new StringBuffer(); // 请求体 String requeststring = "This is a POST.";  try {    // 使用读写权限的 HttpConnection hcon = ( HttpConnection )Connector.open( url, Connector.READ_WRITE );  //设置请求方法为POST hcon.setRequestMethod( HttpConnection.POST ); // 取得发送请求字符串的DataOutputStream dos = hcon.openDataOutputStream();     byte[] request_body = requeststring.getBytes(); // 发送请求字符串到服务器 for( int i = 0; i < request_body.length; i++ ) { dos.writeByte( request_body[i] ); }//结束 for( int i = 0; i < request_body.length; i++ ) //data_Out.writeUTF( text ); //   data = byte_Out.toByteArray(); // 取得做为接收服务器响应的DataInputStream  dis = new DataInputStream( hcon.openInputStream() );  // 从服务器上取回响应 int ch; while( ( ch = dis.read() ) != -1 ) { responseMessage.append( (char)ch ); }//结束while( ( ch = dis.read() ) != -1 ) {; } catch( Exception e ) { e.printStackTrace(); responseMessage.append( "ERROR" );  System.out.println(url); } finally { // 释放输入输出流和HTTP连接 try { if( hcon != null ) hcon.close(); if( dis != null ) dis.close(); if( dos != null ) dos.close(); } catch ( IOException ioe ) { ioe.printStackTrace(); }//结束try/catch  }//结束try/catch/finally System.out.println("zj"); System.out.println(responseMessage.toString()); return responseMessage.toString();}//结束sendHttpPost( String ) public void pauseApp() {}//结束pauseApp() public void destroyApp( boolean unconditional ) {myDisplay = null;requestScreen = null;requestField = null;resultScreen = null;resultField = null;}//结束 destroyApp( boolean )}//结束HttpMidlet 2 servlet package zj;import java.io.*;import javax.servlet.http.*;import javax.servlet.*; public class testServlet extends HttpServlet{public void doPost (HttpServletRequest request, HttpServletResponse response)       throws ServletException, IOException  {     ServletInputStream input_Stream = request.getInputStream();  DataInputStream d_Inputstream = new DataInputStream(input_Stream );     int ch;     StringBuffer responseMessage=new StringBuffer();  while( ( ch = d_Inputstream.read() ) != -1 ) {  responseMessage.append( (char)ch );  }//结束while( ( ch = dis.read() ) != -1 ) {;       // String data_MIDlet = d_Inputstream.readUTF();   d_Inputstream.close();               ByteArrayOutputStream byte_Out =  new ByteArrayOutputStream();  DataOutputStream data_Out = new DataOutputStream( byte_Out );   data_Out.writeUTF("zj");  byte[] data = byte_Out.toByteArray();  // Set the response headers and data...  response.setContentType("application/octet-stream" );  response.setContentLength(data.length );  response.setStatus(response.SC_OK);  OutputStream out_Stream = response.getOutputStream();  out_Stream.write( data );  out_Stream.close();   }}

阅读全文(3818) | 回复(1) | 编辑 | 精华

回复:j2me  and servlet
痞子若鱼发表评论于2007/3/30 23:06:15

呵呵 好久没上来看了`` 路过`看望哈`随便灌水

个人主页 | 引用回复 | 主人回复 | 返回 | 编辑 | 删除

» 1 »

发表评论:
昵称:
密码:
主页:
标题:
验证码:  (不区分大小写,请仔细填写,输错需重写评论内容!)
站点首页 | 联系我们 | 博客注册 | 博客登陆

Sponsored By W3CHINA
W3CHINA Blog 0.8 Processed in 0.039 second(s), page refreshed 144834357 times.
《全国人大常委会关于维护互联网安全的决定》  《计算机信息网络国际联网安全保护管理办法》
苏ICP备05006046号