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{
    @Override
    public void init() throws ServletException {
        ServletConfig config = getServletConfig();
        String initValue = config.getInitParameter("hello");
        System.out.println(initValue);
    }
}

xml configuration

<servlet>
    <servlet-name>Demo01Servlet</servlet-name>
    <servlet-class>com.ct.servlet.Demo01Servlet</servlet-class>
    <init-param>
        <param-name>hello</param-name>
        <param-value>world</param-value>
    </init-param>
    <init-param>
        <param-name>uname</param-name>
        <param-value>jim</param-value>
    </init-param>
</servlet>

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");
System.out.println(contextConfigLocation);//classpath:applicationContext.xml

xml configuration

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
</context-param>

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

<filter>
    <filter-name>demo01</filter-name>
    <filter-class>com.ct.filters.Demo01Filter</filter-class>
</filter>
<filter-mapping>
    <filter-name>demo01</filter-name>
    <url-pattern>/demo01.do</url-pattern>
</filter-mapping>

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

@WebFilter("/demo01.do")
public class Demo01Filter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        System.out.println("helloA");
        filterChain.doFilter(servletRequest,servletResponse);//Execute the service in the servlet
        System.out.println("helloA2");
    }

    @Override
    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

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:

<listener>
    <listener-class>com.ct.listener.MyServletContextListener</listener-class>
</listener>

application

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

    @Override
    public void contextDestroyed(ServletContextEvent servletContextEvent) {

    }
}

Keywords: Java xml

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