一.什么是Servlet?
1.Servlet是javaEE规范之一。规范就是接口。
2.Servlet是javaWeb三大组件之一。三大组件分别是:Servlet程序,Filter过滤器,Listener监听器。
3.Servlet是运行在服务器上的一个java小程序,它可以接收客户端发送过来的请求,并响应数据给客户端。
sevlet标签 | servlet-mapping标签 |
---|---|
servlet-name :Servlet程序起的一个别名(一般是类名) | servlet-name:告诉服务器,当前配置的地址给哪个Servlet程序使用 |
servlet-class:Servlet程序的全类名 | url-pattern:标签配置访问地址 |
<?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>servletDemo</servlet-name>
<!--要配置的Servlet的全限定类名-->
<servlet-class>com.weijisheng.ServletDemo</servlet-class>
</servlet>
<servlet-mapping>
<!--和servlet里面的servlet-name保持一致-->
<servlet-name>servletDemo</servlet-name>
<!--客户端能够访问的映射路径,目前一定要以/开头-->
<url-pattern>/demo01</url-pattern>
</servlet-mapping>
</web-app>
注解方法配置Servlet是在类上加入 @webServlet("/url")
注意:一个Servlet可以配置多个映射路径,但是多个Servlet不能配置一个映射路径。
二.Servlet生命周期?
1.执行Servlet构造器方法。
2.执行init初始化方法。
第一、二步,是在第一次访问的时候创建Service程序会调用。
3.执行service方法。
第三步,每次访问都会调用。
4.执行destroy销毁方法。
第四步,在web工程停止的时候调用。
Servlet的启动项:在服务器启动的时候就创建
注解方式:
配置文件方式:
<load-on-startup>1</load-on-startup>
三.Servlet的三种映射路径的配置
四.Servlet的默认访问路径以及注意事项
**Servlet默认到index.html/jsp,如果想默认到我们表单界面的话可以在web.xml中添加,
** <welcome-file-list> <welcome-file>Login.jsp</welcome-file> </welcome-file-list>
,因为是从index开始的,所以action里面的路径不用加/直接写。
五.ServletContext公共容器实现共享
1.ServletContext的作用:
- 作为域对象存取数据,让Servlet共享(掌握)
思路:两个容量想交换数据,有个共享容器。代码如下:
第1个容器
@WebServlet("/ServletContextDemo1")
public class ServletContextDemo1 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1.获取ServletContext对象
ServletContext servletContext = getServletContext();
//2.往ServletContext里面存入数据
String name="我是共享的数据";
servletContext.setAttribute("str",name);
}
第2个容器
@WebServlet("/ServletContextDemo2")
public class ServletContextDemo2 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1.获取ServletContext对象
ServletContext servletContext = getServletContext();
//2.从ServletContext里面拿取数据
Object str = servletContext.getAttribute("str");
System.out.println("获取到的数据"+str);
}
- 获得文件MIME类型(文件下载)(了解)
- getServletContext().getMimeType(文件名)
- 获得全局初始化参数(了解)
- 获取web资源路径,可以将Web资源转换成字节输入流(掌握)
getServletContext().getRealPath()这个路径可以动态的获取web资源文件的真实路径。(非常重要)
五.小结流(重要)
1.如果是将类路径下面文件转化成流使用类加载器的getResourceAdStream()方法
2.如果是将web里面的资源转换成流就使用ServletContext的getResourceAsStream()方法
六.利用ServletContext获取网站访问次数
- 先在计数器Servlet界面重写init方法定义一个计时器放在ServletContext里面
- 在doGet()方法里面得到计时器,并对数据进行+1操作
- 在新的Show的Servlet界面把统计的次数打印出来。
CountServlet
package com.weijisheng.servlet;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* @author weijisheng
* @create 2021-10-23 22:41
*/
@WebServlet("/Count")
public class CountServlet extends HttpServlet {
@Override
public void init() throws ServletException {
//往ServletContext中放入一个计时器,数值为0
getServletContext().setAttribute("count",0);
super.init();
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//取出初始化好的count对其加一操作
Integer count = (Integer) getServletContext().getAttribute("count");
count++;
//放回到ServletContext中
getServletContext().setAttribute("count",count);
}
}
ShowServlet
package com.weijisheng.servlet;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* @author weijisheng
* @create 2021-10-23 22:50
*/
@WebServlet("/Show")
public class ShowServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Object count = getServletContext().getAttribute("count");
response.getWriter().print(count);//打印访问次数
}
}