java-模拟tomcat服务器
2013-09-17 11:30
218 查看
模拟tomcat服务器端代码示例:
客户端http请求模拟示例:
package cd.itcast.day5; import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.net.ServerSocket; import java.net.Socket; import java.util.Date; public class HttpServerDemo { public static void main(String[] args) { (new HttpServer()).run(); } public static class HttpServer { public void run() { String filePath = "D:\\Java\\apache-tomcat-6.0.35\\webapps\\ROOT"; ServerSocket serverSocket = null; try { serverSocket = new ServerSocket(8888); // 开启线程,接收客户端请求. while (true) { Socket socket = serverSocket.accept(); Thread thread = new Thread(new MyHttpServerThread(socket, filePath)); thread.start(); } } catch (IOException e2) { e2.printStackTrace(); } } } public static class MyHttpServerThread implements Runnable { String filePath = null; private Socket socket = null; private File file = null; public MyHttpServerThread(Socket socket, String filePath) { this.filePath = filePath; this.socket = socket; } @Override public void run() { // 读取客户端数据 BufferedReader request = null; // 处理客户端数据并返回. BufferedOutputStream response = null; try { // System.out.println(socket.getInetAddress().getHostAddress() // + " 连接到服务器."); InputStream inputStream = socket.getInputStream(); OutputStream outputStream = socket.getOutputStream(); request = new BufferedReader(new InputStreamReader(inputStream)); response = new BufferedOutputStream(outputStream); // 读取Http头信息行:GET /index.html HTTP/1.1 String httpHead = request.readLine(); if (httpHead != null) { String[] arr = httpHead.split(" "); if (arr.length == 3 && ("GET".equals(arr[0]) || "POST".equals(arr[0])) && "HTTP/1.1".equals(arr[2])) { // 获取要访问的文件. if ("/".equals(arr[2])) { file = new File(filePath, "/index.html"); } else { file = new File(filePath, arr[1]); } if (file.exists() && file.isFile()) { responseRequest(response); } else { System.out.println("请求的页面:" + arr[1] + " 不存在!"); } } else { System.out.println("客户端请求非法:" + arr[0]); } } else { System.out.println("未发现客户端请求或请求有误!"); } } catch (Exception e) { e.printStackTrace(); } finally { if (request != null) { try { request.close(); } catch (IOException e1) { e1.printStackTrace(); } } if (response != null) { try { response.close(); } catch (IOException e) { e.printStackTrace(); } } if (socket != null) { try { socket.close(); } catch (IOException e) { e.printStackTrace(); } } } } private void responseRequest(BufferedOutputStream response) throws IOException, FileNotFoundException { // 返回给客户端服务器的Http头信息 response.write("HTTP/1.1 200 ok\r\n".getBytes()); response.write("Server: HttpTest Server/1.1\r\n".getBytes()); response.write("Content-Type: text/html\r\n".getBytes()); response.write(("Content-Length: 5866" + file.length() + "\r\n") .getBytes()); response.write(("Date: " + new Date() + "\r\n").getBytes()); // 与实体部分以空行隔开. response.write("\r\n".getBytes()); FileInputStream fis = new FileInputStream(file); byte[] buff = new byte[1024]; int len = 0; while ((len = fis.read(buff)) != -1) { response.write(buff, 0, len); response.flush(); } fis.close(); } } }
客户端http请求模拟示例:
package cd.itcast.day5; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.net.Socket; import java.net.UnknownHostException; public class HttpClientDemo { public static void main(String[] args) { BufferedWriter out = null; BufferedReader in = null; try { Socket socket = new Socket("127.0.0.1", 8888); out = new BufferedWriter(new OutputStreamWriter( socket.getOutputStream())); in = new BufferedReader(new InputStreamReader( socket.getInputStream())); //发送Http头信息到服务器.请求页面为httptest.html. out.write("GET /httptest.html HTTP/1.1\r\n"); out.write("Host: localhost\r\n"); out.newLine(); out.flush(); //获取服务器返回内容. String line = null; while ((line = in.readLine()) != null) { System.out.println(line); } } catch (UnknownHostException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (in != null) { try { in.close(); } catch (IOException e) { e.printStackTrace(); } } if (out != null) { try { out.close(); } catch (IOException e) { e.printStackTrace(); } } } } }
相关文章推荐
- JAVA 模拟TomCat服务器
- 黑马程序员 Java练习-自定义图形化界面模拟浏览器访问Tomcat服务器
- Java网络编程(模拟浏览器访问Tomcat服务器)
- Linux搭建java web服务器环境(jdk7+tomcat7+mysql5.5 基于CentOS 6.5)
- Eclipse Java EE IDE配置Tomcat服务器
- 关于Windows Server 服务器 安装tomcat部署Java Web 项目的问题
- 【web服务器】——tomcat部署Java Web 项目的三种方式
- [服务器] JAVA_OPTS 参数含义说明 | tomcat
- Unity3D与JSP TomCat服务器传递数据和文件( 一 ) 建立Java服务器
- 基于Tomcat7、Java、WebSocket的服务器推送聊天室项目
- Java通过ssh连接到Linxu和Windos服务器远程启动Tomcat
- JavaWeb-Tomcat服务器
- Linux下java web服务器搭建(JDK1.6+Tomcat6)
- Tomcat服务器的配置(java web、jsp)
- java servlet----模拟一个简单的tomcat服务器
- linux服务器的搭建(mysql+java+tomcat)
- 【Java Web】: 使用 Telnet 远程连接服务器端与 Tomcat 服务器的搭建
- Java-->Tomcat(免费的Java Web服务器)
- Mac下配置Java开发环境(JDK1.8)和Tomcat服务器