首页>代码>通往架构师基础必备/详细剖析java web客户端和服务端交互时程序中HTTP协议的传输过程>/WebServer_v16/src/main/java/com/webserver/http/HttpContext.java
package com.webserver.http; import java.io.File; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.Element; import org.dom4j.io.SAXReader; /** * HTTP协议规定的相关内容 * @author soft01 * */ public class HttpContext { /** * 状态代码与对应的描述 * key:状态代码 * value:状态描述 */ private static final Map<Integer,String> STATUS_MAPPING = new HashMap<Integer,String>(); /** * 介质类型映射时 * key:文件的后缀名 * value:Conent-Type对应的值 */ private static final Map<String,String> MIME_TYPE_MAPPING = new HashMap<String,String>(); /* * URL 与 Servlet 之间的映射 * key: URL * value: Servlet 的类名 */ private static final Map<String,String> URL_MAPPING = new HashMap<>(); static { //初始化 initStatusMapping(); initMimeTypeMapping(); initUrlMapping(); } /** * 初始化URL 与 Servlet 之间的对应关系 */ private static void initUrlMapping() { SAXReader reader = new SAXReader(); try { Document doc = reader.read(new File("conf/web.xml")); Element root = doc.getRootElement(); //算法 List<Element> mappings = root.elements("servlet-mapping"); List<Element> servlets = root.elements("servlet"); System.out.println("mappings:"+mappings); System.out.println("servlets:"+servlets); for(Element mapping : mappings) { String url = mapping.elementTextTrim("url-pattern"); String name = mapping.elementTextTrim("servlet-name"); System.out.println(url+":"+name); //根据name 找到对应的className for(Element servlet : servlets) { String name_ = servlet.elementTextTrim("servlet-name"); String className = servlet.elementTextTrim("servlet-class"); System.out.println(name_+":"+className); if(name.equals(name_)) { System.out.println("找到:"+url+":"+className); URL_MAPPING.put(url, className); } } } System.out.println("URL_MAPPING:"+URL_MAPPING); } catch (Exception e) { e.printStackTrace(); throw new RuntimeException(); } } /** * 初始化介质类型 */ private static void initMimeTypeMapping() { // /* // * xxxx.html // * xxxx.js // * xxxx.png // * xxxx.css // * jpg // * gif // */ // MIME_TYPE_MAPPING.put("html", "text/html"); // MIME_TYPE_MAPPING.put("css", "text/css"); // MIME_TYPE_MAPPING.put("png", "image/png"); // MIME_TYPE_MAPPING.put("gif", "image/gif"); // MIME_TYPE_MAPPING.put("jpg", "image/jpg"); // MIME_TYPE_MAPPING.put("js", "application/javascript"); try { //1 SAXReader reader = new SAXReader(); //2文档 Document doc = reader.read(new File("conf/web.xml")); //3获取根元素 Element root = doc.getRootElement(); //获取根元素名字 String ename = root.getName(); System.out.println("根元素:"+ename); //获取根标签下的所有<mime-mapping>标签 List<Element> list = root.elements("mime-mapping"); System.out.println(list.size()); for(Element empEle : list) { String name = empEle.elementText("extension"); String value = empEle.elementText("mime-type"); // System.out.println(name); // System.out.println(value); MIME_TYPE_MAPPING.put(name, value); } } catch (Exception e) { e.printStackTrace(); } } /** * 初始化状态代码和状态描述 */ private static void initStatusMapping() { /* * Status-Code = "200" ; OK | "201" ; Created | "202" ; Accepted | "204" ; No Content | "301" ; Moved Permanently | "302" ; Moved Temporarily | "304" ; Not Modified | "400" ; Bad Request | "401" ; Unauthorized | "403" ; Forbidden | "404" ; Not Found | "500" ; Internal Server Error | "501" ; Not Implemented | "502" ; Bad Gateway | "503" ; Service Unavailable | extension-code */ STATUS_MAPPING.put(200, "OK"); STATUS_MAPPING.put(201, "Created"); STATUS_MAPPING.put(202, "Accepted"); STATUS_MAPPING.put(204, "No Content"); STATUS_MAPPING.put(301, "Moved Permanently"); STATUS_MAPPING.put(302, "Moved Temporarily"); STATUS_MAPPING.put(304, "Not Modified"); STATUS_MAPPING.put(400, "Bad Request"); STATUS_MAPPING.put(401, "Unauthorized"); STATUS_MAPPING.put(403, "Forbidden"); STATUS_MAPPING.put(404, "Not Found"); STATUS_MAPPING.put(500, "Internal Server Error"); STATUS_MAPPING.put(501, "Not Implemented"); STATUS_MAPPING.put(502, "Bad Gateway"); STATUS_MAPPING.put(503, "Service Unavailable"); } /** * 根据给定的状态代码获取对应的状态描述 * @return */ public static String getStatusReason(int code) { return STATUS_MAPPING.get(code); } /** * 根据资源后缀名获取对应的Conent-Type值 * @param ext * @return */ public static String getMimeType(String ext) { return MIME_TYPE_MAPPING.get(ext); } /** * 根据URL 映射到对应的Servlet Class Name * 用户请求URL * @param url * @return */ public static String getServletClass(String url) { return URL_MAPPING.get(url); } public static void main(String[] args) { String line = getStatusReason(404); System.out.println(line); String fileName = "jquery-1.8.8.min.js"; String[] str = fileName.split("\\."); String str1 = str[str.length-1]; // int index = fileName.lastIndexOf(".")+1; // String str1 = fileName.substring(index); System.out.println(str1); String line2 = getMimeType(str1); System.out.println(line2); } }
最近下载更多
yhtech2015 LV8
2019年8月26日
zuidaima005 LV10
2019年6月6日
可是不知道么 LV23
2019年5月17日
qq1453363097 LV13
2019年4月24日
浪子在路上 LV12
2019年4月22日
zuidaima21533 LV1
2019年4月19日
sunliwei LV8
2019年4月19日
小牧34556 LV8
2019年4月17日
最代码官方 LV167
2019年4月12日
最近浏览更多
wan4444 LV1
2023年6月29日
qq232323
2023年5月29日
暂无贡献等级
哼哈二将哈哈哈哈啊哈
2022年12月20日
暂无贡献等级
Murmure LV2
2022年12月9日
crosa_Don LV18
2022年6月7日
gqb0123 LV1
2022年4月8日
a5366869 LV7
2021年10月31日
是pangpang呀 LV6
2021年7月10日
ericxu1116 LV24
2021年6月26日
pxqtsht LV15
2021年6月18日