步骤 1 : 拓扑图点亮 步骤 2 : ServletContext 步骤 3 : BaseServletContext 步骤 4 : ApplicationContext 步骤 5 : Context 步骤 6 : Request 步骤 7 : DefaultServlet 步骤 8 : ServletContext 的意义 步骤 9 : TestTomcat 步骤 10 : 比较可运行项目,快速定位问题
增值内容,请先登录
自己写一个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.http;
import javax.servlet.*;
import javax.servlet.descriptor.JspConfigDescriptor;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Enumeration;
import java.util.EventListener;
import java.util.Map;
import java.util.Set;
public class BaseServletContext implements ServletContext {
@Override
public ServletContext getContext(String s) {
return null;
}
@Override
public String getContextPath() {
return null;
}
@Override
public int getMajorVersion() {
return 0;
}
@Override
public int getMinorVersion() {
return 0;
}
@Override
public int getEffectiveMajorVersion() {
return 0;
}
@Override
public int getEffectiveMinorVersion() {
return 0;
}
@Override
public String getMimeType(String s) {
return null;
}
@Override
public Set<String> getResourcePaths(String s) {
return null;
}
@Override
public URL getResource(String s) throws MalformedURLException {
return null;
}
@Override
public InputStream getResourceAsStream(String s) {
return null;
}
@Override
public RequestDispatcher getRequestDispatcher(String s) {
return null;
}
@Override
public RequestDispatcher getNamedDispatcher(String s) {
return null;
}
@Override
public Servlet getServlet(String s) throws ServletException {
return null;
}
@Override
public Enumeration<Servlet> getServlets() {
return null;
}
@Override
public Enumeration<String> getServletNames() {
return null;
}
@Override
public void log(String s) {
}
@Override
public void log(Exception e, String s) {
}
@Override
public void log(String s, Throwable throwable) {
}
@Override
public String getRealPath(String s) {
return null;
}
@Override
public String getServerInfo() {
return null;
}
@Override
public String getInitParameter(String s) {
return null;
}
@Override
public Enumeration<String> getInitParameterNames() {
return null;
}
@Override
public boolean setInitParameter(String s, String s1) {
return false;
}
@Override
public Object getAttribute(String s) {
return null;
}
@Override
public Enumeration<String> getAttributeNames() {
return null;
}
@Override
public void setAttribute(String s, Object o) {
}
@Override
public void removeAttribute(String s) {
}
@Override
public String getServletContextName() {
return null;
}
@Override
public ServletRegistration.Dynamic addServlet(String s, String s1) {
return null;
}
@Override
public ServletRegistration.Dynamic addServlet(String s, Servlet servlet) {
return null;
}
@Override
public ServletRegistration.Dynamic addServlet(String s, Class<? extends Servlet> aClass) {
return null;
}
@Override
public <T extends Servlet> T createServlet(Class<T> aClass) throws ServletException {
return null;
}
@Override
public ServletRegistration getServletRegistration(String s) {
return null;
}
@Override
public Map<String, ? extends ServletRegistration> getServletRegistrations() {
return null;
}
@Override
public FilterRegistration.Dynamic addFilter(String s, String s1) {
return null;
}
@Override
public FilterRegistration.Dynamic addFilter(String s, Filter filter) {
return null;
}
@Override
public FilterRegistration.Dynamic addFilter(String s, Class<? extends Filter> aClass) {
return null;
}
@Override
public <T extends Filter> T createFilter(Class<T> aClass) throws ServletException {
return null;
}
@Override
public FilterRegistration getFilterRegistration(String s) {
return null;
}
@Override
public Map<String, ? extends FilterRegistration> getFilterRegistrations() {
return null;
}
@Override
public SessionCookieConfig getSessionCookieConfig() {
return null;
}
@Override
public void setSessionTrackingModes(Set<SessionTrackingMode> set) throws IllegalStateException, IllegalArgumentException {
}
@Override
public Set<SessionTrackingMode> getDefaultSessionTrackingModes() {
return null;
}
@Override
public Set<SessionTrackingMode> getEffectiveSessionTrackingModes() {
return null;
}
@Override
public void addListener(String s) {
}
@Override
public <T extends EventListener> void addListener(T t) {
}
@Override
public void addListener(Class<? extends EventListener> aClass) {
}
@Override
public <T extends EventListener> T createListener(Class<T> aClass) throws ServletException {
return null;
}
@Override
public void declareRoles(String... strings) {
}
@Override
public ClassLoader getClassLoader() {
return null;
}
@Override
public JspConfigDescriptor getJspConfigDescriptor() {
return null;
}
}
增值内容,请先登录
自己写一个Tomcat, 几乎使用到了除开框架外的所有Java 技术,如多线程,Socket, J2EE, 反射,Log4j, JSoup, JUnit, Html 等一整套技术栈, 从无到有,循序渐进涵盖全部74个知识点,549个开发步骤, 为竞争高薪资职位加上一个有吸引力的砝码.
增值内容,点击购买
使用爬虫已经被系统记录,请勿使用爬虫,增大封号风险。 如果是误封 ,请联系站长,谢谢
package cn.how2j.diytomcat.http;
import java.io.File;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Collections;
import java.util.Enumeration;
import java.util.EventListener;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import javax.servlet.Filter;
import javax.servlet.FilterRegistration;
import javax.servlet.FilterRegistration.Dynamic;
import javax.servlet.RequestDispatcher;
import javax.servlet.Servlet;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;
import javax.servlet.SessionCookieConfig;
import javax.servlet.SessionTrackingMode;
import javax.servlet.descriptor.JspConfigDescriptor;
import cn.how2j.diytomcat.catalina.Context;
import com.sun.prism.impl.BaseContext;
public class ApplicationContext extends BaseServletContext {
private Map<String, Object> attributesMap;
private Context context;
public ApplicationContext(Context context) {
this.attributesMap = new HashMap<>();
this.context = context;
}
public void removeAttribute(String name) {
attributesMap.remove(name);
}
public void setAttribute(String name, Object value) {
attributesMap.put(name, value);
}
public Object getAttribute(String name) {
return attributesMap.get(name);
}
public Enumeration<String> getAttributeNames() {
Set<String> keys = attributesMap.keySet();
return Collections.enumeration(keys);
}
public String getRealPath(String path) {
return new File(context.getDocBase(), path).getAbsolutePath();
}
}
增值内容,请先登录
自己写一个Tomcat, 几乎使用到了除开框架外的所有Java 技术,如多线程,Socket, J2EE, 反射,Log4j, JSoup, JUnit, Html 等一整套技术栈, 从无到有,循序渐进涵盖全部74个知识点,549个开发步骤, 为竞争高薪资职位加上一个有吸引力的砝码.
增值内容,点击购买
使用爬虫已经被系统记录,请勿使用爬虫,增大封号风险。 如果是误封 ,请联系站长,谢谢
package cn.how2j.diytomcat.catalina;
import cn.how2j.diytomcat.classloader.WebappClassLoader;
import cn.how2j.diytomcat.exception.WebConfigDuplicatedException;
import cn.how2j.diytomcat.http.ApplicationContext;
import cn.how2j.diytomcat.util.ContextXMLUtil;
import cn.how2j.diytomcat.watcher.ContextFileChangeWatcher;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.TimeInterval;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.log.LogFactory;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import javax.servlet.ServletContext;
import java.io.File;
import java.util.*;
public class Context {
private String path;
private String docBase;
private File contextWebXmlFile;
private Map<String, String> url_servletClassName;
private Map<String, String> url_ServletName;
private Map<String, String> servletName_className;
private Map<String, String> className_servletName;
private WebappClassLoader webappClassLoader;
private Host host;
private boolean reloadable;
private ContextFileChangeWatcher contextFileChangeWatcher;
private ServletContext servletContext;
public Context(String path, String docBase, Host host, boolean reloadable) {
TimeInterval timeInterval = DateUtil.timer();
this.host = host;
this.reloadable = reloadable;
this.path = path;
this.docBase = docBase;
this.contextWebXmlFile = new File(docBase, ContextXMLUtil.getWatchedResource());
this.url_servletClassName = new HashMap<>();
this.url_ServletName = new HashMap<>();
this.servletName_className = new HashMap<>();
this.className_servletName = new HashMap<>();
this.servletContext = new ApplicationContext(this);
ClassLoader commonClassLoader = Thread.currentThread().getContextClassLoader();
this.webappClassLoader = new WebappClassLoader(docBase, commonClassLoader);
LogFactory.get().info("Deploying web application directory {}", this.docBase);
deploy();
LogFactory.get().info("Deployment of web application directory {} has finished in {} ms", this.docBase,timeInterval.intervalMs());
}
public void reload() {
host.reload(this);
}
private void deploy() {
init();
if(reloadable){
contextFileChangeWatcher = new ContextFileChangeWatcher(this);
contextFileChangeWatcher.start();
}
}
private void init() {
if (!contextWebXmlFile.exists())
return;
try {
checkDuplicated();
} catch (WebConfigDuplicatedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return;
}
String xml = FileUtil.readUtf8String(contextWebXmlFile);
Document d = Jsoup.parse(xml);
parseServletMapping(d);
}
private void parseServletMapping(Document d) {
// url_ServletName
Elements mappingurlElements = d.select("servlet-mapping url-pattern");
for (Element mappingurlElement : mappingurlElements) {
String urlPattern = mappingurlElement.text();
String servletName = mappingurlElement.parent().select("servlet-name").first().text();
url_ServletName.put(urlPattern, servletName);
}
// servletName_className / className_servletName
Elements servletNameElements = d.select("servlet servlet-name");
for (Element servletNameElement : servletNameElements) {
String servletName = servletNameElement.text();
String servletClass = servletNameElement.parent().select("servlet-class").first().text();
servletName_className.put(servletName, servletClass);
className_servletName.put(servletClass, servletName);
}
// url_servletClassName
Set<String> urls = url_ServletName.keySet();
for (String url : urls) {
String servletName = url_ServletName.get(url);
String servletClassName = servletName_className.get(servletName);
url_servletClassName.put(url, servletClassName);
}
}
private void checkDuplicated(Document d, String mapping, String desc) throws WebConfigDuplicatedException {
Elements elements = d.select(mapping);
// 判断逻辑是放入一个集合,然后把集合排序之后看两临两个元素是否相同
List<String> contents = new ArrayList<>();
for (Element e : elements) {
contents.add(e.text());
}
Collections.sort(contents);
for (int i = 0; i < contents.size() - 1; i++) {
String contentPre = contents.get(i);
String contentNext = contents.get(i + 1);
if (contentPre.equals(contentNext)) {
throw new WebConfigDuplicatedException(StrUtil.format(desc, contentPre));
}
}
}
private void checkDuplicated() throws WebConfigDuplicatedException {
String xml = FileUtil.readUtf8String(contextWebXmlFile);
Document d = Jsoup.parse(xml);
checkDuplicated(d, "servlet-mapping url-pattern", "servlet url 重复,请保持其唯一性:{} ");
checkDuplicated(d, "servlet servlet-name", "servlet 名称重复,请保持其唯一性:{} ");
checkDuplicated(d, "servlet servlet-class", "servlet 类名重复,请保持其唯一性:{} ");
}
public String getServletClassName(String uri) {
return url_servletClassName.get(uri);
}
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
public String getDocBase() {
return docBase;
}
public void setDocBase(String docBase) {
this.docBase = docBase;
}
public WebappClassLoader getWebappClassLoader() {
return webappClassLoader;
}
public void stop() {
webappClassLoader.stop();
contextFileChangeWatcher.stop();
}
public boolean isReloadable() {
return reloadable;
}
public void setReloadable(boolean reloadable) {
this.reloadable = reloadable;
}
public ServletContext getServletContext() {
return servletContext;
}
}
增值内容,请先登录
自己写一个Tomcat, 几乎使用到了除开框架外的所有Java 技术,如多线程,Socket, J2EE, 反射,Log4j, JSoup, JUnit, Html 等一整套技术栈, 从无到有,循序渐进涵盖全部74个知识点,549个开发步骤, 为竞争高薪资职位加上一个有吸引力的砝码.
增值内容,点击购买
使用爬虫已经被系统记录,请勿使用爬虫,增大封号风险。 如果是误封 ,请联系站长,谢谢
public ServletContext getServletContext() {
return context.getServletContext();
}
public ServletContext getServletContext() { return context.getServletContext(); }
增值内容,请先登录
自己写一个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(request.getRealPath(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个开发步骤, 为竞争高薪资职位加上一个有吸引力的砝码.
增值内容,点击购买
使用爬虫已经被系统记录,请勿使用爬虫,增大封号风险。 如果是误封 ,请联系站长,谢谢
增值内容,请先登录
自己写一个Tomcat, 几乎使用到了除开框架外的所有Java 技术,如多线程,Socket, J2EE, 反射,Log4j, JSoup, JUnit, Html 等一整套技术栈, 从无到有,循序渐进涵盖全部74个知识点,549个开发步骤, 为竞争高薪资职位加上一个有吸引力的砝码.
增值内容,点击购买
使用爬虫已经被系统记录,请勿使用爬虫,增大封号风险。 如果是误封 ,请联系站长,谢谢
增值内容,请先登录
自己写一个Tomcat, 几乎使用到了除开框架外的所有Java 技术,如多线程,Socket, J2EE, 反射,Log4j, JSoup, JUnit, Html 等一整套技术栈, 从无到有,循序渐进涵盖全部74个知识点,549个开发步骤, 为竞争高薪资职位加上一个有吸引力的砝码.
增值内容,点击购买
使用爬虫已经被系统记录,请勿使用爬虫,增大封号风险。 如果是误封 ,请联系站长,谢谢
HOW2J公众号,关注后实时获知最新的教程和优惠活动,谢谢。
提问之前请登陆
提问已经提交成功,正在审核。 请于 我的提问 处查看提问记录,谢谢
|