2017年10月

说明:本文基于CentOS release 6.5 (Final),是工作过程中记录的一些随笔,内容未做详细考究.


  • 设置DNS服务器

  vi /etc/resolv.conf
  nameserver 114.114.114.114
  • 查看Linux版本
    lsb_release -a
LSB Version:    :base-4.0-amd64:base-4.0-noarch:core-4.0-amd64:core-4.0-noarch:graphics-4.0-amd64:graphics-4.0-noarch:printing-4.0-amd64:printing-4.0-noarch
Distributor ID:    CentOS
Description:    CentOS release 6.5 (Final)
Release:    6.5
Codename:    Final

通过implements ServletContextListener实现监听器Linstener

package cn.config;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

/*
* Servlet 容器启动或终止Web 应用时,会触发ServletContextEvent事件
* 
* ServletContext对象是一个为整个Web应用提供共享的内存,任何请求都可以获取里面的内容
* 实现ServletContextListener时通过ServletContext.setAttribute(key,value)将内容提交到内存中
* 其他地方通过ServletContext.getAttribute(key)获取内容(HelloWorld#doPost)
* 
* 官方对于ServletContextEvent的介绍:
* This is the event class for notifications about changes to the servlet context of a web application.
*/

public class MyLinstener implements ServletContextListener {

    /**
     * ① Servlet启动时先调用contextInitialized方法, 之后再对Filter和需要被初始化的Servlet初始化
     */
    @Override
    public void contextInitialized(ServletContextEvent paramServletContextEvent) {
        ServletContext context = paramServletContextEvent.getServletContext();
        context.setAttribute("linstener", "fromContextInitialized");
        System.out.println("...    contextInitialized    ... ");
    }

    /**
     * ② Servlet停止时先销毁所有的Servlet和Filter, 再调用contextDestroyed方法
     */
    @Override
    public void contextDestroyed(ServletContextEvent paramServletContextEvent) {
        ServletContext dest = paramServletContextEvent.getServletContext();
        System.out.println("contextDestroyed... " + dest.getAttribute("linstener"));
    }

}

web.xml配置监听器

- 阅读剩余部分 -

  1. 过滤器可以动态的拦截请求和响应, 且Filter在Servlet之前执行.
  2. 过滤器常用语编码过滤,用户是否登录,权限判断等.
  3. 通过 implements javax.servlet.Filter 实现一个过滤器

以字符编码过滤器为例

package cn.config;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class CharSetFilter implements Filter {

    private String encode = "utf-8";

    /**
     * init()用于初始化工作
     * filterConfig.getInitParameter("charset")获取web.xml中配置的内容
     */
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        String defaultCharset = filterConfig.getInitParameter("charset").trim();
        this.encode = "".equals(defaultCharset) ? this.encode : defaultCharset;
        System.out.println("init complete...");
    }

    
    /**
     * doFilter()完成实际的过滤工作 FilterChain用于访问后续过滤器
     */
    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain)
            throws IOException, ServletException {
        HttpServletRequest req = (HttpServletRequest) request;
        HttpServletResponse res = (HttpServletResponse) response;
        req.setCharacterEncoding(encode);
        res.setCharacterEncoding(encode);
                System.out.println("doFilter...");
        filterChain.doFilter(request, response);
    }

    /**
     * Servlet容器在销毁过滤器实例前调用destroy()方法,释放Servlet过滤器占用的资源。
     */
    @Override
    public void destroy() {
        System.out.println("destroy complete...");
    }

}
<filter>
        <filter-name>charSetFilter</filter-name>
        <filter-class>cn.config.CharSetFilter</filter-class>
        <init-param>
            <param-name>charset</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
        <init-param>
            <param-name>contentType</param-name>
            <param-value>text/html;charset=UTF-8</param-value>
        </init-param>
    </filter>
        <!-- 
         多个filter,按照filter-mapping的执行顺序执行,
         而不是按照filter的定义顺序 
         -->
    <filter-mapping>
        <filter-name>charSetFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

使用Servlet的示例代码,此处会先后输出init complete...和doFilter...

一、生命周期 init()、service()、destory()

  1. 在第一次创建Servlet的时候通过init()初始化,之后不再执行此方法
  2. 通过service()处理HTTP请求,常用doGet()、doPost()
  3. destory()在Servlet结束时执行
    最后由JVM进行垃圾回收.

二、实现方式 implements Servlet、extends GenericServlet、extends HttpServlet

HttpServlet本身就是通过implements Servlet实现的.


三、代码示例: 通过继承HttpServlet实现Servlet


- 阅读剩余部分 -