本文主要是介绍手写服务器httpserver_xml配置文件_sax解析基础应用JAVA205-206,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
来源:http://www.bjsxt.com/
一、S02E205_01手写服务器httpserver_xml配置文件_sax解析基础
XML
package com.test.xml;import java.io.IOException;
import java.util.List;import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;import org.xml.sax.SAXException;public class ParseDemo {public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException {//1、获取解析工厂SAXParserFactory factory = SAXParserFactory.newInstance();//2、从解析工厂获取解析器SAXParser parser = factory.newSAXParser();//3、加载文档Document注册处理器//4、编写处理器PersonHandler handler = new PersonHandler();parser.parse(Thread.currentThread().getContextClassLoader().getResourceAsStream("com/test/xml/person.xml"),handler);List<Person> persons = handler.getPersons();for (Person p : persons) {System.out.println(p.getName() + "-->>" + p.getAge());}}}
package com.test.xml;import java.util.ArrayList;
import java.util.List;import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
/*** 存储对象*/
public class PersonHandler extends DefaultHandler{private List<Person> persons;private Person person;private String tag;//记录标签名public List<Person> getPersons() {return persons;}public void setPersons(List<Person> persons) {this.persons = persons;}@Overridepublic void startDocument() throws SAXException {//System.out.println("处理文档开始");persons = new ArrayList<Person>();}@Overridepublic void startElement(String uri, String localName, String qName,Attributes attributes) throws SAXException {System.out.println("开始一个元素" + qName);if(null!=qName){tag = qName;}if(null!=qName && qName.equals("person")){person = new Person();}}@Overridepublic void characters(char[] ch, int start, int length)throws SAXException {//System.out.println(new String(ch,start,length));String str = new String(new String(ch,start,length));if(null!=tag && tag.equals("name")){person.setName(str);}else if(null!=tag && tag.equals("age")){Integer age = Integer.valueOf(str);person.setAge(age);}}@Overridepublic void endElement(String uri, String localName, String qName)throws SAXException {//System.out.println("结束一个元素" + qName);if(qName.equals("person")){this.persons.add(person);}tag = null;}@Overridepublic void endDocument() throws SAXException {System.out.println("处理文档结束");}
}
package com.test.xml;public class Person {private String name;private int age;public Person() {}public Person(String name, int age) {super();this.name = name;this.age = age;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}
}
控制台输出:
开始一个元素persons
开始一个元素person
开始一个元素name
开始一个元素age
开始一个元素person
开始一个元素name
开始一个元素age
处理文档结束
至尊宝-->>9000
白晶晶-->>7000
二、S02E206_01手写服务器httpserver_xml配置文件_sax解析应用
<html><head><title>第一个表单</title></head><body><form method="get" action="http://localhost:8888/g">用户名:<input type="text" name="uname" id="name">密码:<input type="password" name="pwd" id="pass"><input type="submit" value="登录"></form></body>
</html>
web.xml文件
<?xml version="1.0" encoding="UTF-8" ?>
<web_app><servlet><servlet-name>login</servlet-name><servlet-class>com.test.server2.LoginServlet</servlet-class></servlet><servlet-mapping><servlet-name>login</servlet-name><url-pattern>/login</url-pattern><url-pattern>/log</url-pattern><url-pattern>/g</url-pattern></servlet-mapping><servlet><servlet-name>reg</servlet-name><servlet-class>com.test.server2.RegisterServlet</servlet-class></servlet><servlet-mapping><servlet-name>reg</servlet-name><url-pattern>/reg</url-pattern></servlet-mapping>
</web_app>
源码:
package com.test.server2;import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
/*** 创建服务器,并启动* 1、请求* 2、响应*/
public class Server {private ServerSocket server;private boolean isShutDown = false;public static void main(String[] args) {Server server = new Server();server.start();}/*** 启动方法*/public void start(){ start(8888);}/*** 指定端口的启动方法*/public void start(int port){ try {server = new ServerSocket(port);this.receive();} catch (IOException e) {//e.printStackTrace();stop();}}/*** 接收客户端*/private void receive(){try {//请求及响应while(!isShutDown){new Thread(new Dispatcher(server.accept())).start();}} catch (IOException e) {//e.printStackTrace();stop();}}/*** 停止服务器*/public void stop(){isShutDown = true;try {if(!(null==server)){server.close();}} catch (IOException e) {//e.printStackTrace();}}
}
package com.test.server2;import java.io.IOException;
import java.net.Socket;
/*** 一个请求与响应,就一个对象*/
public class Dispatcher implements Runnable{private Socket client;private Request req;private Response rep;private int code = 200;Dispatcher(Socket client){this.client = client;try {req = new Request(client.getInputStream());rep = new Response(client.getOutputStream());} catch (IOException e) {//e.printStackTrace();code = 500;try {rep.pushToClient(code);} catch (IOException e1) {//e1.printStackTrace();}return;}}@Overridepublic void run() {try {Servlet serv = WebApp.getServlet(req.getUrl());if(null==serv){this.code = 404;//找不到处理}else{serv.service(req, rep);}rep.pushToClient(code);//推送到客户端} catch (Exception e) {//e.printStackTrace();this.code = 500;try {rep.pushToClient(code);} catch (IOException e1) {e1.printStackTrace();}}try {client.close();} catch (IOException e) {//e.printStackTrace();}}
}
package com.test.server2;import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;
/*** 封装request*/
public class Request {//请求方式private String method;//请求资源private String url;//请求参数private Map<String,List<String>> parameterMapValues;//内部public static final String CRLF = "\r\n";private InputStream is;private String requestInfo;public Request(){method = "";url = "";parameterMapValues = new HashMap<String,List<String>>();requestInfo = "";}public Request(InputStream is){this();this.is = is;try {byte[] data = new byte[204800];int len = is.read(data);requestInfo = new String(data,0,len);} catch (IOException e) {return;}//分析请求信息parseRequestInfo();}public String getUrl() {return url;}public void setUrl(String url) {this.url = url;}/*** 分析请求信息*/private void parseRequestInfo(){if((null==requestInfo) || (requestInfo=requestInfo.trim()).equals("")){return;}/*** ====================================* 从信息的首行分解出:请求方式 请求路径 请求参数(get可能存在)* 如:GET /index.html?uname=intputUname&pwd=inputPassword HTTP/1.1* * 如果为post方式,请求参数可能在最后正文中* ====================================*/String paramString = "";//接收请求参数int positionCRLF = 0;positionCRLF = requestInfo.indexOf(CRLF);if (positionCRLF == -1){return;}//1、获取请求方式String firstLine = requestInfo.substring(0,requestInfo.indexOf(CRLF));int idx = requestInfo.indexOf("/");// /的位置this.method = firstLine.substring(0,idx).trim();String urlStr = firstLine.substring(idx,firstLine.indexOf("HTTP/")).trim();if(this.method.equalsIgnoreCase("post")){//post方式this.url = urlStr;paramString = requestInfo.substring(requestInfo.lastIndexOf(CRLF)).trim();}else if(this.method.equalsIgnoreCase("get")){//get方式if(urlStr.contains("?")){String[] urlArray = urlStr.split("\\?");this.url = urlArray[0];paramString = urlArray[1];//接收请求参数}else{this.url = urlStr;}}//2、将请求参数封装到Map中parseParams(paramString);}/*** 将请求参数封装到Map中* @param paramString*/private void parseParams(String paramString){//分割,将字符串转成数组StringTokenizer token = new StringTokenizer(paramString,"&");while(token.hasMoreTokens()){String keyValue = token.nextToken();String[] keyValues = keyValue.split("=");if(keyValues.length == 1){keyValues = Arrays.copyOf(keyValues, 2);keyValues[1] = null;}String key = keyValues[0].trim();String value = null==keyValues[1]?null:decode(keyValues[1].trim(),"gbk");//分拣,转换成Mapif(!parameterMapValues.containsKey(key)){parameterMapValues.put(key, new ArrayList<String>());}List<String> values = parameterMapValues.get(key);values.add(value);}}/*** 解决中文* @param value* @param code* @return*/private String decode(String value,String code){try {return java.net.URLDecoder.decode(value, code);} catch (UnsupportedEncodingException e) {//e.printStackTrace();}return null;}/*** 根据页面的name获取对应的多个值*/public String[] getParameterValues(String name){List<String> values = null;if( (values=parameterMapValues.get(name))==null ){return null;}else{return values.toArray(new String[0]);}}/*** 根据页面的name获取对应的单个值*/public String getParameterValue(String name){String[] values = getParameterValues(name);if(null==values){return null;}return values[0];}
}
package com.test.server2;import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.util.Date;
/*** 封装响应信息*/
public class Response {//两个常量public static final String CRLF = "\r\n";public static final String BLANK = " ";//流private BufferedWriter bw;//正文private StringBuilder content;//存储头信息private StringBuilder headInfo;//存储正文长度private int len = 0;public Response(){headInfo = new StringBuilder();content = new StringBuilder();len = 0;}public Response(OutputStream os){this();bw = new BufferedWriter(new OutputStreamWriter(os));}public Response(Socket client){this();try {bw = new BufferedWriter(new OutputStreamWriter(client.getOutputStream()));} catch (IOException e) {headInfo = null;}}/*** 构建正文*/public Response print(String info){content.append(info);len += (info + CRLF).getBytes().length;return this;}/*** 构建正文+回车*/public Response println(String info){content.append(info).append(CRLF);len += (info + CRLF).getBytes().length;return this;}/*** 构建响应头*/private void createHeadInfo(int code){//1)HTTP协议版本、状态代码、描述headInfo.append("HTTP/1.1").append(BLANK).append(code).append(BLANK);switch(code){case 200:headInfo.append("OK");break;case 404:headInfo.append("NOT FOUND");break;case 500:headInfo.append("Server Error");break;}headInfo.append(CRLF);//2)响应头(Response Head)headInfo.append("Server:test Server/0.0.1").append(CRLF);headInfo.append("Date:").append(new Date()).append(CRLF);headInfo.append("Content-type:text/html;charset=GBK").append(CRLF);//正文长度:字节长度headInfo.append("Content-Length:").append(len).append(CRLF);headInfo.append(CRLF);}/*** 推送到客户端* @throws IOException */void pushToClient(int code) throws IOException{if(null==headInfo){code = 500;}createHeadInfo(code);//头信息+分割符bw.append(headInfo.toString());//正文bw.append(content.toString());bw.flush();bw.close();}
}
package com.test.server2;import java.io.IOException;
import java.util.List;
import java.util.Map;import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;public class WebApp {private static ServletContext contxt;static{try {//获取解析工厂SAXParserFactory factory = SAXParserFactory.newInstance();//获取解析器SAXParser sax = factory.newSAXParser();//指定xml + 处理器WebHandler web = new WebHandler();sax.parse(Thread.currentThread().getContextClassLoader().getResourceAsStream("com/test/server2/web.xml"),web);//将List转成Mapcontxt = new ServletContext();Map<String,String> servlet = contxt.getServlet();//servlet-name,servlet-classfor (Entity entity : web.getEntityList()) {servlet.put(entity.getName(), entity.getClz());}//url-pattern,servlet-nameMap<String,String> mapping = contxt.getMapping();for (Mapping mapp : web.getMappingList()) {List<String> urls = mapp.getUrlPattern();for (String url : urls) {mapping.put(url, mapp.getName());}}} catch (ParserConfigurationException e) {e.printStackTrace();} catch (org.xml.sax.SAXException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}}public static Servlet getServlet(String url) throws InstantiationException, IllegalAccessException, ClassNotFoundException{if(null==url || (url=url.trim()).equals("")){return null;}//return contxt.getServlet().get(contxt.getMapping().get(url));//根据字符串(完整路径)创建对象String name = contxt.getServlet().get(contxt.getMapping().get(url));return (Servlet)Class.forName(name).newInstance();//确保空构造存在}
}
package com.test.server2;
/*** 抽象为一个父类*/
public abstract class Servlet {public void service(Request req,Response rep) throws Exception{this.doGet(req,rep);this.doPost(req,rep);}public abstract void doGet(Request req,Response rep) throws Exception;public abstract void doPost(Request req,Response rep) throws Exception;
}
package com.test.server2;import java.util.HashMap;
import java.util.Map;
/*** 上下文*/
public class ServletContext {//为每一个servlet取别名// login-->com.test.server2.LoginServletprivate Map<String,String> servlet;// url-->login// /log-->login// /login-->loginprivate Map<String,String> mapping;public ServletContext() { servlet = new HashMap<String,String>();mapping = new HashMap<String,String>();}public Map<String, String> getServlet() {return servlet;}public void setServlet(Map<String, String> servlet) {this.servlet = servlet;}public Map<String, String> getMapping() {return mapping;}public void setMapping(Map<String, String> mapping) {this.mapping = mapping;}
}
package com.test.server2;public class LoginServlet extends Servlet{@Overridepublic void doGet(Request req,Response rep) throws Exception {String name = req.getParameterValue("uname");String pwd = req.getParameterValue("pwd");if(login(name, pwd)){rep.println("登录成功");}else{rep.println("登录失败");}}public boolean login(String name,String pwd){return name.equals("test") && pwd.equals("12346");}@Overridepublic void doPost(Request req,Response rep) throws Exception {}
}
package com.test.server2;public class RegisterServlet extends Servlet{@Overridepublic void doGet(Request req, Response rep) throws Exception {}@Overridepublic void doPost(Request req, Response rep) throws Exception {rep.println("<html><head><title>返回注册</title>");rep.println("</head><body>");rep.println("你的用户名为:" + req.getParameterValue("uname"));rep.println("</body></html>");}
}
这篇关于手写服务器httpserver_xml配置文件_sax解析基础应用JAVA205-206的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!