login
imooc.study.server.LoginServlet
login
/login
/g
register
imooc.study.server.RegisterServlet
register
/register
/r
package imooc.study.server;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import java.util.List;
import static java.lang.Thread.currentThread;
public class XMLWeb {
public static void main(String[] args) throws Exception {
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser saxParser = factory.newSAXParser();
WebHandler handler = new WebHandler();
saxParser.parse(currentThread().getContextClassLoader().getResourceAsStream("web.xml"), handler);
List entityList = handler.getEntityList();
System.out.println(entityList);
List mappingList = handler.getMappingList();
System.out.println(mappingList);
WebContext webContext = new WebContext(entityList, mappingList);
String className = webContext.getClz("/xxx");
System.out.println(className);
//根据反射 实例化对象
Class> forName = Class.forName(className);
Servlet o = (Servlet) forName.newInstance();
o.service();
}
}
package imooc.study.server;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
class WebHandler extends DefaultHandler {
private Entity entity;
private Mapping mapping;
private List entityList;
private List mappingList;
private String tag;
private boolean isEntity;
private boolean isMapping;
public List getEntityList() {
return entityList;
}
public List getMappingList() {
return mappingList;
}
@Override
public void startDocument() throws SAXException {
entityList = new ArrayList<>();
mappingList = new ArrayList<>();
isEntity = false;
isMapping = false;
}
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
// System.out.println();
// System.out.println("start-->" + qName);
tag = qName;
if (qName.equals("servlet")) {
entity = new Entity();
isEntity = true;
} else if (qName.equals("servlet-mapping")) {
mapping = new Mapping();
isMapping = true;
}
}
@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
// System.out.println();
// System.out.println("end-->" + qName);
if (qName.equals("servlet")) {
entityList.add(entity);
isEntity = false;
} else if (qName.equals("servlet-mapping")) {
mappingList.add(mapping);
isMapping = false;
}
tag = null;
}
@Override
public void endDocument() throws SAXException {
// System.out.println(list);
// System.out.println("stop-----");
// System.out.println("============");
}
@Override
public void characters(char[] ch, int start, int length) throws SAXException {
String content = new String(ch, start, length);
// System.out.println("content--->" + content);
//
// if (Objects.equals(tag, "name")) {
// person.setName(content);
// } else if (Objects.equals(tag, "age")) {
// person.setAge(Integer.parseInt(content));
// }
if (isEntity) {
if (Objects.equals(tag, "servlet-name")) {
entity.setName(content);
} else if (Objects.equals(tag, "servlet-class")) {
entity.setClz(content);
}
} else if (isMapping) {
if (Objects.equals(tag, "servlet-name")) {
mapping.setName(content);
} else if (Objects.equals(tag, "url-pattern")) {
mapping.addPattern(content);
}
}
}
}
package imooc.study.server;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class WebContext {
//k -> servlet-name v -> servlet-class
private Map entityMap;
//k-> url-pattern v -> servlet-name
private Map mapingMap;
public WebContext() {
}
public WebContext(List entityList, List mapingList) {
entityMap = new HashMap<>();
mapingMap = new HashMap<>();
for (Entity entity : entityList) {
entityMap.put(entity.getName(), entity.getClz());
}
for (Mapping mapping : mapingList) {
for (String pattern : mapping.getPattern()) {
mapingMap.put(pattern, mapping.getName());
}
}
}
public String getClz(String pattern) {
return entityMap.get(mapingMap.get(pattern));
}
}
package imooc.study.server;
public interface Servlet {
void service();
}
package imooc.study.server;
public class RegisterServlet implements Servlet {
@Override
public void service() {
System.out.println("RegisterServlet");
}
}
package imooc.study.server;
public class LoginServlet implements Servlet {
@Override
public void service() {
System.out.println("LoginServlet");
}
}
package imooc.study.server;
import java.util.HashSet;
import java.util.Set;
public class Mapping {
private String name;
private Set pattern;
public Mapping() {
pattern = new HashSet<>();
}
public Mapping(String name) {
this.name = name;
}
public void addPattern(String parttern) {
pattern.add(parttern);
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public Set getPattern() {
return pattern;
}
@Override
public String toString() {
return "Mapping{" +
"name='" + name + '\'' +
", pattern=" + pattern +
'}';
}
}
package imooc.study.server;
public class Entity {
private String name;
private String clz;
public Entity() {
}
public Entity(String name, String clz) {
this.name = name;
this.clz = clz;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void setClz(String clz) {
this.clz = clz;
}
public String getClz() {
return clz;
}
@Override
public String toString() {
return "Entity{" +
"name='" + name + '\'' +
", clz='" + clz + '\'' +
'}';
}
}
package imooc.study.server.serverdemo;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Date;
public class Server {
private ServerSocket serverSocket;
public static void main(String[] args) {
Server server = new Server();
server.start();
}
public void start() {
try {
serverSocket = new ServerSocket(8000);
receive();
} catch (IOException e) {
e.printStackTrace();
System.out.println("server is error");
}
}
public void receive() {
try {
Socket accept = serverSocket.accept();
InputStream inputStream = accept.getInputStream();
// BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
// String data;
// while ((data = reader.readLine()) != null) {
// System.out.println(data);
// }
byte[] data = new byte[1024*1024];
int len = inputStream.read(data);
String header = new String(data, 0, len);
System.out.println(header);
OutputStream out = accept.getOutputStream();
// BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(outputStream));
// writer.write("HTTP/1.1 200 OK\r\n");
// writer.write("Content‐Type:text/html\r\n");
// writer.write(String.format("Date:%s\r\n", new Date()));
// writer.write("\r\n");
// writer.write("hello");
out.write("HTTP/1.1 200 OK\r\n".getBytes());
out.write("Content‐Type:text/html\r\n".getBytes());
out.write(String.format("Date:%s\r\n", new Date()).getBytes());
out.write("\r\n".getBytes());
FileInputStream fileInputStream = new FileInputStream("src/main/resources/login.html");
int read;
while ((read = fileInputStream.read()) != -1) {
out.write(read);
}
// out.write("hello".getBytes());
out.flush();
} catch (IOException e) {
e.printStackTrace();
System.out.println("client connect error");
}
}
public void stop() {
}
}