Servlet 与 MVC

d ↵

d第一节 Servlet

1. Servlet 概念

Servlet 是在服务器上运行的能够对客户端请求进行处理,并返回处理结果的程序

2. Servlet 体系结构

2.1 Servlet 接口

<span style="background-color:#f8f8f8"><span style="color:#333333"><span style="color:#aa5500">//Servlet对象的初始化,Servlet 对象初始化后才能处理请求,由 Servlet 容器调用</span>
<span style="color:#770088">public</span> <span style="color:#008855">void</span> <span style="color:#0000ff">init</span>(<span style="color:#000000">ServletConfig</span> <span style="color:#000000">config</span>) <span style="color:#770088">throws</span> <span style="color:#000000">ServletException</span>;
<span style="color:#aa5500">//获取Servlet配置信息</span>
<span style="color:#770088">public</span> <span style="color:#000000">ServletConfig</span> <span style="color:#0000ff">getServletConfig</span>();
<span style="color:#aa5500">//处理客户端的请求,由 Servlet 容器调用</span>
<span style="color:#770088">public</span> <span style="color:#008855">void</span> <span style="color:#0000ff">service</span>(<span style="color:#000000">ServletRequest</span> <span style="color:#000000">req</span>, <span style="color:#000000">ServletResponse</span> <span style="color:#000000">res</span>)
    <span style="color:#770088">throws</span> <span style="color:#000000">ServletException</span>, <span style="color:#000000">IOException</span>;
<span style="color:#aa5500">//返回有关 Servlet 的信息,比如作者、版本和版权</span>
<span style="color:#770088">public</span> <span style="color:#008855">String</span> <span style="color:#0000ff">getServletInfo</span>();
<span style="color:#aa5500">//销毁Servlet,由 Servlet 容器调用</span>
<span style="color:#770088">public</span> <span style="color:#008855">void</span> <span style="color:#0000ff">destroy</span>();</span></span>

2.2 ServletConfig 接口

<span style="background-color:#f8f8f8"><span style="color:#333333"><span style="color:#aa5500">//获取Servlet的实例名称</span>
<span style="color:#770088">public</span> <span style="color:#008855">String</span> <span style="color:#0000ff">getServletName</span>();
<span style="color:#aa5500">//返回正在执行的Servlet所在的上下文对象</span>
<span style="color:#770088">public</span> <span style="color:#000000">ServletContext</span> <span style="color:#0000ff">getServletContext</span>();
<span style="color:#aa5500">//获取Servlet中给定名称的初始化参数</span>
<span style="color:#770088">public</span> <span style="color:#008855">String</span> <span style="color:#0000ff">getInitParameter</span>(<span style="color:#008855">String</span> <span style="color:#000000">name</span>);
<span style="color:#aa5500">//获取Servlet中所有的初始化参数</span>
<span style="color:#770088">public</span> <span style="color:#000000">Enumeration</span><span style="color:#981a1a"><</span><span style="color:#008855">String</span><span style="color:#981a1a">></span> <span style="color:#0000ff">getInitParameterNames</span>();</span></span>

2.3 Servlet 案例

  • 编写Servlet

    <span style="background-color:#f8f8f8"><span style="color:#770088">package</span> <span style="color:#0000ff">com</span>.<span style="color:#000000">qf</span>.<span style="color:#000000">jsp</span>.<span style="color:#000000">servlet</span>;
    ​
    <span style="color:#770088">import</span> <span style="color:#000000">javax</span>.<span style="color:#000000">servlet</span>.<span style="color:#981a1a">*</span>;
    <span style="color:#770088">import</span> <span style="color:#000000">java</span>.<span style="color:#000000">io</span>.<span style="color:#000000">IOException</span>;
    <span style="color:#770088">import</span> <span style="color:#000000">java</span>.<span style="color:#000000">util</span>.<span style="color:#000000">Enumeration</span>;
    ​
    <span style="color:#770088">public</span> <span style="color:#770088">class</span> <span style="color:#0000ff">LoginServlet</span> <span style="color:#770088">implements</span> <span style="color:#000000">Servlet</span> {
    ​
        <span style="color:#770088">private</span> <span style="color:#000000">ServletConfig</span> <span style="color:#000000">servletConfig</span>;
    ​
        <span style="color:#555555">@Override</span>
        <span style="color:#770088">public</span> <span style="color:#008855">void</span> <span style="color:#000000">init</span>(<span style="color:#000000">ServletConfig</span> <span style="color:#000000">config</span>) <span style="color:#770088">throws</span> <span style="color:#000000">ServletException</span> {
            <span style="color:#770088">this</span>.<span style="color:#000000">servletConfig</span> <span style="color:#981a1a">=</span> <span style="color:#000000">config</span>;
            <span style="color:#008855">String</span> <span style="color:#000000">servletName</span> <span style="color:#981a1a">=</span> <span style="color:#000000">config</span>.<span style="color:#000000">getServletName</span>();
            <span style="color:#000000">System</span>.<span style="color:#000000">out</span>.<span style="color:#000000">println</span>(<span style="color:#aa1111">"Servlet 实例的名称:"</span> <span style="color:#981a1a">+</span> <span style="color:#000000">servletName</span>);
            <span style="color:#aa5500">//获取Servlet中所有的初始化参数</span>
            <span style="color:#000000">Enumeration</span><span style="color:#981a1a"><</span><span style="color:#008855">String</span><span style="color:#981a1a">></span> <span style="color:#000000">initParameterNames</span> <span style="color:#981a1a">=</span> <span style="color:#000000">config</span>.<span style="color:#000000">getInitParameterNames</span>();
            <span style="color:#770088">while</span> (<span style="color:#000000">initParameterNames</span>.<span style="color:#000000">hasMoreElements</span>()) {
                <span style="color:#008855">String</span> <span style="color:#000000">initParameterName</span> <span style="color:#981a1a">=</span> <span style="color:#000000">initParameterNames</span>.<span style="color:#000000">nextElement</span>();
                <span style="color:#aa5500">//获取Servlet中给定名称的初始化参数</span>
                <span style="color:#008855">String</span> <span style="color:#000000">initParameterValue</span> <span style="color:#981a1a">=</span> <span style="color:#000000">config</span>.<span style="color:#000000">getInitParameter</span>(<span style="color:#000000">initParameterName</span>);
                <span style="color:#000000">System</span>.<span style="color:#000000">out</span>.<span style="color:#000000">println</span>(<span style="color:#aa1111">"Servlet 初始化参数 "</span> <span style="color:#981a1a">+</span> <span style="color:#000000">initParameterName</span> <span style="color:#981a1a">+</span> <span style="color:#aa1111">":"</span> <span style="color:#981a1a">+</span> <span style="color:#000000">initParameterValue</span>);
            }
        }
    ​
        <span style="color:#555555">@Override</span>
        <span style="color:#770088">public</span> <span style="color:#000000">ServletConfig</span> <span style="color:#000000">getServletConfig</span>() {
            <span style="color:#770088">return</span> <span style="color:#000000">servletConfig</span>;
        }
    ​
        <span style="color:#555555">@Override</span>
        <span style="color:#770088">public</span> <span style="color:#008855">void</span> <span style="color:#000000">service</span>(<span style="color:#000000">ServletRequest</span> <span style="color:#000000">servletRequest</span>, <span style="color:#000000">ServletResponse</span> <span style="color:#000000">servletResponse</span>) <span style="color:#770088">throws</span> <span style="color:#000000">ServletException</span>, <span style="color:#000000">IOException</span> {
            <span style="color:#000000">System</span>.<span style="color:#000000">out</span>.<span style="color:#000000">println</span>(<span style="color:#aa1111">"Servlet 处理请求"</span>);
        }
    ​
        <span style="color:#555555">@Override</span>
        <span style="color:#770088">public</span> <span style="color:#008855">String</span> <span style="color:#000000">getServletInfo</span>() {
            <span style="color:#770088">return</span> <span style="color:#aa1111">"Login Servlet"</span>;
        }
    ​
        <span style="color:#555555">@Override</span>
        <span style="color:#770088">public</span> <span style="color:#008855">void</span> <span style="color:#000000">destroy</span>() {
            <span style="color:#000000">System</span>.<span style="color:#000000">out</span>.<span style="color:#000000">println</span>(<span style="color:#aa1111">"Servlet 销毁"</span>);
        }
    }</span>
  • 配置 web.xml

    <span style="background-color:#f8f8f8"><span style="color:#555555"><?xml</span> <span style="color:#555555">version="1.0" encoding="UTF-8"?></span>
    <span style="color:#117700"><</span><span style="color:#117700">web-app</span> <span style="color:#0000cc">xmlns</span>=<span style="color:#aa1111">"http://xmlns.jcp.org/xml/ns/javaee"</span>
             <span style="color:#0000cc">xmlns:xsi</span>=<span style="color:#aa1111">"http://www.w3.org/2001/XMLSchema-instance"</span>
             <span style="color:#0000cc">xsi:schemaLocation</span>=<span style="color:#aa1111">"http://xmlns.jcp.org/xml/ns/javaee</span>
                          <span style="color:#aa1111">http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"</span>
             <span style="color:#0000cc">version</span>=<span style="color:#aa1111">"4.0"</span><span style="color:#117700">></span>
      
      <span style="color:#117700"><</span><span style="color:#117700">display-name</span><span style="color:#117700">></span>ServletApp<span style="color:#117700"></</span><span style="color:#117700">display-name</span><span style="color:#117700">></span>
      
      <span style="color:#aa5500"><!--Servlet 实例配置--></span>
      <span style="color:#117700"><</span><span style="color:#117700">servlet</span><span style="color:#117700">></span>
        <span style="color:#aa5500"><!-- Servlet 实例的名称--></span>
        <span style="color:#117700"><</span><span style="color:#117700">servlet-name</span><span style="color:#117700">></span>loginServlet<span style="color:#117700"></</span><span style="color:#117700">servlet-name</span><span style="color:#117700">></span>
        <span style="color:#aa5500"><!--Servlet 的类型--></span>
        <span style="color:#117700"><</span><span style="color:#117700">servlet-class</span><span style="color:#117700">></span>com.qf.jsp.servlet.LoginServlet<span style="color:#117700"></</span><span style="color:#117700">servlet-class</span><span style="color:#117700">></span>
        <span style="color:#117700"><</span><span style="color:#117700">init-param</span><span style="color:#117700">></span>
          <span style="color:#aa5500"><!--初始化参数的名称--></span>
          <span style="color:#117700"><</span><span style="color:#117700">param-name</span><span style="color:#117700">></span>characterEncoding<span style="color:#117700"></</span><span style="color:#117700">param-name</span><span style="color:#117700">></span>
          <span style="color:#aa5500"><!--初始化参数的值--></span>
          <span style="color:#117700"><</span><span style="color:#117700">param-value</span><span style="color:#117700">></span>UTF-8<span style="color:#117700"></</span><span style="color:#117700">param-value</span><span style="color:#117700">></span>
        <span style="color:#117700"></</span><span style="color:#117700">init-param</span><span style="color:#117700">></span>
      <span style="color:#117700"></</span><span style="color:#117700">servlet</span><span style="color:#117700">></span>
      <span style="color:#aa5500"><!--Servlet 实例与请求地址的映射配置--></span>
      <span style="color:#117700"><</span><span style="color:#117700">servlet-mapping</span><span style="color:#117700">></span>
        <span style="color:#aa5500"><!-- Servlet 实例的名称--></span>
        <span style="color:#117700"><</span><span style="color:#117700">servlet-name</span><span style="color:#117700">></span>loginServlet<span style="color:#117700"></</span><span style="color:#117700">servlet-name</span><span style="color:#117700">></span>
        <span style="color:#aa5500"><!-- Servlet 匹配的请求地址--></span>
        <span style="color:#117700"><</span><span style="color:#117700">url-pattern</span><span style="color:#117700">></span>/login<span style="color:#117700"></</span><span style="color:#117700">url-pattern</span><span style="color:#117700">></span>
      <span style="color:#117700"></</span><span style="color:#117700">servlet-mapping</span><span style="color:#117700">></span>
      <span style="color:#aa5500"><!--session 配置--></span>
      <span style="color:#117700"><</span><span style="color:#117700">session-config</span><span style="color:#117700">></span>
        <span style="color:#aa5500"><!--超时时间配置--></span>
        <span style="color:#117700"><</span><span style="color:#117700">session-timeout</span><span style="color:#117700">></span>30<span style="color:#117700"></</span><span style="color:#117700">session-timeout</span><span style="color:#117700">></span>
      <span style="color:#117700"></</span><span style="color:#117700">session-config</span><span style="color:#117700">></span>
    <span style="color:#117700"></</span><span style="color:#117700">web-app</span><span style="color:#117700">></span></span>
  • 获取初始化参数信息

    <span style="background-color:#f8f8f8"><span style="color:#555555">@Override</span>
    <span style="color:#770088">public</span> <span style="color:#008855">void</span> <span style="color:#0000ff">init</span>(<span style="color:#000000">ServletConfig</span> <span style="color:#000000">config</span>) <span style="color:#770088">throws</span> <span style="color:#000000">ServletException</span> {
        <span style="color:#770088">this</span>.<span style="color:#000000">servletConfig</span> <span style="color:#981a1a">=</span> <span style="color:#000000">config</span>;
        <span style="color:#008855">String</span> <span style="color:#000000">servletName</span> <span style="color:#981a1a">=</span> <span style="color:#000000">config</span>.<span style="color:#000000">getServletName</span>();
        <span style="color:#000000">System</span>.<span style="color:#000000">out</span>.<span style="color:#000000">println</span>(<span style="color:#aa1111">"Servlet 实例的名称:"</span> <span style="color:#981a1a">+</span> <span style="color:#000000">servletName</span>);
        <span style="color:#aa5500">//获取Servlet中所有的初始化参数</span>
        <span style="color:#000000">Enumeration</span><span style="color:#981a1a"><</span><span style="color:#008855">String</span><span style="color:#981a1a">></span> <span style="color:#000000">initParameterNames</span> <span style="color:#981a1a">=</span> <span style="color:#000000">config</span>.<span style="color:#000000">getInitParameterNames</span>();
        <span style="color:#770088">while</span> (<span style="color:#000000">initParameterNames</span>.<span style="color:#000000">hasMoreElements</span>()){
            <span style="color:#008855">String</span> <span style="color:#000000">initParameterName</span> <span style="color:#981a1a">=</span> <span style="color:#000000">initParameterNames</span>.<span style="color:#000000">nextElement</span>();
            <span style="color:#aa5500">//获取Servlet中给定名称的初始化参数</span>
            <span style="color:#008855">String</span> <span style="color:#000000">initParameterValue</span> <span style="color:#981a1a">=</span> <span style="color:#000000">config</span>.<span style="color:#000000">getInitParameter</span>(<span style="color:#000000">initParameterName</span>);
            <span style="color:#000000">System</span>.<span style="color:#000000">out</span>.<span style="color:#000000">println</span>(<span style="color:#aa1111">"Servlet 初始化参数 "</span> <span style="color:#981a1a">+</span> <span style="color:#000000">initParameterName</span> <span style="color:#981a1a">+</span> <span style="color:#aa1111">":"</span> <span style="color:#981a1a">+</span> <span style="color:#000000">initParameterValue</span>);
        }
    }</span>
  • 编写 index.jsp

    <span style="background-color:#f8f8f8"><%<span style="color:#555555">@</span> <span style="color:#000000">page</span> <span style="color:#000000">contentType</span><span style="color:#981a1a">=</span><span style="color:#aa1111">"text/html;charset=UTF-8"</span> <span style="color:#000000">language</span><span style="color:#981a1a">=</span><span style="color:#aa1111">"java"</span> %>
    <span style="color:#555555"><!DOCTYPE HTML></span>
    <span style="color:#117700"><</span><span style="color:#117700">html</span><span style="color:#117700">></span>
      <span style="color:#117700"><</span><span style="color:#117700">head</span><span style="color:#117700">></span>
        <span style="color:#117700"><</span><span style="color:#117700">title</span><span style="color:#117700">></span>登录页面<span style="color:#117700"></</span><span style="color:#117700">title</span><span style="color:#117700">></span>
      <span style="color:#117700"></</span><span style="color:#117700">head</span><span style="color:#117700">></span>
      <span style="color:#117700"><</span><span style="color:#117700">body</span><span style="color:#117700">></span>
        <span style="color:#117700"><</span><span style="color:#117700">form</span> <span style="color:#0000cc">action</span>=<span style="color:#aa1111">"login"</span> <span style="color:#0000cc">method</span>=<span style="color:#aa1111">"post"</span><span style="color:#117700">></span>
          <span style="color:#117700"><</span><span style="color:#117700">div</span><span style="color:#117700">></span>
            <span style="color:#117700"><</span><span style="color:#117700">input</span> <span style="color:#0000cc">type</span>=<span style="color:#aa1111">"text"</span> <span style="color:#0000cc">name</span>=<span style="color:#aa1111">"username"</span><span style="color:#117700">></span>
          <span style="color:#117700"></</span><span style="color:#117700">div</span><span style="color:#117700">></span>
          <span style="color:#117700"><</span><span style="color:#117700">div</span><span style="color:#117700">></span>
            <span style="color:#117700"><</span><span style="color:#117700">input</span> <span style="color:#0000cc">type</span>=<span style="color:#aa1111">"password"</span> <span style="color:#0000cc">name</span>=<span style="color:#aa1111">"password"</span><span style="color:#117700">></span>
          <span style="color:#117700"></</span><span style="color:#117700">div</span><span style="color:#117700">></span>
          <span style="color:#117700"><</span><span style="color:#117700">div</span><span style="color:#117700">></span>
            <span style="color:#117700"><</span><span style="color:#117700">input</span> <span style="color:#0000cc">type</span>=<span style="color:#aa1111">"submit"</span> <span style="color:#0000cc">value</span>=<span style="color:#aa1111">"登录"</span><span style="color:#117700">></span>
          <span style="color:#117700"></</span><span style="color:#117700">div</span><span style="color:#117700">></span>
        <span style="color:#117700"></</span><span style="color:#117700">form</span><span style="color:#117700">></span>
      <span style="color:#117700"></</span><span style="color:#117700">body</span><span style="color:#117700">></span>
    <span style="color:#117700"></</span><span style="color:#117700">html</span><span style="color:#117700">></span></span>
  • 启动服务器,进行登录操作,查看控制台信息

     

控制台打印信息中并没有打印 "Servlet 销毁" 信息,由此可以得出:Servlet 处理完了请求后,并没有销毁。

  • 关闭服务器,查看控制台信息

     

    由此可以得出:在 Tomcat 关闭之前,Servlet 被销毁

  • 结论

    Servlet 在第一次接收请求时,由容器(如 Tomcat)创建实例,紧接着就由容器调用该 Servlet 的 init 方法完成初始化,然后由容器调用该 Servlet 的 service 方法进行请求处理,请求处理完成后,Servlet 并不会消亡, 而是跟随容器共存亡,在容器关闭之前,由容器调用 Servlet 的 destroy方法进行销毁

  • JSP 本质

     

    <span style="background-color:#f8f8f8"><span style="color:#770088">package</span> <span style="color:#0000ff">org</span>.<span style="color:#000000">apache</span>.<span style="color:#000000">jsp</span>;
    ​
    <span style="color:#770088">import</span> <span style="color:#000000">javax</span>.<span style="color:#000000">servlet</span>.<span style="color:#981a1a">*</span>;
    <span style="color:#770088">import</span> <span style="color:#000000">javax</span>.<span style="color:#000000">servlet</span>.<span style="color:#000000">http</span>.<span style="color:#981a1a">*</span>;
    <span style="color:#770088">import</span> <span style="color:#000000">javax</span>.<span style="color:#000000">servlet</span>.<span style="color:#000000">jsp</span>.<span style="color:#981a1a">*</span>;
    ​
    <span style="color:#770088">public</span> <span style="color:#770088">final</span> <span style="color:#770088">class</span> <span style="color:#0000ff">index_jsp</span> <span style="color:#770088">extends</span> <span style="color:#000000">org</span>.<span style="color:#000000">apache</span>.<span style="color:#000000">jasper</span>.<span style="color:#000000">runtime</span>.<span style="color:#000000">HttpJspBase</span>
        <span style="color:#770088">implements</span> <span style="color:#000000">org</span>.<span style="color:#000000">apache</span>.<span style="color:#000000">jasper</span>.<span style="color:#000000">runtime</span>.<span style="color:#000000">JspSourceDependent</span>,
                     <span style="color:#000000">org</span>.<span style="color:#000000">apache</span>.<span style="color:#000000">jasper</span>.<span style="color:#000000">runtime</span>.<span style="color:#000000">JspSourceImports</span> {
    }</span>

     

由此可以得出:JSP 的本质就是Servlet,只是 JSP 注重的是页面内容的展示,而Servlet注重的是业务逻辑的处理。

3. 请求处理与响应

3.1 体系结构

 

3.2 请求接口

  • ServletRequest 接口常用方法

    <span style="background-color:#f8f8f8"><span style="color:#aa5500">//从请求中获取给定属性名对应的属性值</span>
    <span style="color:#008855">Object</span> <span style="color:#0000ff">getAttribute</span>(<span style="color:#008855">String</span> <span style="color:#000000">attributeName</span>);
    <span style="color:#aa5500">//将给定的属性值以给定的属性名存储在请求中</span>
    <span style="color:#008855">void</span> <span style="color:#0000ff">setAttribute</span>(<span style="color:#008855">String</span> <span style="color:#000000">attributeName</span>, <span style="color:#008855">Object</span> <span style="color:#000000">attributeVaue</span>);
    <span style="color:#aa5500">//从请求中将给定的属性名移除</span>
    <span style="color:#008855">void</span> <span style="color:#0000ff">removeAttribute</span>(<span style="color:#008855">String</span> <span style="color:#000000">attributeName</span>);
    <span style="color:#aa5500">//获取请求中存储的所有属性名</span>
    <span style="color:#000000">Enumeration</span><span style="color:#981a1a"><</span><span style="color:#008855">String</span><span style="color:#981a1a">></span> <span style="color:#0000ff">getAttributeNames</span>();
    <span style="color:#aa5500">//从请求中获取给定参数名对应的参数值(参数值是单个数据)</span>
    <span style="color:#008855">String</span> <span style="color:#0000ff">getParameter</span>(<span style="color:#008855">String</span> <span style="color:#000000">parameterName</span>);
    <span style="color:#aa5500">//从请求中获取给定参数名对应的参数值(参数值是多个数据)</span>
    <span style="color:#008855">String</span>[] <span style="color:#0000ff">getParameterValues</span>(<span style="color:#008855">String</span> <span style="color:#000000">parameterName</span>);
    <span style="color:#aa5500">//从请求中获取所有的参数名</span>
    <span style="color:#000000">Enumeration</span><span style="color:#981a1a"><</span><span style="color:#008855">String</span><span style="color:#981a1a">></span> <span style="color:#0000ff">getParameterNames</span>();
    <span style="color:#aa5500">//从请求中获取所有的参数名和参数值形成的映射</span>
    <span style="color:#000000">Map</span><span style="color:#981a1a"><</span><span style="color:#008855">String</span>, <span style="color:#008855">String</span>[]<span style="color:#981a1a">></span> <span style="color:#0000ff">getParameterMap</span>();
    <span style="color:#aa5500">//从请求中获取字符集编码</span>
    <span style="color:#008855">String</span> <span style="color:#0000ff">getCharacterEncoding</span>();
    <span style="color:#aa5500">//设置请求的字符集编码</span>
    <span style="color:#008855">void</span> <span style="color:#0000ff">setCharacterEncoding</span>(<span style="color:#008855">String</span> <span style="color:#000000">charset</span>) <span style="color:#770088">throws</span> <span style="color:#000000">UnsupportedEncodingException</span>;
    <span style="color:#aa5500">//从请求中获取字符流,该字符流只能读取请求体中的数据信息,与下面的 getInputStream 方法只能二选一</span>
    <span style="color:#000000">BufferedReader</span> <span style="color:#0000ff">getReader</span>() <span style="color:#770088">throws</span> <span style="color:#000000">IOException</span>;
    <span style="color:#aa5500">//从请求中获取字节流,该字节流只能读取请求体中的数据信息</span>
    <span style="color:#000000">ServletInputStream</span> <span style="color:#0000ff">getInputStream</span>() <span style="color:#770088">throws</span> <span style="color:#000000">IOException</span>;
    <span style="color:#aa5500">//从请求中获取当前Servlet所在的上下文对象</span>
    <span style="color:#000000">ServletContext</span> <span style="color:#0000ff">getServletContext</span>();
    <span style="color:#aa5500">//从请求中获取请求转发的对象</span>
    <span style="color:#000000">RequestDispatcher</span> <span style="color:#0000ff">getRequestDispatcher</span>(<span style="color:#008855">String</span> <span style="color:#000000">path</span>);</span>
  • 用法

    <span style="background-color:#f8f8f8"><span style="color:#555555">@Override</span>
    <span style="color:#770088">public</span> <span style="color:#008855">void</span> <span style="color:#0000ff">service</span>(<span style="color:#000000">ServletRequest</span> <span style="color:#000000">servletRequest</span>, <span style="color:#000000">ServletResponse</span> <span style="color:#000000">servletResponse</span>) <span style="color:#770088">throws</span> <span style="color:#000000">ServletException</span>, <span style="color:#000000">IOException</span> {
        <span style="color:#000000">System</span>.<span style="color:#000000">out</span>.<span style="color:#000000">println</span>(<span style="color:#aa1111">"请求的字符集编码需要在读取请求信息之前进行设置,否则,设置的字符集编码格式将不生效"</span>);
        <span style="color:#000000">System</span>.<span style="color:#000000">out</span>.<span style="color:#000000">println</span>(<span style="color:#aa1111">"请求字符集编码:"</span> <span style="color:#981a1a">+</span> <span style="color:#000000">servletRequest</span>.<span style="color:#000000">getCharacterEncoding</span>());
        <span style="color:#000000">servletRequest</span>.<span style="color:#000000">setCharacterEncoding</span>(<span style="color:#aa1111">"UTF-8"</span>);
        <span style="color:#000000">System</span>.<span style="color:#000000">out</span>.<span style="color:#000000">println</span>(<span style="color:#aa1111">"请求字符集编码:"</span> <span style="color:#981a1a">+</span> <span style="color:#000000">servletRequest</span>.<span style="color:#000000">getCharacterEncoding</span>());
    ​
        <span style="color:#000000">BufferedReader</span> <span style="color:#000000">reader</span> <span style="color:#981a1a">=</span> <span style="color:#000000">servletRequest</span>.<span style="color:#000000">getReader</span>();
        <span style="color:#000000">System</span>.<span style="color:#000000">out</span>.<span style="color:#000000">println</span>(<span style="color:#aa1111">"开始读取请求参数信息"</span>);
        <span style="color:#008855">String</span> <span style="color:#000000">line</span>;
        <span style="color:#770088">while</span> ((<span style="color:#000000">line</span> <span style="color:#981a1a">=</span> <span style="color:#000000">reader</span>.<span style="color:#000000">readLine</span>()) <span style="color:#981a1a">!=</span> <span style="color:#221199">null</span>){
            <span style="color:#000000">System</span>.<span style="color:#000000">out</span>.<span style="color:#000000">println</span>(<span style="color:#000000">line</span>);
        }
        <span style="color:#000000">System</span>.<span style="color:#000000">out</span>.<span style="color:#000000">println</span>(<span style="color:#aa1111">"请求参数信息读取完毕"</span>);
        <span style="color:#000000">System</span>.<span style="color:#000000">out</span>.<span style="color:#000000">println</span>(<span style="color:#aa1111">"================"</span>);
        <span style="color:#000000">System</span>.<span style="color:#000000">out</span>.<span style="color:#000000">println</span>(<span style="color:#aa1111">"开始获取请求中存储的数据"</span>);
        <span style="color:#000000">Enumeration</span><span style="color:#981a1a"><</span><span style="color:#008855">String</span><span style="color:#981a1a">></span> <span style="color:#000000">attributeNames</span> <span style="color:#981a1a">=</span> <span style="color:#000000">servletRequest</span>.<span style="color:#000000">getAttributeNames</span>();
        <span style="color:#770088">while</span> (<span style="color:#000000">attributeNames</span>.<span style="color:#000000">hasMoreElements</span>()){
            <span style="color:#008855">String</span> <span style="color:#000000">attributeName</span> <span style="color:#981a1a">=</span> <span style="color:#000000">attributeNames</span>.<span style="color:#000000">nextElement</span>();
            <span style="color:#008855">Object</span> <span style="color:#000000">attributeValue</span> <span style="color:#981a1a">=</span> <span style="color:#000000">servletRequest</span>.<span style="color:#000000">getAttribute</span>(<span style="color:#000000">attributeName</span>);
            <span style="color:#000000">System</span>.<span style="color:#000000">out</span>.<span style="color:#000000">println</span>(<span style="color:#000000">attributeName</span> <span style="color:#981a1a">+</span> <span style="color:#aa1111">" => "</span> <span style="color:#981a1a">+</span> <span style="color:#000000">attributeValue</span>);
        }
        <span style="color:#000000">System</span>.<span style="color:#000000">out</span>.<span style="color:#000000">println</span>(<span style="color:#aa1111">"请求中存储的数据获取完毕"</span>);
        <span style="color:#000000">System</span>.<span style="color:#000000">out</span>.<span style="color:#000000">println</span>(<span style="color:#aa1111">"================"</span>);
        <span style="color:#000000">System</span>.<span style="color:#000000">out</span>.<span style="color:#000000">println</span>(<span style="color:#aa1111">"方式一:开始获取请求参数信息"</span>);
        <span style="color:#000000">Enumeration</span><span style="color:#981a1a"><</span><span style="color:#008855">String</span><span style="color:#981a1a">></span> <span style="color:#000000">parameterNames</span> <span style="color:#981a1a">=</span> <span style="color:#000000">servletRequest</span>.<span style="color:#000000">getParameterNames</span>();
        <span style="color:#770088">while</span> (<span style="color:#000000">parameterNames</span>.<span style="color:#000000">hasMoreElements</span>()){
            <span style="color:#008855">String</span> <span style="color:#000000">parameterName</span> <span style="color:#981a1a">=</span> <span style="color:#000000">parameterNames</span>.<span style="color:#000000">nextElement</span>();
            <span style="color:#008855">String</span> <span style="color:#000000">parameterValue</span> <span style="color:#981a1a">=</span> <span style="color:#000000">servletRequest</span>.<span style="color:#000000">getParameter</span>(<span style="color:#000000">parameterName</span>);
            <span style="color:#000000">System</span>.<span style="color:#000000">out</span>.<span style="color:#000000">println</span>(<span style="color:#000000">parameterName</span> <span style="color:#981a1a">+</span> <span style="color:#aa1111">" => "</span> <span style="color:#981a1a">+</span> <span style="color:#000000">parameterValue</span>);
        }
        <span style="color:#000000">System</span>.<span style="color:#000000">out</span>.<span style="color:#000000">println</span>(<span style="color:#aa1111">"方式一:请求参数信息获取完毕"</span>);
        <span style="color:#000000">System</span>.<span style="color:#000000">out</span>.<span style="color:#000000">println</span>(<span style="color:#aa1111">"================"</span>);
        <span style="color:#000000">System</span>.<span style="color:#000000">out</span>.<span style="color:#000000">println</span>(<span style="color:#aa1111">"方式二:开始获取请求参数信息"</span>);
        <span style="color:#000000">Map</span><span style="color:#981a1a"><</span><span style="color:#008855">String</span>, <span style="color:#008855">String</span>[]<span style="color:#981a1a">></span> <span style="color:#000000">parameterMap</span> <span style="color:#981a1a">=</span> <span style="color:#000000">servletRequest</span>.<span style="color:#000000">getParameterMap</span>();
        <span style="color:#000000">parameterMap</span>.<span style="color:#000000">forEach</span>((<span style="color:#000000">k</span>, <span style="color:#000000">values</span>) <span style="color:#981a1a">-></span> <span style="color:#000000">System</span>.<span style="color:#000000">out</span>.<span style="color:#000000">println</span>(<span style="color:#000000">k</span> <span style="color:#981a1a">+</span> <span style="color:#aa1111">" => "</span> <span style="color:#981a1a">+</span> <span style="color:#000000">Arrays</span>.<span style="color:#000000">toString</span>(<span style="color:#000000">values</span>)));
        <span style="color:#000000">System</span>.<span style="color:#000000">out</span>.<span style="color:#000000">println</span>(<span style="color:#aa1111">"方式二:请求参数信息获取完毕"</span>);
        <span style="color:#000000">System</span>.<span style="color:#000000">out</span>.<span style="color:#000000">println</span>(<span style="color:#aa1111">"请求所使用的上下文路径:"</span> <span style="color:#981a1a">+</span> <span style="color:#000000">servletRequest</span>.<span style="color:#000000">getServletContext</span>().<span style="color:#000000">getContextPath</span>());
    }</span>
  • POST请求测试

     

  • GET 请求测试

     

  • 结论

    使用GET方式发送的请求,只能通过getParameter 方法获取;使用POST方式发送的请求,只能使用流来获取。这是因为使用GET方式发送的请求,参数在URL地址中,解析这些参数的时候将其存放在一个Map集合中,因此可以直接获取。而POS方式发送的请求,参数在请求体中,这部分内容只能通过流来读取,然后再进行处理。

3.3 响应接口

  • ServletResponse 接口常用方法

    <span style="background-color:#f8f8f8"><span style="color:#aa5500">//获取响应的字符集编码</span>
    <span style="color:#008855">String</span> <span style="color:#0000ff">getCharacterEncoding</span>();
    <span style="color:#aa5500">//设置响应的字符集编码</span>
    <span style="color:#008855">void</span> <span style="color:#0000ff">setCharacterEncoding</span>(<span style="color:#008855">String</span> <span style="color:#000000">charset</span>);
    <span style="color:#aa5500">//获取响应的内容类型</span>
    <span style="color:#008855">String</span> <span style="color:#0000ff">getContentType</span>();
    <span style="color:#aa5500">//设置响应的内容类型</span>
    <span style="color:#008855">void</span> <span style="color:#0000ff">setContentType</span>(<span style="color:#008855">String</span> <span style="color:#000000">contentType</span>);
    <span style="color:#aa5500">//获取输出流,主要用于下载文件</span>
    <span style="color:#000000">ServletOutputStream</span> <span style="color:#0000ff">getOutputStream</span>() <span style="color:#770088">throws</span> <span style="color:#000000">IOException</span>;
    <span style="color:#aa5500">//获取打印流,主要用于向页面传输信息</span>
    <span style="color:#000000">PrintWriter</span> <span style="color:#0000ff">getWriter</span>() <span style="color:#770088">throws</span> <span style="color:#000000">IOException</span>;</span>
  • 用法(在 service 方法最后追加如下代码)

    <span style="background-color:#f8f8f8"><span style="color:#000000">System</span>.<span style="color:#000000">out</span>.<span style="color:#000000">println</span>();
    <span style="color:#000000">System</span>.<span style="color:#000000">out</span>.<span style="color:#000000">println</span>();
    <span style="color:#000000">System</span>.<span style="color:#000000">out</span>.<span style="color:#000000">println</span>(<span style="color:#aa1111">"==========================================="</span>);
    <span style="color:#000000">System</span>.<span style="color:#000000">out</span>.<span style="color:#000000">println</span>(<span style="color:#aa1111">"响应的字符集编码:"</span> <span style="color:#981a1a">+</span> <span style="color:#000000">servletResponse</span>.<span style="color:#000000">getCharacterEncoding</span>());
    <span style="color:#000000">servletResponse</span>.<span style="color:#000000">setCharacterEncoding</span>(<span style="color:#aa1111">"UTF-8"</span>);
    <span style="color:#000000">System</span>.<span style="color:#000000">out</span>.<span style="color:#000000">println</span>(<span style="color:#aa1111">"响应的字符集编码:"</span> <span style="color:#981a1a">+</span> <span style="color:#000000">servletResponse</span>.<span style="color:#000000">getCharacterEncoding</span>());
    <span style="color:#000000">System</span>.<span style="color:#000000">out</span>.<span style="color:#000000">println</span>(<span style="color:#aa1111">"响应的内容类型:"</span> <span style="color:#981a1a">+</span> <span style="color:#000000">servletResponse</span>.<span style="color:#000000">getContentType</span>());
    <span style="color:#000000">servletResponse</span>.<span style="color:#000000">setContentType</span>(<span style="color:#aa1111">"text/html;charset=utf-8"</span>);
    <span style="color:#000000">System</span>.<span style="color:#000000">out</span>.<span style="color:#000000">println</span>(<span style="color:#aa1111">"响应的内容类型:"</span> <span style="color:#981a1a">+</span> <span style="color:#000000">servletResponse</span>.<span style="color:#000000">getContentType</span>());
    <span style="color:#000000">PrintWriter</span> <span style="color:#000000">writer</span> <span style="color:#981a1a">=</span> <span style="color:#000000">servletResponse</span>.<span style="color:#000000">getWriter</span>();
    <span style="color:#000000">writer</span>.<span style="color:#000000">print</span>(<span style="color:#aa1111">"登录请求已处理"</span>);
    <span style="color:#000000">writer</span>.<span style="color:#000000">flush</span>();
    <span style="color:#000000">writer</span>.<span style="color:#000000">close</span>();</span>
  • 测试

     

3.4 HTTP 请求和响应

  • HttpServletRequest 接口常用方法

    <span style="background-color:#f8f8f8"><span style="color:#aa5500">//从请求中获取Cookie信息</span>
    <span style="color:#000000">Cookie</span>[] <span style="color:#0000ff">getCookies</span>();
    <span style="color:#aa5500">//从请求中获取给定请求头名称对应的属性值</span>
    <span style="color:#008855">String</span> <span style="color:#0000ff">getHeader</span>(<span style="color:#008855">String</span> <span style="color:#000000">headerName</span>);
    <span style="color:#aa5500">//从请求中获取所有的请求头名称</span>
    <span style="color:#000000">Enumeration</span><span style="color:#981a1a"><</span><span style="color:#008855">String</span><span style="color:#981a1a">></span> <span style="color:#0000ff">getHeaderNames</span>();
    <span style="color:#aa5500">//获取请求的方式:GET、POST、PUT、DELETE等</span>
    <span style="color:#008855">String</span> <span style="color:#0000ff">getMethod</span>();
    <span style="color:#aa5500">//从请求中获取上下文路径</span>
    <span style="color:#008855">String</span> <span style="color:#0000ff">getContextPath</span>();
    <span style="color:#aa5500">//从请求中获取session</span>
    <span style="color:#000000">HttpSession</span> <span style="color:#0000ff">getSession</span>();
    <span style="color:#aa5500">//获取请求地址</span>
    <span style="color:#008855">String</span> <span style="color:#0000ff">getRequestURI</span>();</span>
  • HttpServletResponse 接口常用方法

    <span style="background-color:#f8f8f8"><span style="color:#aa5500">//添加客户端存储的Cookie信息</span>
    <span style="color:#008855">void</span> <span style="color:#0000ff">addCookie</span>(<span style="color:#000000">Cookie</span> <span style="color:#000000">cookie</span>);
    <span style="color:#aa5500">//返回错误状态及错误信息</span>
    <span style="color:#008855">void</span> <span style="color:#0000ff">sendError</span>(<span style="color:#008855">int</span> <span style="color:#000000">status</span>, <span style="color:#008855">String</span> <span style="color:#000000">errorMsg</span>) <span style="color:#770088">throws</span> <span style="color:#000000">IOException</span>;
    <span style="color:#aa5500">//返回错误状态</span>
    <span style="color:#008855">void</span> <span style="color:#0000ff">sendError</span>(<span style="color:#008855">int</span> <span style="color:#000000">status</span>) <span style="color:#770088">throws</span> <span style="color:#000000">IOException</span>;
    <span style="color:#aa5500">//重定向至新的资源</span>
    <span style="color:#008855">void</span> <span style="color:#0000ff">sendRedirect</span>(<span style="color:#008855">String</span> <span style="color:#000000">redirectURL</span>) <span style="color:#770088">throws</span> <span style="color:#000000">IOException</span>;
    <span style="color:#aa5500">//设置响应头信息</span>
    <span style="color:#008855">void</span> <span style="color:#0000ff">setHeader</span>(<span style="color:#008855">String</span> <span style="color:#000000">headerName</span>, <span style="color:#008855">String</span> <span style="color:#000000">headerValue</span>);
    <span style="color:#aa5500">//添加响应头信息</span>
    <span style="color:#008855">void</span> <span style="color:#0000ff">addHeader</span>(<span style="color:#008855">String</span> <span style="color:#000000">headerName</span>, <span style="color:#008855">String</span> <span style="color:#000000">headerValue</span>);
    <span style="color:#aa5500">//设置响应状态</span>
    <span style="color:#008855">void</span> <span style="color:#0000ff">setStatus</span>(<span style="color:#008855">int</span> <span style="color:#000000">status</span>);</span>
  • HttpServlet常用方法(支持 HTTP 协议的 Servlet)

    <span style="background-color:#f8f8f8"><span style="color:#aa5500">//对父类抽象方法的实现,该方法是对HTTP协议的交互信息的实现,调用的是下面的 service 方法</span>
    <span style="color:#008855">void</span> <span style="color:#0000ff">service</span>(<span style="color:#000000">ServletRequest</span> <span style="color:#000000">req</span>,<span style="color:#000000">ServletResponse</span> <span style="color:#000000">res</span>);
    <span style="color:#aa5500">//HTTP协议的交互信息的实现,该方法主要针对不同的请求方式进行处理。GET请求会调用 doGet 方法处理,</span>
    <span style="color:#aa5500">//POST请求会调用 doPost 处理, PUT请求会调用 doPut 方法处理, DELETE请求会调用 doDelete 方法处理 </span>
    <span style="color:#008855">void</span> <span style="color:#0000ff">service</span>(<span style="color:#000000">HttpServletRequest</span>  <span style="color:#000000">req</span>, <span style="color:#000000">HttpServletResponseres</span>);
    <span style="color:#aa5500">//GET请求处理</span>
    <span style="color:#008855">void</span> <span style="color:#0000ff">doGet</span>(<span style="color:#000000">HttpServletRequestreq</span>,<span style="color:#000000">HttpServletResponse</span> <span style="color:#000000">res</span>);
    <span style="color:#aa5500">//POST请求处理</span>
    <span style="color:#008855">void</span> <span style="color:#0000ff">doPost</span>(<span style="color:#000000">HttpServletRequestreq</span>,<span style="color:#000000">HttpServletResponse</span> <span style="color:#000000">res</span>);
    <span style="color:#aa5500">//PUT请求处理</span>
    <span style="color:#008855">void</span> <span style="color:#0000ff">doPut</span>(<span style="color:#000000">HttpServletRequestreq</span>,<span style="color:#000000">HttpServletResponse</span> <span style="color:#000000">res</span>);
    <span style="color:#aa5500">//DELETE请求处理</span>
    <span style="color:#008855">void</span> <span style="color:#0000ff">doDelete</span>(<span style="color:#000000">HttpServletRequestreq</span>,<span style="color:#000000">HttpServletResponse</span> <span style="color:#000000">res</span>);</span>
  • 用法

    <span style="background-color:#f8f8f8"><span style="color:#770088">import</span> <span style="color:#000000">javax</span>.<span style="color:#000000">servlet</span>.<span style="color:#000000">ServletException</span>;
    <span style="color:#770088">import</span> <span style="color:#000000">javax</span>.<span style="color:#000000">servlet</span>.<span style="color:#000000">http</span>.<span style="color:#000000">HttpServlet</span>;
    <span style="color:#770088">import</span> <span style="color:#000000">javax</span>.<span style="color:#000000">servlet</span>.<span style="color:#000000">http</span>.<span style="color:#000000">HttpServletRequest</span>;
    <span style="color:#770088">import</span> <span style="color:#000000">javax</span>.<span style="color:#000000">servlet</span>.<span style="color:#000000">http</span>.<span style="color:#000000">HttpServletResponse</span>;
    <span style="color:#770088">import</span> <span style="color:#000000">java</span>.<span style="color:#000000">io</span>.<span style="color:#000000">IOException</span>;
    <span style="color:#770088">import</span> <span style="color:#000000">java</span>.<span style="color:#000000">io</span>.<span style="color:#000000">PrintWriter</span>;
    <span style="color:#770088">import</span> <span style="color:#000000">java</span>.<span style="color:#000000">util</span>.<span style="color:#000000">Arrays</span>;
    <span style="color:#770088">import</span> <span style="color:#000000">java</span>.<span style="color:#000000">util</span>.<span style="color:#000000">Enumeration</span>;
    <span style="color:#770088">import</span> <span style="color:#000000">java</span>.<span style="color:#000000">util</span>.<span style="color:#000000">Map</span>;
    ​
    <span style="color:#770088">public</span> <span style="color:#770088">class</span> <span style="color:#0000ff">RegisterServlet</span> <span style="color:#770088">extends</span> <span style="color:#000000">HttpServlet</span> {
    ​
        <span style="color:#555555">@Override</span>
        <span style="color:#770088">protected</span> <span style="color:#008855">void</span> <span style="color:#000000">doPost</span>(<span style="color:#000000">HttpServletRequest</span> <span style="color:#000000">req</span>, <span style="color:#000000">HttpServletResponse</span> <span style="color:#000000">resp</span>) <span style="color:#770088">throws</span> <span style="color:#000000">ServletException</span>, <span style="color:#000000">IOException</span> {
            <span style="color:#000000">System</span>.<span style="color:#000000">out</span>.<span style="color:#000000">println</span>(<span style="color:#aa1111">"开始获取请求头信息"</span>);
            <span style="color:#000000">Enumeration</span><span style="color:#981a1a"><</span><span style="color:#008855">String</span><span style="color:#981a1a">></span> <span style="color:#000000">headerNames</span> <span style="color:#981a1a">=</span> <span style="color:#000000">req</span>.<span style="color:#000000">getHeaderNames</span>();
            <span style="color:#770088">while</span> (<span style="color:#000000">headerNames</span>.<span style="color:#000000">hasMoreElements</span>()){
                <span style="color:#008855">String</span> <span style="color:#000000">headerName</span> <span style="color:#981a1a">=</span> <span style="color:#000000">headerNames</span>.<span style="color:#000000">nextElement</span>();
                <span style="color:#008855">String</span> <span style="color:#000000">headerValue</span> <span style="color:#981a1a">=</span> <span style="color:#000000">req</span>.<span style="color:#000000">getHeader</span>(<span style="color:#000000">headerName</span>);
                <span style="color:#000000">System</span>.<span style="color:#000000">out</span>.<span style="color:#000000">println</span>(<span style="color:#000000">headerName</span> <span style="color:#981a1a">+</span> <span style="color:#aa1111">" => "</span> <span style="color:#981a1a">+</span> <span style="color:#000000">headerValue</span>);
            }
            <span style="color:#000000">System</span>.<span style="color:#000000">out</span>.<span style="color:#000000">println</span>(<span style="color:#aa1111">"请求头信息获取完毕"</span>);
            <span style="color:#000000">System</span>.<span style="color:#000000">out</span>.<span style="color:#000000">println</span>(<span style="color:#aa1111">"请求方式:"</span> <span style="color:#981a1a">+</span> <span style="color:#000000">req</span>.<span style="color:#000000">getMethod</span>());
            <span style="color:#000000">System</span>.<span style="color:#000000">out</span>.<span style="color:#000000">println</span>(<span style="color:#aa1111">"请求地址:"</span> <span style="color:#981a1a">+</span> <span style="color:#000000">req</span>.<span style="color:#000000">getRequestURI</span>());
            <span style="color:#000000">System</span>.<span style="color:#000000">out</span>.<span style="color:#000000">println</span>(<span style="color:#aa1111">"请求的上下文路径:"</span> <span style="color:#981a1a">+</span> <span style="color:#000000">req</span>.<span style="color:#000000">getContextPath</span>());
            <span style="color:#000000">System</span>.<span style="color:#000000">out</span>.<span style="color:#000000">println</span>(<span style="color:#aa1111">"=================="</span>);
            <span style="color:#000000">System</span>.<span style="color:#000000">out</span>.<span style="color:#000000">println</span>(<span style="color:#aa1111">"开始读取请求参数"</span>);
            <span style="color:#000000">Map</span><span style="color:#981a1a"><</span><span style="color:#008855">String</span>, <span style="color:#008855">String</span>[]<span style="color:#981a1a">></span> <span style="color:#000000">parameterMap</span> <span style="color:#981a1a">=</span> <span style="color:#000000">req</span>.<span style="color:#000000">getParameterMap</span>();
            <span style="color:#000000">parameterMap</span>.<span style="color:#000000">forEach</span>((<span style="color:#000000">k</span>, <span style="color:#000000">values</span>) <span style="color:#981a1a">-></span> <span style="color:#000000">System</span>.<span style="color:#000000">out</span>.<span style="color:#000000">println</span>(<span style="color:#000000">k</span> <span style="color:#981a1a">+</span> <span style="color:#aa1111">"=>"</span> <span style="color:#981a1a">+</span> <span style="color:#000000">Arrays</span>.<span style="color:#000000">toString</span>(<span style="color:#000000">values</span>)));
            <span style="color:#000000">System</span>.<span style="color:#000000">out</span>.<span style="color:#000000">println</span>(<span style="color:#aa1111">"请求参数读取完毕"</span>);
            <span style="color:#000000">System</span>.<span style="color:#000000">out</span>.<span style="color:#000000">println</span>();
            <span style="color:#000000">System</span>.<span style="color:#000000">out</span>.<span style="color:#000000">println</span>();
            <span style="color:#000000">System</span>.<span style="color:#000000">out</span>.<span style="color:#000000">println</span>(<span style="color:#aa1111">"========================="</span>);
            <span style="color:#000000">System</span>.<span style="color:#000000">out</span>.<span style="color:#000000">println</span>(<span style="color:#aa1111">"响应开始"</span>);
            <span style="color:#000000">resp</span>.<span style="color:#000000">setContentType</span>(<span style="color:#aa1111">"text/html;charset=utf-8"</span>);
            <span style="color:#000000">PrintWriter</span> <span style="color:#000000">writer</span> <span style="color:#981a1a">=</span> <span style="color:#000000">resp</span>.<span style="color:#000000">getWriter</span>();
            <span style="color:#000000">writer</span>.<span style="color:#000000">print</span>(<span style="color:#aa1111">"注册请求已经处理"</span>);
            <span style="color:#000000">writer</span>.<span style="color:#000000">flush</span>();
            <span style="color:#000000">writer</span>.<span style="color:#000000">close</span>();
        }
    }</span>
    <span style="background-color:#f8f8f8"><span style="color:#aa5500"><!-- register.jsp --></span>
    <%<span style="color:#555555">@</span> <span style="color:#000000">page</span> <span style="color:#000000">contentType</span><span style="color:#981a1a">=</span><span style="color:#aa1111">"text/html;charset=UTF-8"</span> <span style="color:#000000">language</span><span style="color:#981a1a">=</span><span style="color:#aa1111">"java"</span> %>
    <span style="color:#555555"><!DOCTYPE HTML></span>
    <span style="color:#117700"><</span><span style="color:#117700">html</span><span style="color:#117700">></span>
      <span style="color:#117700"><</span><span style="color:#117700">head</span><span style="color:#117700">></span>
        <span style="color:#117700"><</span><span style="color:#117700">title</span><span style="color:#117700">></span>注册页面<span style="color:#117700"></</span><span style="color:#117700">title</span><span style="color:#117700">></span>
      <span style="color:#117700"></</span><span style="color:#117700">head</span><span style="color:#117700">></span>
      <span style="color:#117700"><</span><span style="color:#117700">body</span><span style="color:#117700">></span>
        <span style="color:#117700"><</span><span style="color:#117700">form</span> <span style="color:#0000cc">action</span>=<span style="color:#aa1111">"createUser"</span> <span style="color:#0000cc">method</span>=<span style="color:#aa1111">"post"</span><span style="color:#117700">></span>
          <span style="color:#117700"><</span><span style="color:#117700">div</span><span style="color:#117700">></span>
            <span style="color:#117700"><</span><span style="color:#117700">input</span> <span style="color:#0000cc">type</span>=<span style="color:#aa1111">"text"</span> <span style="color:#0000cc">name</span>=<span style="color:#aa1111">"username"</span><span style="color:#117700">></span>
          <span style="color:#117700"></</span><span style="color:#117700">div</span><span style="color:#117700">></span>
          <span style="color:#117700"><</span><span style="color:#117700">div</span><span style="color:#117700">></span>
            <span style="color:#117700"><</span><span style="color:#117700">input</span> <span style="color:#0000cc">type</span>=<span style="color:#aa1111">"password"</span> <span style="color:#0000cc">name</span>=<span style="color:#aa1111">"password"</span><span style="color:#117700">></span>
          <span style="color:#117700"></</span><span style="color:#117700">div</span><span style="color:#117700">></span>
          <span style="color:#117700"><</span><span style="color:#117700">div</span><span style="color:#117700">></span>
            <span style="color:#117700"><</span><span style="color:#117700">input</span> <span style="color:#0000cc">type</span>=<span style="color:#aa1111">"submit"</span> <span style="color:#0000cc">value</span>=<span style="color:#aa1111">"注册"</span><span style="color:#117700">></span>
          <span style="color:#117700"></</span><span style="color:#117700">div</span><span style="color:#117700">></span>
        <span style="color:#117700"></</span><span style="color:#117700">form</span><span style="color:#117700">></span>
      <span style="color:#117700"></</span><span style="color:#117700">body</span><span style="color:#117700">></span>
    <span style="color:#117700"></</span><span style="color:#117700">html</span><span style="color:#117700">></span></span>
    <span style="background-color:#f8f8f8"><span style="color:#aa5500"><!-- web.xml 中添加如下配置 --></span>
    <span style="color:#117700"><</span><span style="color:#117700">servlet</span><span style="color:#117700">></span>
        <span style="color:#117700"><</span><span style="color:#117700">servlet-name</span><span style="color:#117700">></span>registerServlet<span style="color:#117700"></</span><span style="color:#117700">servlet-name</span><span style="color:#117700">></span>
        <span style="color:#117700"><</span><span style="color:#117700">servlet-class</span><span style="color:#117700">></span>com.qf.jsp.servlet.RegisterServlet<span style="color:#117700"></</span><span style="color:#117700">servlet-class</span><span style="color:#117700">></span>
    <span style="color:#117700"></</span><span style="color:#117700">servlet</span><span style="color:#117700">></span>
    ​
    <span style="color:#117700"><</span><span style="color:#117700">servlet-mapping</span><span style="color:#117700">></span>
        <span style="color:#117700"><</span><span style="color:#117700">servlet-name</span><span style="color:#117700">></span>registerServlet<span style="color:#117700"></</span><span style="color:#117700">servlet-name</span><span style="color:#117700">></span>
        <span style="color:#117700"><</span><span style="color:#117700">url-pattern</span><span style="color:#117700">></span>/createUser<span style="color:#117700"></</span><span style="color:#117700">url-pattern</span><span style="color:#117700">></span>
    <span style="color:#117700"></</span><span style="color:#117700">servlet-mapping</span><span style="color:#117700">></span></span>
  • 测试

    查看控制台信息

3.5 Servlet 交互流程

 

4. ServletContext

4.1 常用方法

<span style="background-color:#f8f8f8"><span style="color:#333333"><span style="color:#aa5500">//获取上下文路径</span>
<span style="color:#008855">String</span> <span style="color:#0000ff">getContextPath</span>();
<span style="color:#aa5500">//获取给定相对路径对应的绝对路径</span>
<span style="color:#008855">String</span> <span style="color:#0000ff">getRealPath</span>(<span style="color:#008855">String</span> <span style="color:#000000">path</span>);
<span style="color:#aa5500">//获取上下文初始化参数中给定参数名对应的参数值</span>
<span style="color:#008855">String</span> <span style="color:#0000ff">getInitParameter</span>(<span style="color:#008855">String</span> <span style="color:#000000">parameterName</span>);
<span style="color:#aa5500">//获取上下文初始化参数中所有的参数名</span>
<span style="color:#000000">Enumeration</span><span style="color:#981a1a"><</span><span style="color:#008855">String</span><span style="color:#981a1a">></span> <span style="color:#0000ff">getInitParameterNames</span>();
<span style="color:#aa5500">//获取上下文存储的数据中给定属性名对应的属性值</span>
<span style="color:#008855">Object</span> <span style="color:#0000ff">getAttribute</span>(<span style="color:#008855">String</span> <span style="color:#000000">attributeName</span>);
<span style="color:#aa5500">//获取上下文存储的数据中所有的属性名</span>
<span style="color:#000000">Enumeration</span><span style="color:#981a1a"><</span><span style="color:#008855">String</span><span style="color:#981a1a">></span> <span style="color:#0000ff">getAttributeNames</span>();
<span style="color:#aa5500">//将给定的属性值使用给定的属性名存储在上下文中</span>
<span style="color:#008855">void</span> <span style="color:#0000ff">setAttribute</span>(<span style="color:#008855">String</span> <span style="color:#000000">attributeName</span>, <span style="color:#008855">Object</span> <span style="color:#000000">attributeValue</span>);
<span style="color:#aa5500">//从上下文存储的数据中将给定的属性名移出</span>
<span style="color:#008855">void</span> <span style="color:#0000ff">removeAttribute</span>(<span style="color:#008855">String</span> <span style="color:#000000">attributeName</span>);</span></span>

4.2 用法

  • 配置 web.xml

    <span style="background-color:#f8f8f8"><span style="color:#117700"><</span><span style="color:#117700">context-param</span><span style="color:#117700">></span>
        <span style="color:#117700"><</span><span style="color:#117700">param-name</span><span style="color:#117700">></span>characterEncoding<span style="color:#117700"></</span><span style="color:#117700">param-name</span><span style="color:#117700">></span>
        <span style="color:#117700"><</span><span style="color:#117700">param-value</span><span style="color:#117700">></span>UFT-8<span style="color:#117700"></</span><span style="color:#117700">param-value</span><span style="color:#117700">></span>
    <span style="color:#117700"></</span><span style="color:#117700">context-param</span><span style="color:#117700">></span></span>
  • 修改 RegisterServlet

    <span style="background-color:#f8f8f8"><span style="color:#555555">@Override</span>
    <span style="color:#770088">public</span> <span style="color:#008855">void</span> <span style="color:#0000ff">init</span>(<span style="color:#000000">ServletConfig</span> <span style="color:#000000">config</span>) <span style="color:#770088">throws</span> <span style="color:#000000">ServletException</span> {
        <span style="color:#000000">System</span>.<span style="color:#000000">out</span>.<span style="color:#000000">println</span>(<span style="color:#aa1111">"开始读取上下文参数信息"</span>);
        <span style="color:#000000">ServletContext</span> <span style="color:#000000">servletConte</span>
            <span style="color:#000000">xt</span> <span style="color:#981a1a">=</span> <span style="color:#000000">config</span>.<span style="color:#000000">getServletContext</span>();
        <span style="color:#000000">Enumeration</span><span style="color:#981a1a"><</span><span style="color:#008855">String</span><span style="color:#981a1a">></span> <span style="color:#000000">initParameterNames</span> <span style="color:#981a1a">=</span> <span style="color:#000000">servletContext</span>.<span style="color:#000000">getInitParameterNames</span>();
        <span style="color:#770088">while</span> (<span style="color:#000000">initParameterNames</span>.<span style="color:#000000">hasMoreElements</span>()){
            <span style="color:#008855">String</span> <span style="color:#000000">initParameterName</span> <span style="color:#981a1a">=</span> <span style="color:#000000">initParameterNames</span>.<span style="color:#000000">nextElement</span>();
            <span style="color:#008855">String</span> <span style="color:#000000">initParameterValue</span> <span style="color:#981a1a">=</span> <span style="color:#000000">servletContext</span>.<span style="color:#000000">getInitParameter</span>(<span style="color:#000000">initParameterName</span>);
            <span style="color:#000000">System</span>.<span style="color:#000000">out</span>.<span style="color:#000000">println</span>( <span style="color:#000000">initParameterName</span> <span style="color:#981a1a">+</span> <span style="color:#aa1111">" => "</span> <span style="color:#981a1a">+</span> <span style="color:#000000">initParameterValue</span>);
        }
    }</span>
  • 测试

    查看控制台信息

第二节 MVC

1. 什么是 MVC

模型-视图-控制器(MVC模式)是一种非常经典的软件架构模式,在UI框架和UI设计思路中扮演着非常重要的角色。从设计模式的角度来看,MVC模式是一种复合模式,它将多个设计模式在一种解决方案中结合起来,用来解决许多设计问题。MVC模式把用户界面交互分拆到不同的三种角色中,使应用程序被分成三个核心部件:Model(模型)、View(视图)、Control(控制器) 

 

  • 模型:模型持有所有的数据、状态和程序逻辑。模型独立于视图和控制器。

  • 视图:用来呈现模型。视图通常直接从模型中取得它需要显示的状态与数据。对于相同的信息可以有多个不同的显示形式或视图。

  • 控制器:位于视图和模型中间,负责接受用户的输入,将输入进行解析并反馈给模型

MVC模式将它们分离以提高系统的灵活性和复用性,不使用MVC模式,用户界面设计往往将这些对象混在一起。MVC模式实现了模型和视图的分离,使得其具有以下优点:

  • 一个模型提供不同的多个视图表现形式,也能够为一个模型创建新的视图而无须重写模型。一旦模型的数据发生变化,模型将通知有关的视图,每个视图相应地刷新自己。

  • 模型可复用。因为模型是独立于视图的,所以可以把一个模型独立地移植到新的平台工作。

  • 提高开发效率。在开发界面显示部分时,仅仅需要考虑的是如何布局一个好的用户界面;开发模型时,仅仅要考虑的是业务逻辑和数据维护,这样能使开发者专注于某一方面的开发,提高开发效率。

2. JSP 中的 MVC

在 JSP 中 Servlet 扮演的是控制器, JSP 页面扮演的是视图,Java Bean 扮演的是模型。

案例: 将用户信息呈现在页面上

  • 编写视图 user.jsp

    <span style="background-color:#f8f8f8"><%<span style="color:#555555">@</span> <span style="color:#000000">page</span> <span style="color:#000000">contentType</span><span style="color:#981a1a">=</span><span style="color:#aa1111">"text/html;charset=UTF-8"</span> <span style="color:#000000">language</span><span style="color:#981a1a">=</span><span style="color:#aa1111">"java"</span> %>
    <span style="color:#117700"><</span><span style="color:#117700">html</span><span style="color:#117700">></span>
    <span style="color:#117700"><</span><span style="color:#117700">head</span><span style="color:#117700">></span>
        <span style="color:#117700"><</span><span style="color:#117700">title</span><span style="color:#117700">></span>显示用户信息<span style="color:#117700"></</span><span style="color:#117700">title</span><span style="color:#117700">></span>
    <span style="color:#117700"></</span><span style="color:#117700">head</span><span style="color:#117700">></span>
    <span style="color:#117700"><</span><span style="color:#117700">body</span><span style="color:#117700">></span>
        <span style="color:#117700"><</span><span style="color:#117700">a</span> <span style="color:#0000cc">href</span>=<span style="color:#aa1111">"showUserInfo"</span><span style="color:#117700">></span>显示用户信息<span style="color:#117700"></</span><span style="color:#117700">a</span><span style="color:#117700">></span>
    <span style="color:#117700"></</span><span style="color:#117700">body</span><span style="color:#117700">></span>
    <span style="color:#117700"></</span><span style="color:#117700">html</span><span style="color:#117700">></span></span>
  • 编写模型 User

    <span style="background-color:#f8f8f8"><span style="color:#770088">package</span> <span style="color:#0000ff">com</span>.<span style="color:#000000">qf</span>.<span style="color:#000000">jsp</span>.<span style="color:#000000">pojo</span>;
    ​
    <span style="color:#770088">public</span> <span style="color:#770088">class</span> <span style="color:#0000ff">User</span> {
    ​
        <span style="color:#770088">private</span> <span style="color:#008855">String</span> <span style="color:#000000">username</span>;
    ​
        <span style="color:#770088">private</span> <span style="color:#008855">String</span> <span style="color:#000000">name</span>;
    ​
        <span style="color:#770088">private</span> <span style="color:#008855">String</span> <span style="color:#000000">sex</span>;
    ​
        <span style="color:#770088">private</span> <span style="color:#008855">int</span> <span style="color:#000000">age</span>;
    ​
        <span style="color:#770088">public</span> <span style="color:#008855">String</span> <span style="color:#000000">getUsername</span>() {
            <span style="color:#770088">return</span> <span style="color:#000000">username</span>;
        }
    ​
        <span style="color:#770088">public</span> <span style="color:#008855">void</span> <span style="color:#000000">setUsername</span>(<span style="color:#008855">String</span> <span style="color:#000000">username</span>) {
            <span style="color:#770088">this</span>.<span style="color:#000000">username</span> <span style="color:#981a1a">=</span> <span style="color:#000000">username</span>;
        }
    ​
        <span style="color:#770088">public</span> <span style="color:#008855">String</span> <span style="color:#000000">getName</span>() {
            <span style="color:#770088">return</span> <span style="color:#000000">name</span>;
        }
    ​
        <span style="color:#770088">public</span> <span style="color:#008855">void</span> <span style="color:#000000">setName</span>(<span style="color:#008855">String</span> <span style="color:#000000">name</span>) {
            <span style="color:#770088">this</span>.<span style="color:#000000">name</span> <span style="color:#981a1a">=</span> <span style="color:#000000">name</span>;
        }
    ​
        <span style="color:#770088">public</span> <span style="color:#008855">String</span> <span style="color:#000000">getSex</span>() {
            <span style="color:#770088">return</span> <span style="color:#000000">sex</span>;
        }
    ​
        <span style="color:#770088">public</span> <span style="color:#008855">void</span> <span style="color:#000000">setSex</span>(<span style="color:#008855">String</span> <span style="color:#000000">sex</span>) {
            <span style="color:#770088">this</span>.<span style="color:#000000">sex</span> <span style="color:#981a1a">=</span> <span style="color:#000000">sex</span>;
        }
    ​
        <span style="color:#770088">public</span> <span style="color:#008855">int</span> <span style="color:#000000">getAge</span>() {
            <span style="color:#770088">return</span> <span style="color:#000000">age</span>;
        }
    ​
        <span style="color:#770088">public</span> <span style="color:#008855">void</span> <span style="color:#000000">setAge</span>(<span style="color:#008855">int</span> <span style="color:#000000">age</span>) {
            <span style="color:#770088">this</span>.<span style="color:#000000">age</span> <span style="color:#981a1a">=</span> <span style="color:#000000">age</span>;
        }
    ​
        <span style="color:#555555">@Override</span>
        <span style="color:#770088">public</span> <span style="color:#008855">String</span> <span style="color:#000000">toString</span>() {
            <span style="color:#770088">return</span> <span style="color:#aa1111">"User{"</span> <span style="color:#981a1a">+</span>
                    <span style="color:#aa1111">"username='"</span> <span style="color:#981a1a">+</span> <span style="color:#000000">username</span> <span style="color:#981a1a">+</span> <span style="color:#aa1111">'\''</span> <span style="color:#981a1a">+</span>
                    <span style="color:#aa1111">", name='"</span> <span style="color:#981a1a">+</span> <span style="color:#000000">name</span> <span style="color:#981a1a">+</span> <span style="color:#aa1111">'\''</span> <span style="color:#981a1a">+</span>
                    <span style="color:#aa1111">", sex='"</span> <span style="color:#981a1a">+</span> <span style="color:#000000">sex</span> <span style="color:#981a1a">+</span> <span style="color:#aa1111">'\''</span> <span style="color:#981a1a">+</span>
                    <span style="color:#aa1111">", age="</span> <span style="color:#981a1a">+</span> <span style="color:#000000">age</span> <span style="color:#981a1a">+</span>
                    <span style="color:#aa1111">'}'</span>;
        }
    }</span>
  • 编写控制器 UserInfoServlet

    <span style="background-color:#f8f8f8"><span style="color:#770088">package</span> <span style="color:#0000ff">com</span>.<span style="color:#000000">qf</span>.<span style="color:#000000">jsp</span>.<span style="color:#000000">servlet</span>;
    ​
    <span style="color:#770088">import</span> <span style="color:#000000">com</span>.<span style="color:#000000">qf</span>.<span style="color:#000000">jsp</span>.<span style="color:#000000">pojo</span>.<span style="color:#000000">User</span>;
    ​
    <span style="color:#770088">import</span> <span style="color:#000000">javax</span>.<span style="color:#000000">servlet</span>.<span style="color:#000000">ServletException</span>;
    <span style="color:#770088">import</span> <span style="color:#000000">javax</span>.<span style="color:#000000">servlet</span>.<span style="color:#000000">http</span>.<span style="color:#000000">HttpServlet</span>;
    <span style="color:#770088">import</span> <span style="color:#000000">javax</span>.<span style="color:#000000">servlet</span>.<span style="color:#000000">http</span>.<span style="color:#000000">HttpServletRequest</span>;
    <span style="color:#770088">import</span> <span style="color:#000000">javax</span>.<span style="color:#000000">servlet</span>.<span style="color:#000000">http</span>.<span style="color:#000000">HttpServletResponse</span>;
    <span style="color:#770088">import</span> <span style="color:#000000">java</span>.<span style="color:#000000">io</span>.<span style="color:#000000">IOException</span>;
    <span style="color:#770088">import</span> <span style="color:#000000">java</span>.<span style="color:#000000">io</span>.<span style="color:#000000">PrintWriter</span>;
    ​
    <span style="color:#770088">public</span> <span style="color:#770088">class</span> <span style="color:#0000ff">UserInfoServlet</span> <span style="color:#770088">extends</span> <span style="color:#000000">HttpServlet</span> {
    ​
        <span style="color:#555555">@Override</span>
        <span style="color:#770088">protected</span> <span style="color:#008855">void</span> <span style="color:#000000">doGet</span>(<span style="color:#000000">HttpServletRequest</span> <span style="color:#000000">req</span>, <span style="color:#000000">HttpServletResponse</span> <span style="color:#000000">resp</span>) <span style="color:#770088">throws</span> <span style="color:#000000">ServletException</span>, <span style="color:#000000">IOException</span> {
            <span style="color:#000000">User</span> <span style="color:#000000">user</span> <span style="color:#981a1a">=</span> <span style="color:#770088">new</span> <span style="color:#000000">User</span>();
            <span style="color:#000000">user</span>.<span style="color:#000000">setUsername</span>(<span style="color:#aa1111">"admin"</span>);
            <span style="color:#000000">user</span>.<span style="color:#000000">setName</span>(<span style="color:#aa1111">"管理员"</span>);
            <span style="color:#000000">user</span>.<span style="color:#000000">setSex</span>(<span style="color:#aa1111">"男"</span>);
            <span style="color:#000000">user</span>.<span style="color:#000000">setAge</span>(<span style="color:#116644">20</span>);
            <span style="color:#000000">resp</span>.<span style="color:#000000">setContentType</span>(<span style="color:#aa1111">"text/html;charset=utf-8"</span>);
            <span style="color:#000000">resp</span>.<span style="color:#000000">setCharacterEncoding</span>(<span style="color:#aa1111">"UTF-8"</span>);
            <span style="color:#000000">PrintWriter</span> <span style="color:#000000">writer</span> <span style="color:#981a1a">=</span> <span style="color:#000000">resp</span>.<span style="color:#000000">getWriter</span>();
            <span style="color:#aa5500">//告知视图需要展示的模型</span>
            <span style="color:#000000">writer</span>.<span style="color:#000000">print</span>(<span style="color:#000000">user</span>.<span style="color:#000000">toString</span>());
            <span style="color:#000000">writer</span>.<span style="color:#000000">flush</span>();
            <span style="color:#000000">writer</span>.<span style="color:#000000">close</span>();
        }
    }</span>
  • 配置 web.xml

    <span style="background-color:#f8f8f8"><span style="color:#117700"><</span><span style="color:#117700">servlet</span><span style="color:#117700">></span>
        <span style="color:#117700"><</span><span style="color:#117700">servlet-name</span><span style="color:#117700">></span>userInfoServlet<span style="color:#117700"></</span><span style="color:#117700">servlet-name</span><span style="color:#117700">></span>
        <span style="color:#117700"><</span><span style="color:#117700">servlet-class</span><span style="color:#117700">></span>com.qf.jsp.servlet.UserInfoServlet<span style="color:#117700"></</span><span style="color:#117700">servlet-class</span><span style="color:#117700">></span>
    <span style="color:#117700"></</span><span style="color:#117700">servlet</span><span style="color:#117700">></span>
    ​
    <span style="color:#117700"><</span><span style="color:#117700">servlet-mapping</span><span style="color:#117700">></span>
        <span style="color:#117700"><</span><span style="color:#117700">servlet-name</span><span style="color:#117700">></span>userInfoServlet<span style="color:#117700"></</span><span style="color:#117700">servlet-name</span><span style="color:#117700">></span>
        <span style="color:#117700"><</span><span style="color:#117700">url-pattern</span><span style="color:#117700">></span>/showUserInfo<span style="color:#117700"></</span><span style="color:#117700">url-pattern</span><span style="color:#117700">></span>
    <span style="color:#117700"></</span><span style="color:#117700">servlet-mapping</span><span style="color:#117700">></span></span>
  • 启动服务器,访问 user.jsp,然后测试

第三节 过滤器

1. 什么是过滤器

过滤器的概念过滤器是一个服务器端的组件,可以拦截客户端的请求和响应信息并对这些信息进行过滤。

2. 过滤器体系结构

2.1 Filter接口

<span style="background-color:#f8f8f8"><span style="color:#333333"><span style="color:#aa5500">//过滤器初始化</span>
<span style="color:#770088">default</span> <span style="color:#008855">void</span> <span style="color:#0000ff">init</span>(<span style="color:#000000">FilterConfig</span> <span style="color:#000000">filterConfig</span>) <span style="color:#770088">throws</span> <span style="color:#000000">ServletException</span> {
}
<span style="color:#aa5500">//过滤操作,与协议无关</span>
<span style="color:#008855">void</span> <span style="color:#0000ff">doFilter</span>(<span style="color:#000000">ServletRequest</span> <span style="color:#000000">req</span>, <span style="color:#000000">ServletResponse</span> <span style="color:#000000">resp</span>, <span style="color:#000000">FilterChain</span> <span style="color:#000000">chain</span>) <span style="color:#770088">throws</span> <span style="color:#000000">IOException</span>, <span style="color:#000000">ServletException</span>;
<span style="color:#aa5500">//过滤器销毁</span>
<span style="color:#770088">default</span> <span style="color:#008855">void</span> <span style="color:#0000ff">destroy</span>() {
}</span></span>

2.2 FilterConfig 接口

<span style="background-color:#f8f8f8"><span style="color:#333333"><span style="color:#aa5500">//获取过滤器实例的名称</span>
<span style="color:#008855">String</span> <span style="color:#0000ff">getFilterName</span>();
<span style="color:#aa5500">//获取Servlet上下文</span>
<span style="color:#000000">ServletContext</span> <span style="color:#0000ff">getServletContext</span>();
<span style="color:#aa5500">//从过滤器初始化配置中获取给定属性名对应的属性值</span>
<span style="color:#008855">String</span> <span style="color:#0000ff">getInitParameter</span>(<span style="color:#008855">String</span> <span style="color:#000000">parameterName</span>);
<span style="color:#aa5500">//获取过滤器初始化配置中所有的属性名</span>
<span style="color:#000000">Enumeration</span><span style="color:#981a1a"><</span><span style="color:#008855">String</span><span style="color:#981a1a">></span> <span style="color:#0000ff">getInitParameterNames</span>();</span></span>

2.3 案例

使用过滤器完成中文乱码处理

  • 编写过滤器 CharacterEncodingFilter

    <span style="background-color:#f8f8f8"><span style="color:#770088">package</span> <span style="color:#0000ff">com</span>.<span style="color:#000000">qf</span>.<span style="color:#000000">jsp</span>.<span style="color:#000000">filter</span>;
    ​
    <span style="color:#770088">import</span> <span style="color:#000000">javax</span>.<span style="color:#000000">servlet</span>.<span style="color:#981a1a">*</span>;
    <span style="color:#770088">import</span> <span style="color:#000000">java</span>.<span style="color:#000000">io</span>.<span style="color:#000000">IOException</span>;
    ​
    <span style="color:#770088">public</span> <span style="color:#770088">class</span> <span style="color:#0000ff">CharacterEncodingFilter</span> <span style="color:#770088">implements</span> <span style="color:#000000">Filter</span> {
    ​
        <span style="color:#770088">private</span> <span style="color:#008855">String</span> <span style="color:#000000">characterEncoding</span>;
    ​
        <span style="color:#555555">@Override</span>
        <span style="color:#770088">public</span> <span style="color:#008855">void</span> <span style="color:#000000">init</span>(<span style="color:#000000">FilterConfig</span> <span style="color:#000000">config</span>) <span style="color:#770088">throws</span> <span style="color:#000000">ServletException</span> {
            <span style="color:#000000">System</span>.<span style="color:#000000">out</span>.<span style="color:#000000">println</span>(<span style="color:#aa1111">"过滤器初始化"</span>);
            <span style="color:#770088">this</span>.<span style="color:#000000">characterEncoding</span> <span style="color:#981a1a">=</span> <span style="color:#000000">config</span>.<span style="color:#000000">getInitParameter</span>(<span style="color:#aa1111">"characterEncoding"</span>);
        }
    ​
        <span style="color:#555555">@Override</span>
        <span style="color:#770088">public</span> <span style="color:#008855">void</span> <span style="color:#000000">doFilter</span>(<span style="color:#000000">ServletRequest</span> <span style="color:#000000">request</span>,
                             <span style="color:#000000">ServletResponse</span> <span style="color:#000000">response</span>,
                             <span style="color:#000000">FilterChain</span> <span style="color:#000000">chain</span>)
                <span style="color:#770088">throws</span> <span style="color:#000000">IOException</span>, <span style="color:#000000">ServletException</span> {
            <span style="color:#000000">System</span>.<span style="color:#000000">out</span>.<span style="color:#000000">println</span>(<span style="color:#aa1111">"过滤器过滤操作"</span>);
            <span style="color:#000000">request</span>.<span style="color:#000000">setCharacterEncoding</span>(<span style="color:#000000">characterEncoding</span>);
            <span style="color:#000000">response</span>.<span style="color:#000000">setCharacterEncoding</span>(<span style="color:#000000">characterEncoding</span>);
            <span style="color:#aa5500">//让过滤器链中的其他过滤器执行,这行代码必不可少,否则,无法进入后面的Servlet执行</span>
            <span style="color:#000000">chain</span>.<span style="color:#000000">doFilter</span>(<span style="color:#000000">request</span>, <span style="color:#000000">response</span>);
        }
    ​
        <span style="color:#555555">@Override</span>
        <span style="color:#770088">public</span> <span style="color:#008855">void</span> <span style="color:#000000">destroy</span>() {
            <span style="color:#000000">System</span>.<span style="color:#000000">out</span>.<span style="color:#000000">println</span>(<span style="color:#aa1111">"过滤器销毁"</span>);
        }
    }</span>
  • 在 web.xml 中配置过滤器

    <span style="background-color:#f8f8f8"><span style="color:#117700"><</span><span style="color:#117700">filter</span><span style="color:#117700">></span>
        <span style="color:#117700"><</span><span style="color:#117700">filter-name</span><span style="color:#117700">></span>characterEncodingFilter<span style="color:#117700"></</span><span style="color:#117700">filter-name</span><span style="color:#117700">></span>
        <span style="color:#117700"><</span><span style="color:#117700">filter-class</span><span style="color:#117700">></span>com.qf.jsp.filter.CharacterEncodingFilter<span style="color:#117700"></</span><span style="color:#117700">filter-class</span><span style="color:#117700">></span>
        <span style="color:#117700"><</span><span style="color:#117700">init-param</span><span style="color:#117700">></span>
            <span style="color:#117700"><</span><span style="color:#117700">param-name</span><span style="color:#117700">></span>characterEncoding<span style="color:#117700"></</span><span style="color:#117700">param-name</span><span style="color:#117700">></span>
            <span style="color:#117700"><</span><span style="color:#117700">param-value</span><span style="color:#117700">></span>UTF-8<span style="color:#117700"></</span><span style="color:#117700">param-value</span><span style="color:#117700">></span>
        <span style="color:#117700"></</span><span style="color:#117700">init-param</span><span style="color:#117700">></span>
    <span style="color:#117700"></</span><span style="color:#117700">filter</span><span style="color:#117700">></span>
    ​
    <span style="color:#117700"><</span><span style="color:#117700">filter-mapping</span><span style="color:#117700">></span>
        <span style="color:#117700"><</span><span style="color:#117700">filter-name</span><span style="color:#117700">></span>characterEncodingFilter<span style="color:#117700"></</span><span style="color:#117700">filter-name</span><span style="color:#117700">></span>
        <span style="color:#aa5500"><!-- * 标识通配符,匹配所有--></span>
        <span style="color:#117700"><</span><span style="color:#117700">url-pattern</span><span style="color:#117700">></span>/*<span style="color:#117700"></</span><span style="color:#117700">url-pattern</span><span style="color:#117700">></span>
    <span style="color:#117700"></</span><span style="color:#117700">filter-mapping</span><span style="color:#117700">></span></span>
  • 测试

  • 结论

2.4 HttpFilter 抽象类

<span style="background-color:#f8f8f8"><span style="color:#333333"><span style="color:#aa5500">//重写无协议过滤器操作,调用下面支持HTTP协议请求过滤操作的方法</span>
<span style="color:#770088">public</span> <span style="color:#008855">void</span> <span style="color:#0000ff">doFilter</span>(<span style="color:#000000">ServletRequest</span> <span style="color:#000000">request</span>, <span style="color:#000000">ServletResponse</span> <span style="color:#000000">response</span>, <span style="color:#000000">FilterChain</span> <span style="color:#000000">chain</span>) <span style="color:#770088">throws</span> <span style="color:#000000">IOException</span>, <span style="color:#000000">ServletException</span> {}
<span style="color:#aa5500">//HTTP协议请求过滤操作的方法</span>
<span style="color:#770088">protected</span> <span style="color:#008855">void</span> <span style="color:#0000ff">doFilter</span>(<span style="color:#000000">HttpServletRequest</span> <span style="color:#000000">request</span>, <span style="color:#000000">HttpServletResponse</span> <span style="color:#000000">response</span>, <span style="color:#000000">FilterChain</span> <span style="color:#000000">chain</span>) <span style="color:#770088">throws</span> <span style="color:#000000">IOException</span>, <span style="color:#000000">ServletException</span> {}</span></span>

2.5 案例

使用过滤器完成登录超时处理

  • 编写过滤器

    <span style="background-color:#f8f8f8"><span style="color:#770088">package</span> <span style="color:#0000ff">com</span>.<span style="color:#000000">qf</span>.<span style="color:#000000">jsp</span>.<span style="color:#000000">filter</span>;
    ​
    <span style="color:#770088">import</span> <span style="color:#000000">javax</span>.<span style="color:#000000">servlet</span>.<span style="color:#000000">FilterChain</span>;
    <span style="color:#770088">import</span> <span style="color:#000000">javax</span>.<span style="color:#000000">servlet</span>.<span style="color:#000000">ServletException</span>;
    <span style="color:#770088">import</span> <span style="color:#000000">javax</span>.<span style="color:#000000">servlet</span>.<span style="color:#000000">http</span>.<span style="color:#000000">HttpFilter</span>;
    <span style="color:#770088">import</span> <span style="color:#000000">javax</span>.<span style="color:#000000">servlet</span>.<span style="color:#000000">http</span>.<span style="color:#000000">HttpServletRequest</span>;
    <span style="color:#770088">import</span> <span style="color:#000000">javax</span>.<span style="color:#000000">servlet</span>.<span style="color:#000000">http</span>.<span style="color:#000000">HttpServletResponse</span>;
    <span style="color:#770088">import</span> <span style="color:#000000">javax</span>.<span style="color:#000000">servlet</span>.<span style="color:#000000">http</span>.<span style="color:#000000">HttpSession</span>;
    <span style="color:#770088">import</span> <span style="color:#000000">java</span>.<span style="color:#000000">io</span>.<span style="color:#000000">IOException</span>;
    ​
    <span style="color:#770088">public</span> <span style="color:#770088">class</span> <span style="color:#0000ff">TimeoutFilter</span> <span style="color:#770088">extends</span> <span style="color:#000000">HttpFilter</span> {
    ​
        <span style="color:#555555">@Override</span>
        <span style="color:#770088">protected</span> <span style="color:#008855">void</span> <span style="color:#000000">doFilter</span>(<span style="color:#000000">HttpServletRequest</span> <span style="color:#000000">request</span>, <span style="color:#000000">HttpServletResponse</span> <span style="color:#000000">response</span>, <span style="color:#000000">FilterChain</span> <span style="color:#000000">chain</span>) <span style="color:#770088">throws</span> <span style="color:#000000">IOException</span>, <span style="color:#000000">ServletException</span> {
            <span style="color:#000000">HttpSession</span> <span style="color:#000000">session</span> <span style="color:#981a1a">=</span> <span style="color:#000000">request</span>.<span style="color:#000000">getSession</span>();
            <span style="color:#008855">Object</span> <span style="color:#000000">username</span> <span style="color:#981a1a">=</span> <span style="color:#000000">session</span>.<span style="color:#000000">getAttribute</span>(<span style="color:#aa1111">"username"</span>);
            <span style="color:#aa5500">//session中没有数据了,说明session已经过期了,当前的session是一个新的session</span>
            <span style="color:#770088">if</span>(<span style="color:#000000">username</span> <span style="color:#981a1a">==</span> <span style="color:#221199">null</span>){
                <span style="color:#aa5500">//首页的地址 就是上下文路径</span>
                <span style="color:#008855">String</span> <span style="color:#000000">homePageUrl</span> <span style="color:#981a1a">=</span> <span style="color:#000000">request</span>.<span style="color:#000000">getContextPath</span>();
                <span style="color:#770088">if</span>(<span style="color:#aa1111">""</span>.<span style="color:#000000">equalsIgnoreCase</span>(<span style="color:#000000">homePageUrl</span>)){<span style="color:#aa5500">//上下文路径为空字符串时给一条斜杠即可</span>
                    <span style="color:#000000">homePageUrl</span> <span style="color:#981a1a">=</span> <span style="color:#aa1111">"/"</span>;
                }
                <span style="color:#000000">response</span>.<span style="color:#000000">sendRedirect</span>(<span style="color:#000000">homePageUrl</span>);
            } <span style="color:#770088">else</span> {
                <span style="color:#aa5500">//让过滤器链中的其他过滤器执行,这行代码必不可少,否则,无法进入后面的Servlet执行</span>
                <span style="color:#000000">chain</span>.<span style="color:#000000">doFilter</span>(<span style="color:#000000">request</span>, <span style="color:#000000">response</span>);
            }
        }
    }</span>
  • 在 web.xml 中配置过滤器

    <span style="background-color:#f8f8f8"><span style="color:#117700"><</span><span style="color:#117700">filter</span><span style="color:#117700">></span>
        <span style="color:#117700"><</span><span style="color:#117700">filter-name</span><span style="color:#117700">></span>timeoutFilter<span style="color:#117700"></</span><span style="color:#117700">filter-name</span><span style="color:#117700">></span>
        <span style="color:#117700"><</span><span style="color:#117700">filter-class</span><span style="color:#117700">></span>com.qf.jsp.filter.TimeoutFilter<span style="color:#117700"></</span><span style="color:#117700">filter-class</span><span style="color:#117700">></span>
    <span style="color:#117700"></</span><span style="color:#117700">filter</span><span style="color:#117700">></span>
    ​
    <span style="color:#117700"><</span><span style="color:#117700">filter-mapping</span><span style="color:#117700">></span>
        <span style="color:#117700"><</span><span style="color:#117700">filter-name</span><span style="color:#117700">></span>timeoutFilter<span style="color:#117700"></</span><span style="color:#117700">filter-name</span><span style="color:#117700">></span>
        <span style="color:#aa5500"><!-- * 标识通配符,匹配所有--></span>
        <span style="color:#117700"><</span><span style="color:#117700">url-pattern</span><span style="color:#117700">></span>/*<span style="color:#117700"></</span><span style="color:#117700">url-pattern</span><span style="color:#117700">></span>
    <span style="color:#117700"></</span><span style="color:#117700">filter-mapping</span><span style="color:#117700">></span></span>

2.6 Filter 交互流程

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值