步骤 1 : 拓扑图点亮 步骤 2 : DefaultServlet 步骤 3 : Constant 步骤 4 : Response 步骤 5 : InvokerServlet 步骤 6 : DefaultServlet 步骤 7 : HttpProcessor 步骤 8 : TestTomcat 步骤 9 : 比较可运行项目,快速定位问题
增值内容,请先登录
自己写一个Tomcat, 几乎使用到了除开框架外的所有Java 技术,如多线程,Socket, J2EE, 反射,Log4j, JSoup, JUnit, Html 等一整套技术栈, 从无到有,循序渐进涵盖全部74个知识点,549个开发步骤, 为竞争高薪资职位加上一个有吸引力的砝码.
增值内容,点击购买
使用爬虫已经被系统记录,请勿使用爬虫,增大封号风险。 如果是误封 ,请联系站长,谢谢
增值内容,请先登录
自己写一个Tomcat, 几乎使用到了除开框架外的所有Java 技术,如多线程,Socket, J2EE, 反射,Log4j, JSoup, JUnit, Html 等一整套技术栈, 从无到有,循序渐进涵盖全部74个知识点,549个开发步骤, 为竞争高薪资职位加上一个有吸引力的砝码.
增值内容,点击购买
使用爬虫已经被系统记录,请勿使用爬虫,增大封号风险。 如果是误封 ,请联系站长,谢谢
增值内容,请先登录
自己写一个Tomcat, 几乎使用到了除开框架外的所有Java 技术,如多线程,Socket, J2EE, 反射,Log4j, JSoup, JUnit, Html 等一整套技术栈, 从无到有,循序渐进涵盖全部74个知识点,549个开发步骤, 为竞争高薪资职位加上一个有吸引力的砝码.
增值内容,点击购买
使用爬虫已经被系统记录,请勿使用爬虫,增大封号风险。 如果是误封 ,请联系站长,谢谢
package cn.how2j.diytomcat.util;
import cn.hutool.system.SystemUtil;
import java.io.File;
public class Constant {
public static final int CODE_200 = 200;
public static final int CODE_302 = 302;
public static final int CODE_404 = 404;
public static final int CODE_500 = 500;
public final static String response_head_202 =
"HTTP/1.1 200 OK\r\n" +
"Content-Type: {}\r\n\r\n";
public static final String response_head_404 =
"HTTP/1.1 404 Not Found\r\n" +
"Content-Type: text/html\r\n\r\n";
public static final String response_head_500 = "HTTP/1.1 500 Internal Server Error\r\n"
+ "Content-Type: text/html\r\n\r\n";
public static final String textFormat_404 =
"<html><head><title>DIY Tomcat/1.0.1 - Error report</title><style>" +
"<!--H1 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:22px;} " +
"H2 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:16px;} " +
"H3 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:14px;} " +
"BODY {font-family:Tahoma,Arial,sans-serif;color:black;background-color:white;} " +
"B {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;} " +
"P {font-family:Tahoma,Arial,sans-serif;background:white;color:black;font-size:12px;}" +
"A {color : black;}A.name {color : black;}HR {color : #525D76;}--></style> " +
"</head><body><h1>HTTP Status 404 - {}</h1>" +
"<HR size='1' noshade='noshade'><p><b>type</b> Status report</p><p><b>message</b> <u>{}</u></p><p><b>description</b> " +
"<u>The requested resource is not available.</u></p><HR size='1' noshade='noshade'><h3>DiyTocmat 1.0.1</h3>" +
"</body></html>";
public static final String textFormat_500 = "<html><head><title>DIY Tomcat/1.0.1 - Error report</title><style>"
+ "<!--H1 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:22px;} "
+ "H2 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:16px;} "
+ "H3 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:14px;} "
+ "BODY {font-family:Tahoma,Arial,sans-serif;color:black;background-color:white;} "
+ "B {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;} "
+ "P {font-family:Tahoma,Arial,sans-serif;background:white;color:black;font-size:12px;}"
+ "A {color : black;}A.name {color : black;}HR {color : #525D76;}--></style> "
+ "</head><body><h1>HTTP Status 500 - An exception occurred processing {}</h1>"
+ "<HR size='1' noshade='noshade'><p><b>type</b> Exception report</p><p><b>message</b> <u>An exception occurred processing {}</u></p><p><b>description</b> "
+ "<u>The server encountered an internal error that prevented it from fulfilling this request.</u></p>"
+ "<p>Stacktrace:</p>" + "<pre>{}</pre>" + "<HR size='1' noshade='noshade'><h3>DiyTocmat 1.0.1</h3>"
+ "</body></html>";
public final static File webappsFolder = new File(SystemUtil.get("user.dir"),"webapps");
public final static File rootFolder = new File(webappsFolder,"ROOT");
public static final File confFolder = new File(SystemUtil.get("user.dir"),"conf");
public static final File serverXmlFile = new File(confFolder, "server.xml");
public static final File webXmlFile = new File(confFolder, "web.xml");
public static final File contextXmlFile = new File(confFolder, "context.xml");
}
增值内容,请先登录
自己写一个Tomcat, 几乎使用到了除开框架外的所有Java 技术,如多线程,Socket, J2EE, 反射,Log4j, JSoup, JUnit, Html 等一整套技术栈, 从无到有,循序渐进涵盖全部74个知识点,549个开发步骤, 为竞争高薪资职位加上一个有吸引力的砝码.
增值内容,点击购买
使用爬虫已经被系统记录,请勿使用爬虫,增大封号风险。 如果是误封 ,请联系站长,谢谢
package cn.how2j.diytomcat.http;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
public class Response extends BaseResponse {
private StringWriter stringWriter;
private PrintWriter writer;
private String contentType;
private byte[] body;
private int status;
public Response(){
this.stringWriter = new StringWriter();
this.writer = new PrintWriter(stringWriter);
this.contentType = "text/html";
}
public String getContentType() {
return contentType;
}
public void setContentType(String contentType) {
this.contentType = contentType;
}
public PrintWriter getWriter() {
return writer;
}
public byte[] getBody() throws UnsupportedEncodingException {
if(null==body) {
String content = stringWriter.toString();
body = content.getBytes("utf-8");
}
return body;
}
public void setBody(byte[] body) {
this.body = body;
}
@Override
public void setStatus(int status) {
this.status = status;
}
@Override
public int getStatus() {
return status;
}
}
增值内容,请先登录
自己写一个Tomcat, 几乎使用到了除开框架外的所有Java 技术,如多线程,Socket, J2EE, 反射,Log4j, JSoup, JUnit, Html 等一整套技术栈, 从无到有,循序渐进涵盖全部74个知识点,549个开发步骤, 为竞争高薪资职位加上一个有吸引力的砝码.
增值内容,点击购买
使用爬虫已经被系统记录,请勿使用爬虫,增大封号风险。 如果是误封 ,请联系站长,谢谢
package cn.how2j.diytomcat.servlets;
import cn.how2j.diytomcat.catalina.Context;
import cn.how2j.diytomcat.http.Request;
import cn.how2j.diytomcat.http.Response;
import cn.how2j.diytomcat.util.Constant;
import cn.hutool.core.util.ReflectUtil;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class InvokerServlet extends HttpServlet {
private static InvokerServlet instance = new InvokerServlet();
public static synchronized InvokerServlet getInstance() {
return instance;
}
private InvokerServlet() {
}
public void service(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse)
throws IOException, ServletException {
Request request = (Request) httpServletRequest;
Response response = (Response) httpServletResponse;
String uri = request.getUri();
Context context = request.getContext();
String servletClassName = context.getServletClassName(uri);
Object servletObject = ReflectUtil.newInstance(servletClassName);
ReflectUtil.invoke(servletObject, "service", request, response);
response.setStatus(Constant.CODE_200);
}
}
增值内容,请先登录
自己写一个Tomcat, 几乎使用到了除开框架外的所有Java 技术,如多线程,Socket, J2EE, 反射,Log4j, JSoup, JUnit, Html 等一整套技术栈, 从无到有,循序渐进涵盖全部74个知识点,549个开发步骤, 为竞争高薪资职位加上一个有吸引力的砝码.
增值内容,点击购买
使用爬虫已经被系统记录,请勿使用爬虫,增大封号风险。 如果是误封 ,请联系站长,谢谢
package cn.how2j.diytomcat.servlets;
import cn.how2j.diytomcat.catalina.Context;
import cn.how2j.diytomcat.http.Request;
import cn.how2j.diytomcat.http.Response;
import cn.how2j.diytomcat.util.Constant;
import cn.how2j.diytomcat.util.WebXMLUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.core.util.ReflectUtil;
import cn.hutool.core.util.StrUtil;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
public class DefaultServlet extends HttpServlet {
private static DefaultServlet instance = new DefaultServlet();
public static synchronized DefaultServlet getInstance() {
return instance;
}
private DefaultServlet() {
}
public void service(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse)
throws IOException, ServletException {
Request request = (Request) httpServletRequest;
Response response = (Response) httpServletResponse;
Context context = request.getContext();
String uri = request.getUri();
if ("/500.html".equals(uri))
throw new RuntimeException("this is a deliberately created exception");
if ("/".equals(uri))
uri = WebXMLUtil.getWelcomeFile(request.getContext());
String fileName = StrUtil.removePrefix(uri, "/");
File file = FileUtil.file(context.getDocBase(), fileName);
if (file.exists()) {
String extName = FileUtil.extName(file);
String mimeType = WebXMLUtil.getMimeType(extName);
response.setContentType(mimeType);
byte body[] = FileUtil.readBytes(file);
response.setBody(body);
if (fileName.equals("timeConsume.html"))
ThreadUtil.sleep(1000);
response.setStatus(Constant.CODE_200);
} else {
response.setStatus(Constant.CODE_404);
}
}
}
增值内容,请先登录
自己写一个Tomcat, 几乎使用到了除开框架外的所有Java 技术,如多线程,Socket, J2EE, 反射,Log4j, JSoup, JUnit, Html 等一整套技术栈, 从无到有,循序渐进涵盖全部74个知识点,549个开发步骤, 为竞争高薪资职位加上一个有吸引力的砝码.
增值内容,点击购买
使用爬虫已经被系统记录,请勿使用爬虫,增大封号风险。 如果是误封 ,请联系站长,谢谢
package cn.how2j.diytomcat.catalina;
import cn.how2j.diytomcat.http.Request;
import cn.how2j.diytomcat.http.Response;
import cn.how2j.diytomcat.servlets.DefaultServlet;
import cn.how2j.diytomcat.servlets.InvokerServlet;
import cn.how2j.diytomcat.util.Constant;
import cn.how2j.diytomcat.util.WebXMLUtil;
import cn.how2j.diytomcat.webappservlet.HelloServlet;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.ReUtil;
import cn.hutool.core.util.ReflectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.log.LogFactory;
import org.apache.el.util.ReflectionUtil;
import javax.servlet.Servlet;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
public class HttpProcessor {
public void execute(Socket s, Request request, Response response){
try {
String uri = request.getUri();
if(null==uri)
return;
Context context = request.getContext();
String servletClassName = context.getServletClassName(uri);
if(null!=servletClassName)
InvokerServlet.getInstance().service(request,response);
else
DefaultServlet.getInstance().service(request,response);
if(Constant.CODE_200 == response.getStatus()){
handle200(s, response);
return;
}
if(Constant.CODE_404 == response.getStatus()){
handle404(s, uri);
return;
}
} catch (Exception e) {
LogFactory.get().error(e);
handle500(s,e);
}
finally{
try {
if(!s.isClosed())
s.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
private static void handle200(Socket s, Response response) throws IOException {
String contentType = response.getContentType();
String headText = Constant.response_head_202;
headText = StrUtil.format(headText, contentType);
byte[] head = headText.getBytes();
byte[] body = response.getBody();
byte[] responseBytes = new byte[head.length + body.length];
ArrayUtil.copy(head, 0, responseBytes, 0, head.length);
ArrayUtil.copy(body, 0, responseBytes, head.length, body.length);
OutputStream os = s.getOutputStream();
os.write(responseBytes);
}
private void handle404(Socket s, String uri) throws IOException {
OutputStream os = s.getOutputStream();
String responseText = StrUtil.format(Constant.textFormat_404, uri, uri);
responseText = Constant.response_head_404 + responseText;
byte[] responseByte = responseText.getBytes("utf-8");
os.write(responseByte);
}
private void handle500(Socket s, Exception e) {
try {
OutputStream os = s.getOutputStream();
StackTraceElement stes[] = e.getStackTrace();
StringBuffer sb = new StringBuffer();
sb.append(e.toString());
sb.append("\r\n");
for (StackTraceElement ste : stes) {
sb.append("\t");
sb.append(ste.toString());
sb.append("\r\n");
}
String msg = e.getMessage();
if (null != msg && msg.length() > 20)
msg = msg.substring(0, 19);
String text = StrUtil.format(Constant.textFormat_500, msg, e.toString(), sb.toString());
text = Constant.response_head_500 + text;
byte[] responseBytes = text.getBytes("utf-8");
os.write(responseBytes);
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
增值内容,请先登录
自己写一个Tomcat, 几乎使用到了除开框架外的所有Java 技术,如多线程,Socket, J2EE, 反射,Log4j, JSoup, JUnit, Html 等一整套技术栈, 从无到有,循序渐进涵盖全部74个知识点,549个开发步骤, 为竞争高薪资职位加上一个有吸引力的砝码.
增值内容,点击购买
使用爬虫已经被系统记录,请勿使用爬虫,增大封号风险。 如果是误封 ,请联系站长,谢谢
增值内容,请先登录
自己写一个Tomcat, 几乎使用到了除开框架外的所有Java 技术,如多线程,Socket, J2EE, 反射,Log4j, JSoup, JUnit, Html 等一整套技术栈, 从无到有,循序渐进涵盖全部74个知识点,549个开发步骤, 为竞争高薪资职位加上一个有吸引力的砝码.
增值内容,点击购买
使用爬虫已经被系统记录,请勿使用爬虫,增大封号风险。 如果是误封 ,请联系站长,谢谢
HOW2J公众号,关注后实时获知最新的教程和优惠活动,谢谢。
问答区域
2020-06-15
这个finally代码块已经从HttpProcessor移到Connector了
回答已经提交成功,正在审核。 请于 我的回答 处查看回答记录,谢谢
提问之前请登陆
提问已经提交成功,正在审核。 请于 我的提问 处查看提问记录,谢谢
|