J2ME手机与服务器交互的一个Bean
J2ME手机与服务器交互的一个Bean 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(){}
//清除资源
}