1.创建自己的MyServletContextListener.java:
package yybwb; import java.net.ServerSocket; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; public class MyServletContextListener implements ServletContextListener { //这里使该类实现 ServletContextListener监听接口,在Tomcat启动时会调用实现该接口 //的 contextInitialized方法 ServerSocket ss = null;//声明ServerSocket对象 ServerThread st = null;//声明ServerThread对象 //创建服务线程,并将其启动,本方法在tomca启动时调用 public void contextInitialized(ServletContextEvent sce){ try{ ss = new ServerSocket(8888); st = new ServerThread(ss); st.start(); }catch(Exception e){ e.printStackTrace(); } } //停止服务线程,本方法在tomca停止时调用 public void contextDestroyed(ServletContextEvent sce){ try{ st.flag = false; ss.close(); ss = null; st = null; }catch(Exception e){ e.printStackTrace(); } } }
2.在Web.xml中配置上面的监听类
<Listener> <Listener-class>XX.MyServletContextListener</Listener-class> </Listener>
3.创建服务线程ServerThread类如:
package yybwb; import java.net.ServerSocket; import java.net.Socket; import java.net.SocketException; public class ServerThread extends Thread{ public ServerSocket ss; //声明ServerSocket对象 public boolean flag = false; //循环标志位 public ServerThread(ServerSocket ss){ //构造器 this.ss = ss; flag = true; } //run方法创建ServerSocket 监听 public void run(){ while(flag){ try{ Socket socket = ss.accept(); //客户连接后,创建代理线程 ServerAgent,负责与该客户的通讯 ServerAgent sa = new ServerAgent(socket); sa.start(); } catch(SocketException se){ try{ ss.close(); ss = null; System.out.println("ServerSocket closed"); }catch(Exception ee){ ee.printStackTrace(); } } catch(Exception e){ e.printStackTrace(); } } } }
4.代理线程ServerAgent,如:
package yybwb; /**LastDate:2014-10-22 * 更新菜单-#GET_MENULIST# * Author:Wuchyuan */ import static yybwb.ConstantUtil.DELETE_SUCCESS; import static yybwb.ConstantUtil.DIARY_SUCCESS; import static yybwb.ConstantUtil.REGISTER_FAIL; import static yybwb.ConstantUtil.UPDATE_STATE_SUCCESS; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.EOFException; import java.io.IOException; import java.net.Socket; import java.net.SocketException; import java.sql.Blob; import java.util.ArrayList; import java.util.List; public class ServerAgent extends Thread{ public Socket socket; public DataInputStream din; public DataOutputStream dout; boolean flag = false; public ServerAgent(Socket socket) { this.socket = socket; try { this.din = new DataInputStream(socket.getInputStream()); this.dout = new DataOutputStream(socket.getOutputStream()); flag =true; } catch (IOException e) { e.printStackTrace(); } } //方法:线程执行方法 public void run(){ while(flag){ try { String msg = din.readUTF(); //接收客户端发来的消息 // System.out.println("收到的消息是:"+msg); if(msg.startsWith("<#LOGIN#>")){ //消息为登录 String content = msg.substring(9); //获得消息内容 String [] sa = content.split("\\|"); ArrayList<String> result = DBUtil.checkLogin(sa[0], sa[1]); if(result.size()>1){ //登录成功 StringBuilder sb = new StringBuilder(); sb.append("<#LOGIN_SUCCESS#>"); for(String s:result){ sb.append(s); sb.append("|"); } String loginInfo = sb.substring(0,sb.length()-1); dout.writeUTF(loginInfo); //返回用户的基本信息 } else{ //登录失败 String loginInfo = "<#LOGIN_FAIL#>"+result.get(0); dout.writeUTF(loginInfo); } } //mealSvr发来的消息 2014-7-9 else if(msg.startsWith("<#PRINT_DINGD#>")){ //消息为查询打印订单2014-7-9 msg = msg.substring(15); //提取内容 String [] sa = msg.split("\\|"); //分割字符串 boolean result = DBUtil.QueryPrint(sa[0]); //查询是否有需打印订单 // if(result){ //有订单 dout.writeUTF("<#HAVE_DINGD#>"); //发回成功消息 dout.flush(); } else{ dout.writeUTF("<#NOT_DINGD#>"); //发回创建失败消息 dout.flush(); } } //2014-10-22 检查用户密码 else if(msg.startsWith("<#CHECK_USERPASSWD#>")){ // msg = msg.substring(20); //提取内容 String [] sa = msg.split("\\|"); //分割字符串 boolean result = DBUtil.CheckUserPw(sa[0],sa[1]); //查询是否有需打印订单 System.out.println("查询"+sa[0]+"密码是否正确"); if(result){ //有 dout.writeUTF("<#CHECKPWD_HAVE#>"); //发回成功消息 dout.flush(); } else{ dout.writeUTF("<#CHECKPWD_NOT#>"); //发回创建失败消息 dout.flush(); } } } } catch(SocketException se){ try { dout.close(); din.close(); socket.close(); socket = null; flag = false; } catch (IOException e) { e.printStackTrace(); } } catch(EOFException eof){ try { dout.close(); din.close(); socket.close(); socket = null; flag = false; } catch (IOException e) { e.printStackTrace(); } } catch (Exception e) { e.printStackTrace(); } } } }