Java Web Learning of CT -- Servlet API

8 Servlet API

Servlet life cycle: instantiation, initialization, service and destruction

Initialization methods: init(), init (config).

If we need to do some preparatory work during Servlet initialization, we can override the init method

8.1 ServletConfig

Get the initialization data of ServletConfig

public class Demo01Servlet extends HttpServlet{
    public void init() throws ServletException {
        ServletConfig config = getServletConfig();
        String initValue = config.getInitParameter("hello");

xml configuration


Annotation method

@WebServlet(urlPatterns = {"/demo01"},
        initParams = {
            @WebInitParam(name = "hello",value = "world"),
            @WebInitParam(name = "uname",value = "jim")

Get context data

Get ServletContext:

  • In the init method, it can be obtained directly through the get method
  • In the service method, it can also be obtained through request
ServletContext servletContext = getServletContext();
String contextConfigLocation = servletContext.getInitParameter("contextConfigLocation");

xml configuration


8.2 Filter

  • Filter also belongs to Servlet specification
  • Create a new Filter class to implement the Filter interface. You can use the annotation @ WebFilter or xml file configuration
  • The Filter configuration can also use wildcards to realize the common filtering of multiple servlets
  • Filter chain: multiple filters take effect at the same time and are passed in a chain.
    • Effective sequence of filter chain:
      • Annotation configuration: takes effect according to the parent order of the full class name of the file
      • xml configuration: takes effect in xml order

xml configuration


Annotation configuration (note that the name is the same as the configuration of Servlet)

public class Demo01Filter implements Filter {
    public void init(FilterConfig filterConfig) throws ServletException {


    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        filterChain.doFilter(servletRequest,servletResponse);//Execute the service in the servlet

    public void destroy() {


We put the setting code into the Filter in the fruit inventory

8.3 transaction management

Refer to JDBC md

Fruit inventory update component

  • OpenSessionInViewFilter

  • TransactionManager

  • ConnUtil

  • BaseDAO

  • ThreadLocal


ThreadLocal is called a local thread

  • set(): store data on the current thread
  • get(Object): fetch data from the current thread
  • Each Thread maintains its own container ThreadLocalMap
  • ThreadLocal is equivalent to key and maintains a value (such as Connection)

8.4 Listener

first group

  1. ServletContextListener: monitors the creation and destruction of ServletContext objects

  2. HttpSessionListener: monitors the creation and destruction of HttpSession objects

  3. ServletRequestListener: monitors the creation and destruction of ServletRequest objects

Group 2

  1. ServletContextAttributeListener: listens to the change events (add, remove, replace) of the save scope of ServletContext

  2. HttpSessionAttributeListener: listens to the change events (add, remove, replace) of the save scope of HttpSession

  3. ServletRequestAttributeListener: listens to the change events (add, remove, replace) of the save scope of ServletRequest

Group 3

  1. HttpSessionBindingListener: listens to the creation and removal of an object in the session domain
  2. HttpSessionActivationListener: listens for the serialization and deserialization of an object in the session domain

collocation method

Comments: @ WebListener

xml configuration:



* @Description: Listen to the creation of ServletContext in ServletContextListener and create beanFactory at this time
public class ContextLoaderListener implements ServletContextListener {
    public void contextInitialized(ServletContextEvent servletContextEvent) {
        ServletContext application = servletContextEvent.getServletContext();
        String path = application.getInitParameter("contextConfigLocation");
        BeanFactory beanFactory = new ClassPathXmlApplicationContext(path);

    public void contextDestroyed(ServletContextEvent servletContextEvent) {


Keywords: Java xml

Added by Thatsmej on Mon, 21 Feb 2022 15:52:39 +0200