🧑🎓 个人主页:花棉袄
📖 本章内容:【Tomcat】
📢 版权: 本文由【花棉袄】原创 💝在 CSDN 首发 💝 需要转载请联系博主

🌳 静态资源
html css JavaScript jpg…🌳 动态资源
servlet/jsp php asp…🌳 IP:电子设备(计算机)在网络中的唯一标识
🌳 端口:应用程序在计算机中的唯一标识 – 0~65536
🌳 传输协议:规定了数据传输的规则



<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<groupId>org.apache.tomcatgroupId>
<artifactId>Tomcat8.5artifactId>
<name>Tomcat8.5name>
<version>8.5version>
<build>
<finalName>Tomcat8.5finalName>
<sourceDirectory>javasourceDirectory>
<testSourceDirectory>testtestSourceDirectory>
<resources>
<resource>
<directory>javadirectory>
resource>
resources>
<testResources>
<testResource>
<directory>testdirectory>
testResource>
testResources>
<plugins>
<plugin>
<groupId>org.apache.maven.pluginsgroupId>
<artifactId>maven-compiler-pluginartifactId>
<version>2.3version>
<configuration>
<encoding>UTF-8encoding>
<source>1.8source>
<target>1.8target>
configuration>
plugin>
plugins>
build>
<dependencies>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.12version>
<scope>testscope>
dependency>
<dependency>
<groupId>org.easymockgroupId>
<artifactId>easymockartifactId>
<version>3.4version>
dependency>
<dependency>
<groupId>antgroupId>
<artifactId>antartifactId>
<version>1.7.0version>
dependency>
<dependency>
<groupId>wsdl4jgroupId>
<artifactId>wsdl4jartifactId>
<version>1.6.2version>
dependency>
<dependency>
<groupId>javax.xmlgroupId>
<artifactId>jaxrpcartifactId>
<version>1.1version>
dependency>
<dependency>
<groupId>org.eclipse.jdt.core.compilergroupId>
<artifactId>ecjartifactId>
<version>4.5.1version>
dependency>
dependencies>
project>

‐Dcatalina.home=E:/JAVA\tomcat/apache-tomcat-8.5.42-src/home
‐Dcatalina.base=E:/JAVA\tomcat/apache-tomcat-8.5.42-src/home
‐Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager
‐Djava.util.logging.config.file=E:/JAVA/tomcat/apache-tomcat-8.5.42-src/home/conf/logging.properties
出现上述异常的原因,是我们直接启动org.apache.catalina.startup.Bootstrap的时候没有加载JasperInitializer,从而无法编译JSP
解决办法是在tomcat的源码ContextConfig中的configureStart函数中手动将JSP解析器初始化:
在ContextConfig类中添加:context.addServletContainerInitializer(new JasperInitializer(), null);



🌳 点击 Tomcat Server -> Deployment

🌳 设置项目路径

🌳 启动项目

🌳在项目的 web 路径下打 war 包:jar -cvf 文件名.war .
jar -cvf war包名称 空格 .,最后的一个点代表当前路径


🌳虚拟目录的作用:可以发布任意目录下的项目
标签




🌳 Servlet容器工作流程
ServletRequest对象把客户的请求信息封装起来根据请求的URL和Servlet的映射关系找到相应的Servlet反射机制创建这个ServletServlet的service方法来处理请求HTTP服务器会把响应发送给客户端
🌳 Tomcat要实现两个核心功能
🌳 Tomcat设计了两个核心组件

Servlet 是运行在 Java 服务器端的程序,用于接收和响应来自客户端基于 HTTP 协议的请求
如果想实现 Servlet 的功能,可以通过实现 javax.servlet.Servlet 接口或者继承它的实现类
核心方法:service(),任何客户端的请求都会经过该方法

🌳 创建一个类继承 GenericServlet,重写 service 方法
public class HelloServlet extends GenericServlet {
@Override
public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
System.out.println("方法执行了");
}
}
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<servlet>
<servlet-name>testServlet</servlet-name>
<servlet-class>com.michale.javaweb.TestServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>testServlet</servlet-name>
<url-pattern>/testServlet</url-pattern>
</servlet-mapping>
</web-app>


🌳Servlet的生命周期结论
public class HelloServlet extends HttpServlet {
@Override
public void init() throws ServletException {
System.out.println("对象创建并初始化了...");
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("接收到了客户端的请求...");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req,resp);
}
@Override
public void destroy() {
System.out.println("对象销毁了...");
}
}


具体名称的方式/ 开头 + 通配符的方式通配符 + 固定格式结尾的方式第一次访问时创建:
服务器加载时创建:
正整数代表服务器加载时创建,值越小、优先级越高
标签中,通过标签来配置。有两个子标签:代表初始化参数的 key:代表初始化参数的 value

<servlet>
<servlet-name>HelloServletservlet-name>
<servlet-class>com.example.demo01.HelloServletservlet-class>
<init-param>
<param-name>encodingparam-name>
<param-value>UTF-8param-value>
init-param>
<init-param>
<param-name>nameparam-name>
<param-value>michaleparam-value>
init-param>
servlet>
<servlet-mapping>
<servlet-name>HelloServletservlet-name>
<url-pattern>/hello-servleturl-pattern>
servlet-mapping>
public class TestServletConfig extends HttpServlet {
//声明ServletConfig配置对象
private ServletConfig config;
@Override
public void init(ServletConfig config) throws ServletException {
this.config = config;
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//获取key值
String name = config.getInitParameter("name");
System.out.println("name:"+name);
//获取所有的key
Enumeration<String> names = config.getInitParameterNames();
//遍历得到的key
while(names.hasMoreElements()) {
//获取每一个key
String key = names.nextElement();
//通过key获取value
String value = config.getInitParameter(key);
System.out.println("name:" + key + ",value:" + value);
}
//获取ServletContext对象
ServletContext context = config.getServletContext();
System.out.println(context);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req,resp);
}
}


ServletContext 并不属于某个 Servlet 的配置,而是针对于整个应用的配置,也叫全局的初始化参数
在标签中,通过标签来配置。有两个子标签
:代表全局初始化参数的 key
:代表全局初始化参数的 value




<web-app>
....
<servlet>
<servlet-name>servletContextDemoservlet-name>
<servlet-class>com.example.demo01.ServletContextDemoservlet-class>
servlet>
<servlet-mapping>
<servlet-name>servletContextDemoservlet-name>
<url-pattern>/servletContextDemourl-pattern>
servlet-mapping>
<context-param>
<param-name>globalEncodingparam-name>
<param-value>UTF-8param-value>
context-param>
<context-param>
<param-name>globalDescparam-name>
<param-value>This is ServletContextparam-value>
context-param>
web-app>
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//获取ServletContext对象
ServletContext context = getServletContext();
//获取全局配置的globalEncoding
String value = context.getInitParameter("globalDesc");
System.out.println(value);
//获取应用的访问虚拟目录
String contextPath = context.getContextPath();
System.out.println(contextPath);
//根据虚拟目录获取应用部署的磁盘绝对路径
String realPath = context.getRealPath("/");
System.out.println(realPath);
//获取b.txt文件的绝对路径
String b = context.getRealPath("/b.txt");
System.out.println(b);
//获取c.txt文件的绝对路径
String c = context.getRealPath("/WEB-INF/c.txt");
System.out.println(c);
//获取a.txt文件的绝对路径
String a = context.getRealPath("/WEB-INF/classes/a.txt");
System.out.println(a);
}

🌳 共享数据
//修改ServletContextDemo:存储数据
//向域对象中存储数据
context.setAttribute("username","zhangsan");
//修改ServletConfigDemo:获取数据
//获取ServletContextDemo设置共享的数据
Object username = context.getAttribute("username");
System.out.println(username);






@WebServlet("/servletDemo1")
public class ServletDemo1 extends HttpServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("servlet执行了...");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req,resp);
}
}