手写Javaweb服务器
回忆socket
public class Client {
public static void main(String[] args) {
//1.创建socket对象
Socket client=null;
DataOutputStream dos = null;
DataInputStream dis =null;
try {
client = new Socket("localhost", 8888);
//2.获取输出流请求
dos = new DataOutputStream(client.getOutputStream());
dos.writeUTF("我是客服端:服务器你好!");
//3.获取输出流响应
dis = new DataInputStream(client.getInputStream());
System.out.println(dis.readUTF());
} catch (IOException e) {
e.printStackTrace();
} finally {
//4.关闭流
IOClose.closeAll(dis,dos,client);
}
}
创建IOClose类
public class IOClose {
//关闭全部的工具类
public static void closeAll(Closeable...c){
for (Closeable closeable : c) {
if (closeable!=null){
try {
closeable.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
创建服务端(在httpserver_1包下)
package com.feng.server;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
public static void main(String[] args) {
//1.创建ServerSocket对象
ServerSocket server = null;
Socket client =null;
DataInputStream dis =null;
DataOutputStream dos =null;
try {
server = new ServerSocket(8888);
//2.监听是否有客服端发送请求
client = server.accept();
//3.获取Socket对象
//4.获取输入流 ->得到客服端的请求
dis = new DataInputStream(client.getInputStream());
System.out.println(dis.readUTF());
//5.获取输出流 ->给客服端响应结果
dos = new DataOutputStream(client.getOutputStream());
dos.writeUTF("客服端你好,我是服务器,我接受到了你的信息");
} catch (IOException e) {
e.printStackTrace();
} finally {
IOClose.closeAll(dos,dis,client,server);
}
同样创建IOClose
在CS结构server(2)
package com.feng.server;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
public class Server2 {
public static void main(String[] args) {
//1.创建ServerSocket对象
ServerSocket server = null;
Socket client =null;
BufferedReader br = null;
try {
server = new ServerSocket(8888);
//2.监听是否有客服端发送请求
client = server.accept();
//获取来自浏览器的请求信息
br = new BufferedReader(new InputStreamReader(client.getInputStream(), "utf-8"));
String str=null;
while ((str=br.readLine()).length()>0) {
System.out.println(str);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
IOClose.closeAll(br,client,server);
}
}
}
创建HTML
创建server3
获取来自浏览器的请求信息
//获取来自浏览器的请求信息
is = client.getInputStream();
byte[] buf = new byte[20480];
int len = is.read(buf);
System.out.println(new String(buf,0,len));
对web浏览器做出响应
添加参数
String CRLF="\r\n";
String BLANK=" ";
InputStream is = null;
/**对web浏览器做出响应*/
StringBuilder sb = new StringBuilder();
StringBuilder sbContent = new StringBuilder(); //响应的文本
sbContent.append("<html><head><title>响应结果</title></head>");
sbContent.append("<body>登入成功</body></html>");
//1拼接响应头
sb.append("HTTP/1.1").append(BLANK).append(200).append(BLANK).append("OK");
sb.append(CRLF);
sb.append("Content-Type: text/html;charset=UTF-8");
sb.append(CRLF);
sb.append("Content-Length:").append(sbContent.toString().getBytes().length).append(CRLF);
sb.append(CRLF);//带表响应头与响应正文部分之间的换行
sb.append(sbContent);
//2通过输出流
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(client.getOutputStream(),"UTF-8"));
bw.write(sb.toString());
bw.flush();
bw.close();
整合server3
public class Server3 {
public static void main(String[] args) {
//1.创建ServerSocket对象
ServerSocket server = null;
Socket client =null;
String CRLF="\r\n";
String BLANK=" ";
InputStream is = null;
try {
server = new ServerSocket(8888);
//2.监听是否有客服端发送请求
client = server.accept();
//获取来自浏览器的请求信息
is = client.getInputStream();
byte[] buf = new byte[20480];
int len = is.read(buf);
System.out.println(new String(buf,0,len));
/**对web浏览器做出响应*/
StringBuilder sb = new StringBuilder();
StringBuilder sbContent = new StringBuilder(); //响应的文本
sbContent.append("<html><head><title>响应结果</title></head>");
sbContent.append("<body>登入成功</body></html>");
//1拼接响应头
sb.append("HTTP/1.1").append(BLANK).append(200).append(BLANK).append("OK");
sb.append(CRLF);
sb.append("Content-Type: text/html;charset=UTF-8");
sb.append(CRLF);
sb.append("Content-Length:").append(sbContent.toString().getBytes().length).append(CRLF);
sb.append(CRLF);//带表响应头与响应正文部分之间的换行
sb.append(sbContent);
//2通过输出流
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(client.getOutputStream(),"UTF-8"));
bw.write(sb.toString());
bw.flush();
bw.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
//6.关闭流
IOClose.closeAll(is,client,server);
}
}
}
手写服务器 整体架构,编写XML文件
创建Request,Response,Server,WebApp(在server包下)
创建Servlet,LoginServlet(在servlet包下)
创建IOCloseUtil(在util包下)
创建web.xml(在WEB_INFO包下)
创建Entity,Mapping,ServletContext,WebDom4j,创建IOClose在util包下
配置解析XML文件
补充实体类
package com.feng.server;
public class Entity { //servlet-name或每一个servlet-name所对应得实体类
private String name;
private String clazz;
public Entity() {
}
public Entity(String name, String clazz) {
this.name = name;
this.clazz = clazz;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getClazz() {
return clazz;
}
public void setClazz(String clazz) {
this.clazz = clazz;
}
}
补充Mapping类
package com.feng.server;
import java.util.ArrayList;
import java.util.List;
public class Mapping { //映射关系,多个路径可以访问资源
private String name; //servlet-name
private List<String> urlPattern;//url-pattern
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<String> getUrlPattern() {
return urlPattern;
}
public void setUrlPattern(List<String> urlPattern) {
this.urlPattern = urlPattern;
}
public Mapping() {
this.urlPattern = new ArrayList<String>();
}
public Mapping(String name, List<String> urlPattern) {
this.name = name;
this.urlPattern = urlPattern;
}
}
配置dom4j
package com.feng.server;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class WebDom4j { //用于解析xml
private List<Entity> entityList; //用于储存实体类,每一个实体类一个servlet-name对应一个servlet-class
private List<Mapping> mappingList;//用于储存映射类,每一个servlet-name对应一个url-pattern
public List<Entity> getEntityList() {
return entityList;
}
public void setEntityList(List<Entity> entityList) {
this.entityList = entityList;
}
public List<Mapping> getMappingList() {
return mappingList;
}
public void setMappingList(List<Mapping> mappingList) {
this.mappingList = mappingList;
}
//构造方法
public WebDom4j() {
entityList = new ArrayList<Entity>();
mappingList = new ArrayList<Mapping>();
}
//获取Document对象的方法
public Document getDocument(){
try {
//创建SAXReader对象
SAXReader reader = new SAXReader();
//调用read方法
return reader.read(new File("web/WEB-INF/web.xml"));
} catch (DocumentException e) {
e.printStackTrace();
}
return null;
}
//获取元素
public void parse(Document doc) {
//1获取根元素
Element root = doc.getRootElement(); //web-app
//2获取servlet子元素
for (Iterator<Element> ite = root.elementIterator("servlet"); ite.hasNext();) {
Element subElement = ite.next(); //得到每一个servlet
//创建一个实体类
Entity ent = new Entity(); //用于储存servlet-name与servlet-class