J2ME手機與伺服器互動的一個Bean

javaprogramers發表於2006-05-12

ServletApp.java

import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class ServletApp extends HttpServlet{
public void doGet(HttpServletRequest request,HttpServletResponse response)
throws IOException,ServletException

{
long usersize=0;
String return1Str="測試成功";
response.setContentType("text/html; charset=GBK");
//設定響應屬性

//接收客戶端的請求
InputStream is = request.getInputStream();
DataInputStream dis = new DataInputStream(is);
String tf1Str = dis.readUTF();

//處理客戶端資訊
if(!tf1Str.equals("^*")){
System.out.println("servlet端接收到手機傳來的第一個引數為:" + tf1Str);
File i = new File("E://jakarta-tomcat-5.0.28//webapps//servlets-examples//WEB-INF//classes//input.txt");
DataOutputStream in = new DataOutputStream(new FileOutputStream(i));
in.writeUTF(tf1Str);
// 將新資訊寫入input.txt.
in.close();
}


File f = new File("E://jakarta-tomcat-5.0.28//webapps//servlets-examples//WEB-INF//classes//user.txt");
InputStreamReader  read = new InputStreamReader (new FileInputStream(f));
BufferedReader reader=new BufferedReader(read);
while (reader.readLine() != null) {return1Str= reader.readLine();}


//改變檢測數值為當前檔案長度
System.out.println("servlet傳遞給手機的第一個引數為:" + return1Str);
DataOutputStream dos = new DataOutputStream(response.getOutputStream());

if(usersize!= f.length())
{usersize= f.length();
dos.writeUTF(return1Str);}
else
{//return1Str="^";
dos.writeUTF("^");
}
//如果檔案大小未改變則設回覆資訊為"^"(無資訊標誌)
//傳送處理後的引數給手機
}
public void doPost(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException {doGet(request,response);}

 

public void destroy(){}
//清除資源
}

 
                         

相關文章