使用XML作为配置文件的方式完成模拟TOMCAT(XML,socket访问浏览器,DTD)
来源:互联网 发布:2018开淘宝店挣钱吗 编辑:程序博客网 时间:2024/06/06 04:06
import org.dom4j.Document;import org.dom4j.DocumentException;import org.dom4j.Element;import org.dom4j.io.SAXReader;import org.junit.Test;public class dom4j {@Testpublic void demo1() throws DocumentException{SAXReader sxr=new SAXReader();//创建解析器Document docu = sxr.read("src/xml/demo1.xml");//解析xml文档Element root = docu.getRootElement();//获得根节点System.out.println(root.getName());//打印根节点名称Element person=root.element("Person");//获得根节点第一个子节点Element person1 = (Element) root.elements("person").get(1);//获得根节点第二个子节点Element na = person1.element("name");System.out.println(na.getText());//打印子节点中的文本}}@Testpublic void demo2() throws DocumentException{SAXReader sxr=new SAXReader();//创建解析器Document docu = sxr.read("src/xml/demo1.xml");//解析xml文档//List<Element> list = docu.selectNodes("//name");//查询所有的name元素List<Element> list = docu.selectNodes("//person[@id]");//查询带有id属性的person元素for(Element i:list){System.out.println(i.attributeValue("id"));}}import java.io.IOException;import java.net.ServerSocket;import java.net.Socket;import org.dom4j.Document;import org.dom4j.DocumentException;import org.dom4j.Element;import org.dom4j.Node;import org.dom4j.io.SAXReader;public class Myserver {public static void main(String args[]) throws IOException, DocumentException{//解析XML//创建解析器SAXReader sr=new SAXReader();//解析XML文档Document docu = sr.read("src/Confi/server.xml");Element e = (Element) docu.selectSingleNode("//Connector['@port']");//获得带有port属性的Connector节点 String s = e.attributeValue("port");//获得属性值 int port=Integer.parseInt(s);ServerSocket ss=new ServerSocket(port);//创建服务器的socketwhile(true){Socket sock = ss.accept();//服务器监听 new Thread(new myservice(sock)).start();//新建一个线程,执行run方法}}}import java.io.BufferedReader;import java.io.FileInputStream;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.net.Socket;import org.omg.CORBA.portable.OutputStream;public class myservice implements Runnable {private Socket sock;public myservice(Socket sock) {super();this.sock = sock;}public void run() {// TODO Auto-generated method stubtry {InputStream is = sock.getInputStream();//从浏览器中读取输入流InputStreamReader isr=new InputStreamReader(is);BufferedReader br=new BufferedReader(isr);String s=br.readLine();String path = s.split(" ")[1];//读取输入流的第二个字节,包含html文件名System.out.println(path);FileInputStream fis=new FileInputStream("src"+path);//读取相应的html文件java.io.OutputStream os=sock.getOutputStream();//向浏览器发送相应信息os.write("HTTP/1.1 200 OK\r\n".getBytes());os.write("\r\n".getBytes());byte[] buffer=new byte[1024];//长度为1024的缓存bufferint len=0;while((len=fis.read(buffer))!=-1){//将fis中的字节缓冲到buffer中,每次缓冲1024个字节,相当于把文件按照1024进行分割,指导文件末尾os.write(buffer,0,len);System.out.println(len);}} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}
阅读全文
0 0
- 使用XML作为配置文件的方式完成模拟TOMCAT(XML,socket访问浏览器,DTD)
- web.xml配置文件的dtd(http://java.sun.com/dtd/web-app_2_3.dtd)
- XML(二)有效的XML: DTD(文档类型定义)介绍与三种使用方式
- 使用xml作为数据库的配置文件的路径读取问题
- Spring依赖注入(基于XML配置文件和Annotation的方式完成属性装配)
- XML为什么使用 DTD?
- 运用DTD完成DOM对XML的解析
- 创建dtd文件完成xml的自动提示功能
- tomcat的配置文件web.xml
- Tomcat的xml配置文件详解
- 为Eclipse添加XML文档所使用的DTD或XML Schema(xml编辑提示)
- 观看内部使用dtd的note.xml
- 观看内部使用dtd的note.xml
- [XML学习笔记]004DTD的使用
- XML的DTD
- Xml的DTD
- web.xml的dtd
- XML的DOCTYPE,DTD
- 爬虫知识点(Handler和opener以及开放代理和私密代理的使用)(利用urllib2库)
- SSH整合 DAO注入sessionfactory的方式汇总
- [cocos2dx]三种缓存类介绍
- 读《SEO 艺术》
- STM32 RTC时钟配置
- 使用XML作为配置文件的方式完成模拟TOMCAT(XML,socket访问浏览器,DTD)
- 各种分布式文件系统比较
- ionic2项目实现项目打包
- SQL Server 2017 SELECT…INTO 创建的新表指定到文件组
- linux中“弹出界面eth0:设备似乎不存在”的问题
- HttpClient请求和HttpURLConnect请求数据
- 最长回文子串
- AT学习报告一 软件的初步使用
- HttpURLConnection获取网络数据(实例:网路请求图片)