http请求头信息
1、封装请求和响应信息
封装请求对象:通过输入流,对HTTP协议进行解析,拿到了HTTP请求头的方法和URL。
package com.yan.tomcat.request;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
public class MyRequest {
private String url;
private String method;
public MyRequest(InputStream inputStream) throws IOException {
String httpRequest = "";
byte[] httpRequestBytes = new byte[1024];
int length = 0;
ByteArrayOutputStream bio = new ByteArrayOutputStream();
if ((length = inputStream.read(httpRequestBytes)) != -1) {
bio.write(httpRequestBytes,0,length);
}
httpRequest = bio.toString();
String httpHead = httpRequest.split("\n")[0];
url=httpHead.split("\\s")[1];
method=httpHead.split("\\s")[0];
System.out.println(this);
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getMethod() {
return method;
}
public void setMethod(String method) {
this.method = method;
}
}
封装响应对象:基于HTTP协议的格式进行输出写入。
package com.yan.tomcat.request;
import java.io.IOException;
import java.io.OutputStream;
public class MyResponse {
private OutputStream outputStream;
public MyResponse(OutputStream outputStream){
this.outputStream = outputStream;
}
/**
* 封装http响应协议的内容
* @param content 返回的数据
* @throws IOException
*/
public void write(String content)throws IOException {
StringBuffer httpResponse = new StringBuffer();
httpResponse.append("HTTP/1.1 200 OK\n")
.append("Content-Type: text/html\n")
.append("\r\n")
.append("<html><body>")
.append(content)
.append("</body></html>");
outputStream.write(httpResponse.toString().getBytes());
outputStream.close();
}
}
2、创建Servlet基础接口
package com.yan.tomcat.request;
/**
* Servlet请求处理基类
*
* @author Administrator
*
*/
public abstract class MyServlet {
public abstract void doGet(MyRequest myRequest, MyResponse myResponse);
public abstract void doPost(MyRequest myRequest, MyResponse myResponse);
public void service(MyRequest myRequest, MyResponse myResponse) {
if (myRequest.getMethod().equalsIgnoreCase("POST")) {
doPost(myRequest, myResponse);
} else if (myRequest.getMethod().equalsIgnoreCase("GET")) {
doGet(myRequest, myResponse);
}
}
}
3、创建Servlet所需对象
package com.yan.tomcat.request;
public class ServletMapping {
private String servletName;
private String url;
private String clazz;
public ServletMapping(String servletName, String url, String clazz) {
this.servletName = servletName;
this.url = url;
this.clazz = clazz;
}
public String getServletName() {
return servletName;
}
public void setServletName(String servletName) {
this.servletName = servletName;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getClazz() {
return clazz;
}
public void setClazz(String clazz) {
this.clazz = clazz;
}
}
4、读取servlet与方法对应路径
package com.yan.tomcat.request;
import java.util.ArrayList;
import java.util.List;
public class ServletMappingConfig {
public static List<ServletMapping> servletMappingList =new ArrayList<ServletMapping>();
//制定哪个URL交给哪个servlet来处理
static{
servletMappingList.add(new ServletMapping("hello","/excute","com.yan.tomcat.request.ExcuteMethodServlet"));
}
}
5、执行Servlet方法
package com.yan.tomcat.request;
import java.io.IOException;
public class ExcuteMethodServlet extends MyServlet {
@Override
public void doGet(MyRequest myRequest, MyResponse myResponse) {
System.out.println("执行了doget方法。。。。。。。。");
try {
myResponse.write("excute doget");
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void doPost(MyRequest myRequest, MyResponse myResponse) {
System.out.println("执行了dopost方法。。。。。。。。");
try {
myResponse.write("excute dopost");
} catch (IOException e) {
e.printStackTrace();
}
}
}
6、启动类
package com.yan.tomcat;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.HashMap;
import java.util.Map;
import com.yan.tomcat.request.MyRequest;
import com.yan.tomcat.request.MyResponse;
import com.yan.tomcat.request.MyServlet;
import com.yan.tomcat.request.ServletMapping;
import com.yan.tomcat.request.ServletMappingConfig;
public class BootstrapStart {
private int port = 8080;
private Map<String, String> urlServletMap = new HashMap<String, String>();
public BootstrapStart(int port){
this.port=port;
}
public void start() {
// 初始化URL与对应处理的servlet的关系
initServletMapping();
ServerSocket serverSocket = null;
try {
serverSocket = new ServerSocket(port);
System.out.println("MyTomcat is start...");
while (true) {
Socket socket = serverSocket.accept();
InputStream inputStream = socket.getInputStream();
OutputStream outputStream = socket.getOutputStream();
MyRequest myRequest = new MyRequest(inputStream);
MyResponse myResponse = new MyResponse(outputStream);
// 请求分发
dispatch(myRequest, myResponse);
socket.close();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (null != serverSocket) {
try {
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
private void initServletMapping() {
for (ServletMapping servletMapping : ServletMappingConfig.servletMappingList) {
urlServletMap.put(servletMapping.getUrl(), servletMapping.getClazz());
}
}
public void dispatch(MyRequest myRequest, MyResponse myResponse) {
String clazz = urlServletMap.get(myRequest.getUrl());
// 反射
try {
Class<MyServlet> myServletClass = (Class<MyServlet>) Class.forName(clazz);
MyServlet myServlet = myServletClass.newInstance();
myServlet.service(myRequest, myResponse);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
new BootstrapStart(8080).start();
}
}