Create a Java Web listener

Source: Internet
Author: User
Tags stub

Prior to learning from the Java Web, I have not learned some advanced usage of the Servlet container class, because after learning the simple JSP and the servlet writing, spring learning is started.

Some common variables of Web application are application, that is, the life cycle of application opening is statically processed.

< Listener >        < Listener-class >xyz.springabc.web.listener.StartUpListener</listener-class>  </listener>

First add listen, an observer

 Public classStartuplistenerImplementsServletcontextlistener {//This observer implements the Servletcontextlistener/*** Default constructor. */     PublicStartuplistener () {//TODO auto-generated Constructor stub    }    /**     * @seeservletcontextlistener#contextdestroyed (servletcontextevent)*/     Public voidcontextdestroyed (Servletcontextevent event) {//TODO auto-generated Method Stub    }    /**     * @seeservletcontextlistener#contextinitialized (servletcontextevent)*/     Public voidcontextinitialized (Servletcontextevent event) {//servlet Call this function when the context is initialized ServletContext application=Event.getservletcontext ();//Get Context ApplicationContext ApplicationContext=webapplicationcontextutils.getwebapplicationcontext (application);//Use spring's tool class to get the spring context Propertyserv Propertyserv= Applicationcontext.getbean (Propertyserv.class);//Get Service Bean propertyserv.setup (application) in spring context;//throw the setting information inside, the page can be read directly    }}

Here are some implementations of the service

@Service Public classPropertyserv {@AutowiredPrivatePropertyrepo Propertyrepo; @AutowiredPrivateFieldrepo Fieldrepo; PrivateServletContext Application; /*** Start listening Setting this property *@paramApplication*/     Public voidSetup (ServletContext application) {//Call the Setup method This. application=Application;  for(Property Property:propertyRepo.findAll ()) {Field field=Fieldrepo.findonebyproperty (property);Application.setattribute (Property.getkeyword (), field);//This reads from the DAO layer that key value is loaded into the Application object, so the JSP page Polygons can get these constant values directly without having to invoke the service every time they get these site static variables from the database}}}

Create a Java Web listener

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.