1,浏览器和后端服务如何实现通讯,首先传输的数据要遵循http协议,经过tcp也就是咱们常说的套接字编程来实现,具体的底层数据传输确定就是咱们的输入输出流来实现了。html
2,咱们再来看后台服务器的实现逻辑,首先咱们要定义两个对象,一个request,一个response分别用来存放请求的参数以及返回的参数。java
3,建立一个ServerSocket,等待浏览器的链接和数据传输,当经过输入流读取到数据,放入到request对象。web
4,浏览器的请求类型通常分为get和post两种,因此咱们须要一个mpper映射类来根据浏览器的访问路径来分发具体处理的类或者方法。编程
5,处理以后获取处理的结果,并放到response对象,经过输出流返回给浏览器。后端
1,reques类浏览器
package com.liuyi; import java.io.InputStream; public class MyRequest { //请求方法 GET/POST private String requestMethod; //请求地址 private String requestUrl; public MyRequest(InputStream inputStream) throws Exception{ //缓冲区域 byte[] buffer = new byte[1024]; //读取数据的长度 int len = 0; //定义请求的变量 String str = null; if((len = inputStream.read(buffer))>0){ str = new String(buffer,0,len); } // GET / HTTP/1.1 String data = str.split("\n")[0]; String[] params = data.split(" "); this.requestMethod = params[0]; this.requestUrl = params[1]; } public String getRequestMethod() { return requestMethod; } public void setRequestMethod(String requestMethod) { this.requestMethod = requestMethod; } public String getRequestUrl() { return requestUrl; } public void setRequestUrl(String requestUrl) { this.requestUrl = requestUrl; } }
2,response类tomcat
package com.liuyi; import java.io.OutputStream; public class MyResponse { private OutputStream outputStream; public MyResponse(OutputStream outputStream) { this.outputStream = outputStream; } public void write(String str) throws Exception{ StringBuilder builder = new StringBuilder(); builder.append("HTTP/1.1 200 OK\n") .append("Content-Type:text/html\n") .append("\r\n") .append("<html>") .append("<body>") .append("<h1>"+str+"</h1>") .append("</body>") .append("</html>"); this.outputStream.write(builder.toString().getBytes()); this.outputStream.flush(); this.outputStream.close(); } }
3,Mapping类,定义请求路径和请求处理类的关系服务器
package com.liuyi; import java.util.HashMap; public class MyMapping { public static HashMap<String,String> mapping = new HashMap<String,String>(); static {
//这里请改为本身servlet实现类的彻底限定名 mapping.put("/mytomcat","com.liuyi.MyServlet"); } public HashMap<String,String> getMapping(){ return mapping; } }
4,定义HpptServlet抽象类,实现get和post的分发(根据不一样的请求方式调用不一样的方法进行处理)app
package com.mashibing; public abstract class MyHttpServlet { //定义常量 public static final String METHOD_GET = "GET"; public static final String METHOD_POST = "POST"; public abstract void doGet(MyRequest request,MyResponse response) throws Exception; public abstract void doPost(MyRequest request,MyResponse response) throws Exception; /** * 根据请求方式来判断调用哪一种处理方法 * @param request * @param response */ public void service(MyRequest request,MyResponse response) throws Exception{ if(METHOD_GET.equals(request.getRequestMethod())){ doGet(request,response); }else if(METHOD_POST.equals(request.getRequestMethod())){ doPost(request,response); } } }
5,定义本身的servletsocket
package com.liuyi; public class MyServlet extends MyHttpServlet{ @Override public void doGet(MyRequest request, MyResponse response) throws Exception { response.write("mytomcat get"); } @Override public void doPost(MyRequest request, MyResponse response) throws Exception { response.write("post tomcat"); } }
6,定义服务端的接受程序,接受socket请求
package com.liuyi; import java.io.InputStream; import java.io.OutputStream; import java.net.ServerSocket; import java.net.Socket; public class MyServer { /** * 定义服务端的接受程序,接受socket请求 * @param port */ public static void startServer(int port) throws Exception{ //定义服务端套接字 ServerSocket serverSocket = new ServerSocket(port); //定义客户端套接字 Socket socket = null; while (true){ socket = serverSocket.accept(); //获取输入流和输出流 InputStream inputStream = socket.getInputStream(); OutputStream outputStream = socket.getOutputStream(); //定义请求对象 MyRequest request = new MyRequest(inputStream); //定义响应对象 MyResponse response = new MyResponse(outputStream); String clazz = new MyMapping().getMapping().get(request.getRequestUrl()); if(clazz!=null){ Class<MyServlet> myServletClass = (Class<MyServlet>)Class.forName(clazz); //根据myServletClass建立对象 MyServlet myServlet = myServletClass.newInstance(); myServlet.service(request,response); } } } //运行启动 public static void main(String[] args) { try { startServer(10086); } catch (Exception e) { e.printStackTrace(); } } }
本案例只是实现了简单的tomcat和servlet,仅仅只是为了让咱们学习servlet和tomcat没有那么多的疑惑,学起来会更容易。实际工做中固然也不会这样去写,
可是若是你认值把这个代码敲一遍,并理解为何这样写,那么对你学习java web确定会有很大的帮助。