标签:结束 accept 开始 dateutil 主程 cto public eval buffere
日期工具类:
package com.zda.httpserver.util;
import java.text.SimpleDateFormat; import java.util.Date; /** * 日期工具类 * @author zda * @version 1.0 * @since 1.0 * */ public class DateUtil { private static SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS"); private DateUtil() { } /** * 获取系统当前时间 * @return String [yyyy-MM-dd HH:mm:ss SSS] */ public static String getCurrentTime() { return dateFormat.format(new Date()); } }
日志记录器:
package com.zda.httpserver.util;
import java.text.SimpleDateFormat; import java.util.Date; /** * 日志记录器 * @author zda * @version 1.0 * @since 1.0 * */ public class Logger { //工具类的方法往往是静态的,不需要创建对象 //工具类的构造方法往往也是私有的,但不是必须的 private Logger() { } /** * 普通日志记录器 * @param msg */ public static void log(String msg) { //输出到控制台 System.out.println("[INFO]" + DateUtil.getCurrentTime() + msg); } }
xml文档
<!-- server.xml--> <?xml version="1.0" encoding="UTF-8"?> <server> <service> <connector port="8080"></connector> </service> </server>
解析xml文档
package com.zda.httpserver.core; import org.dom4j.Document; import org.dom4j.Element; import org.dom4j.io.*; /** * 解析conf文件夹中的server.xml文件 * @author zda * @version 1.0 * @since 1.0 * */ public class ServerParser { /** * 获取服务器的端口号 * @return int port */ public static int getPort() { //设置服务器默认端口号:8080 int port = 8080; try { //创建解析器 SAXReader saxReader = new SAXReader(); //通过解析器的read方法将配置文件读取到内存中,生成一个Document[org.dom4j]对象树 Document document = saxReader.read("conf/server.xml"); //获取connector节点元素的路径:server -> service -> connector //获取connector节点元素的xpath路径:/server/service/connector //获取connector节点元素的xpath路径:server//connector //获取connector节点元素的xpath路径://connector Element connectorElt = (Element) document.selectSingleNode("//connector"); //获取port属性的值 port = Integer.parseInt(connectorElt.attributeValue("port")); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return port; } }
程序入口:
package com.zda.httpserver.core; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.ServerSocket; import java.net.Socket; import com.zda.httpserver.util.Logger; /** * httpserver主入口 * @author zda * @version 1.0 * @since 1.0 * */ public class BootStrap { public static void main(String[] args) { // 程序入口 start(); } /** * 主程序入口 */ public static void start() { ServerSocket serverSocket = null; Socket clientSocket = null; BufferedReader br = null; try { Logger.log("httpserver start"); //获取当前时间 long start = System.currentTimeMillis(); //获取系统端口号 int port = ServerParser.getPort(); Logger.log("httpserver-port: " + port); //创建服务器套接字,并且绑定端口号:8080 serverSocket = new ServerSocket(port); //获取结束时间 long end = System.currentTimeMillis(); Logger.log("httpserver started: " + (end-start) + " ms"); while(true){ //开始监听网络,此时程序处于等待状态,等待接收客户端的消息 clientSocket = serverSocket.accept(); //接收客户端消息 br = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); String temp = null; while((temp = br.readLine()) != null){ System.out.println(temp); } } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ //关闭资源 if(br != null){ try { br.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if(clientSocket != null){ try { clientSocket.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if(serverSocket != null){ try { serverSocket.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } }
浏览器访问:
http://localhost:8080/abc?username=123 &password=456
输出结果:
标签:结束 accept 开始 dateutil 主程 cto public eval buffere
原文地址:https://www.cnblogs.com/zhaideang/p/12299934.html