1.创建自己的MyServletContextListener.java:
1 package yybwb; 2 3 import java.net.ServerSocket; 4 5 import javax.servlet.ServletContextEvent; 6 7 import javax.servlet.ServletContextListener; 8 9 public class MyServletContextListener implements ServletContextListener 10 11 { //这里使该类实现 ServletContextListener监听接口,在Tomcat启动时会调用实现该接口 12 13 //的 contextInitialized方法 14 15 ServerSocket ss = null;//声明ServerSocket对象 16 17 ServerThread st = null;//声明ServerThread对象 18 19 20 21 //创建服务线程,并将其启动,本方法在tomca启动时调用 22 23 public void contextInitialized(ServletContextEvent sce){ 24 25 try{ ss = new ServerSocket(8888); 26 27 28 29 st = new ServerThread(ss); 30 31 st.start(); 32 33 }catch(Exception e){ e.printStackTrace(); 34 35 } } 36 37 38 39 //停止服务线程,本方法在tomca停止时调用 40 41 public void contextDestroyed(ServletContextEvent sce){ 42 43 44 45 try{ st.flag = false; 46 47 ss.close(); 48 49 ss = null; 50 51 st = null; }catch(Exception e){ e.printStackTrace(); 52 53 } } }
2.在Web.xml中配置上面的监听类
1 <Listener> 2 3 <Listener-class>XX.MyServletContextListener</Listener-class> 4 5 </Listener>
3.创建服务线程ServerThread类如:
1 package yybwb; 2 3 import java.net.ServerSocket; 4 5 import java.net.Socket; 6 7 import java.net.SocketException; 8 9 public class ServerThread extends Thread{ 10 11 public ServerSocket ss; //声明ServerSocket对象 12 13 public boolean flag = false; //循环标志位 14 15 public ServerThread(ServerSocket ss){ //构造器 16 17 this.ss = ss; 18 19 flag = true; } 20 21 //run方法创建ServerSocket 监听 22 23 public void run(){ 24 25 while(flag){ 26 27 try{ 28 29 Socket socket = ss.accept(); 30 31 //客户连接后,创建代理线程 ServerAgent,负责与该客户的通讯 32 33 ServerAgent sa = new ServerAgent(socket); 34 35 sa.start(); 36 37 } 38 39 catch(SocketException se){ 40 41 try{ ss.close(); 42 43 ss = null; 44 45 System.out.println("ServerSocket closed"); 46 47 }catch(Exception ee){ 48 49 ee.printStackTrace(); 50 51 } 52 53 } 54 55 catch(Exception e){ 56 57 e.printStackTrace(); 58 59 } 60 61 } 62 63 } 64 65 }
4.代理线程ServerAgent,如:
1 package yybwb; 2 3 /**LastDate:2014-10-22 * 更新菜单-#GET_MENULIST# * Author:Wuchyuan */ 4 5 import static yybwb.ConstantUtil.DELETE_SUCCESS; 6 7 import static yybwb.ConstantUtil.DIARY_SUCCESS; 8 9 import static yybwb.ConstantUtil.REGISTER_FAIL; 10 11 import static yybwb.ConstantUtil.UPDATE_STATE_SUCCESS; 12 13 import java.io.DataInputStream; 14 15 import java.io.DataOutputStream; 16 17 import java.io.EOFException; 18 19 import java.io.IOException; 20 21 import java.net.Socket; 22 23 import java.net.SocketException; 24 25 import java.sql.Blob; 26 27 import java.util.ArrayList; 28 29 import java.util.List; 30 31 public class ServerAgent extends Thread{ 32 33 public Socket socket; 34 35 public DataInputStream din; 36 37 public DataOutputStream dout; 38 39 boolean flag = false; 40 41 public ServerAgent(Socket socket) 42 43 { this.socket = socket; 44 45 try { this.din = new DataInputStream(socket.getInputStream()); this.dout = new DataOutputStream(socket.getOutputStream()); 46 47 flag =true; 48 49 } catch (IOException e) { e.printStackTrace(); } } //方法:线程执行方法 50 51 public void run(){ 52 53 while(flag){ 54 55 try { String msg = din.readUTF(); //接收客户端发来的消息 56 57 // System.out.println("收到的消息是:"+msg); 58 59 if(msg.startsWith("<#LOGIN#>")){ //消息为登录 60 61 String content = msg.substring(9); //获得消息内容 62 63 String [] sa = content.split("\\|"); 64 65 ArrayList<String> result = DBUtil.checkLogin(sa[0], sa[1]); 66 67 if(result.size()>1){ //登录成功 68 69 StringBuilder sb = new StringBuilder(); 70 71 sb.append("<#LOGIN_SUCCESS#>"); 72 73 for(String s:result){ 74 75 sb.append(s); 76 77 sb.append("|"); } 78 79 String loginInfo = sb.substring(0,sb.length()-1); 80 81 dout.writeUTF(loginInfo); //返回用户的基本信息 } 82 83 else{ //登录失败 84 85 String loginInfo = "<#LOGIN_FAIL#>"+result.get(0); 86 87 dout.writeUTF(loginInfo); } } 88 89 //mealSvr发来的消息 2014-7-9 90 91 else if(msg.startsWith("<#PRINT_DINGD#>")){ 92 93 //消息为查询打印订单2014-7-9 94 95 msg = msg.substring(15); //提取内容 96 97 String [] sa = msg.split("\\|"); //分割字符串 98 99 boolean result = DBUtil.QueryPrint(sa[0]); //查询是否有需打印订单 100 101 // if(result){ //有订单 102 103 dout.writeUTF("<#HAVE_DINGD#>"); //发回成功消息 104 105 dout.flush(); 106 107 } else{ 108 109 dout.writeUTF("<#NOT_DINGD#>"); //发回创建失败消息 110 111 dout.flush(); } } 112 113 //2014-10-22 检查用户密码 114 115 116 117 else if(msg.startsWith("<#CHECK_USERPASSWD#>")){ 118 119 // msg = msg.substring(20); //提取内容 120 121 String [] sa = msg.split("\\|"); //分割字符串 122 123 boolean result = DBUtil.CheckUserPw(sa[0],sa[1]); //查询是否有需打印订单 124 125 System.out.println("查询"+sa[0]+"密码是否正确"); 126 127 if(result){ //有 128 129 dout.writeUTF("<#CHECKPWD_HAVE#>"); //发回成功消息 130 131 dout.flush(); } 132 133 else{ 134 135 dout.writeUTF("<#CHECKPWD_NOT#>"); //发回创建失败消息 136 137 dout.flush(); 138 139 } 140 141 } 142 143 144 145 } 146 147 } catch(SocketException se){ try { 148 149 dout.close(); 150 151 din.close(); 152 153 socket.close(); 154 155 socket = null; 156 157 flag = false; 158 159 } catch (IOException e) { 160 161 e.printStackTrace(); } 162 163 } catch(EOFException eof){ 164 165 try { dout.close(); 166 167 din.close(); 168 169 socket.close(); 170 171 socket = null; 172 173 flag = false; 174 175 } catch (IOException e) { 176 177 e.printStackTrace(); } 178 179 } catch (Exception e) { 180 181 e.printStackTrace(); 182 183 } 184 185 } 186 187 } 188 189 }