1 package com.caipei.thread; 2 3 import java.io.BufferedReader; 4 import java.io.IOException; 5 import java.io.InputStreamReader; 6 import java.io.OutputStreamWriter; 7 import java.io.Writer; 8 import java.net.ServerSocket; 9 import java.net.Socket; 10 import java.sql.Connection; 11 import java.sql.DriverManager; 12 import java.sql.ResultSet; 13 import java.sql.SQLException; 14 import java.sql.Statement; 15 import java.text.SimpleDateFormat; 16 import java.util.Calendar; 17 import java.util.Random; 18 19 import javax.servlet.ServletContextEvent; 20 import javax.servlet.ServletContextListener; 21 22 23 24 25 public class MyListener implements ServletContextListener{ 26 27 private MyThread myThread; 71 72 @Override 73 public void contextInitialized(ServletContextEvent arg0) { 74 // TODO Auto-generated method stub 75 String str=null; 76 if (str==null&&myThread==null) { 77 myThread=new MyThread(); 78 myThread.start();//servlet 上下文初始化时启动socket 79 } 80 } 81 82 83 //自定义一个class 线程类继承自线程类 重写 84 class MyThread extends Thread 85 { 86 87 public static final int PORT = 4800;//监听的端口号 88 public void run() 89 { 90 while(!this.isInterrupted()) 91 { 92 try { 93 Thread.sleep(2000);//每隔2000ms执行一次 94 } catch (Exception e) { 95 // TODO: handle exception 96 e.printStackTrace(); 97 } 98 99 100 //定义一个ServerSocket监听在端口4800上 101 ServerSocket server = null; 102 try { 103 server = new ServerSocket(PORT); 104 } catch (IOException e) { 105 // TODO Auto-generated catch block 106 e.printStackTrace(); 107 } 108 while (true) { 109 //server尝试接收其他Socket的连接请求,server的accept方法是阻塞式的 110 Socket socket = null; 111 try { 112 socket = server.accept(); 113 } catch (IOException e) { 114 // TODO Auto-generated catch block 115 e.printStackTrace(); 116 } 117 //每接收到一个Socket就建立一个新的线程来处理它 118 119 new Thread(new Task(socket)).start(); 120 } 121 } 122 123 } 124 125 126 /** 127 * 用来处理Socket请求的 128 */ 129 class Task implements Runnable { 130 131 private Socket socket; 132 133 public Task(Socket socket) { 134 this.socket = socket; 135 } 136 137 public void run() { 138 try { 139 handleSocket(); 140 } catch (Exception e) { 141 e.printStackTrace(); 142 } 143 } 144 145 /** 146 * 跟客户端Socket进行通信 147 * @throws Exception 148 */ 149 public void handleSocket() throws Exception { 150 BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream())); 151 StringBuilder sb = new StringBuilder(); 152 String temp; 153 int index; 154 int flag=1; 155 while ((temp=br.readLine()) != null) { 156 System.out.println(temp); 157 if ((index = temp.indexOf("eof")) != -1) {//遇到eof时就结束接收 158 sb.append(temp.substring(0, index)); 159 break; 160 } 161 sb.append(temp); 162 }
加入jar包
在web.xml里加入配置文件
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> 3 <display-name>JavaWebThread</display-name> 4 <welcome-file-list> 5 <welcome-file>index.html</welcome-file> 6 <welcome-file>index.htm</welcome-file> 7 <welcome-file>index.jsp</welcome-file> 8 <welcome-file>default.html</welcome-file> 9 <welcome-file>default.htm</welcome-file> 10 <welcome-file>default.jsp</welcome-file> 11 </welcome-file-list> 12 13 <listener> 14 <listener-class> com.caipei.thread.MyListener</listener-class> 15 </listener> 16 17 </web-app>