java socket 深入学习tomcat 自写动态服务器 tomcat
此动态服务器 没有内置对象的设置,只是阐述最基本原理,
服务器:
package cn.com.test10; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.ServerSocket; import java.net.Socket; import java.util.HashMap; public class t01 { public static void main(String[] args) { try { ServerSocket ss = new ServerSocket(8080); while (true) { Socket soc = ss.accept(); new Thread(new ServerOO(soc)).start(); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } class ServerOO implements Runnable { private Socket soc; private BufferedReader br; private PrintWriter pw; private Request req=new Request(); private Response res=new Response(); public ServerOO(Socket soc) { this.soc = soc; try { this.br = new BufferedReader(new InputStreamReader(soc .getInputStream())); this.pw = new PrintWriter(soc.getOutputStream(), true); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } @Override public void run() { // TODO Auto-generated method stub String one; try { one = br.readLine(); if (!(one.equals("GET /favicon.ico HTTP/1.1"))) { String str = getSS(getSS(one, " ")[1], "\\?")[1]; String[] element= getSS(str,"&"); System.out.println(element[0]); for(String s:element){ String[] oo=getSS(s,"="); req.setAttribute(oo[0], oo[1]); } res.setPw(pw); Serverlet s= new Serverlet(); s.doGet(req, res); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally{ try { br.close(); pw.close(); soc.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } private String[] getSS(String str, String s) { return str.split(s); } } class Request { private HashMap<String, String> map = new HashMap<String, String>(); public void setAttribute(String str, String s) { map.put(str, s); } public String getAttribute(String str) { return map.get(str); } } class Response { private PrintWriter pw; public PrintWriter getPw() { return pw; } public void setPw(PrintWriter pw) { this.pw = pw; } }
serverlet:
package cn.com.test10; import java.io.PrintWriter; public class Serverlet { public void doGet(Request req,Response res){ Per p = new Per(); p.setName(req.getAttribute("name")); p.setAge(Integer.parseInt(req.getAttribute("age"))); System.out.println(p); PrintWriter pw = res.getPw(); pw.println("<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.0 Transitional//EN'><HTML><HEAD><TITLE> New Document </TITLE></HEAD><BODY>"+ p.toString() +"</BODY></HTML>"); } }
Per:
package cn.com.test10; public class Per { private String name; private int age; public String getName() { return name; } public void setName(String name) { System.out.println("===============setname======"); this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String toString(){ return "我叫:"+name+" 我的年龄是:"+age; } }