Listener, Filter, Servlet and Java Web project Initialization

Source: Internet
Author: User

Generally, Initialization is required when a Java Web project is started, such as configuring cache and scheduled tasks. For example, a company in China is responsible for maintaining the server of the company's global communication system. The company has more than employees, the content to be initialized is to initialize more than user information to the cache at system startup. the user logs on to the cache through the client, as after the server sends a request, verify user information directly from the cache. The advantage of this is to reduce the pressure on the server and database. Another advantage is that when an employee logs on from the client, the system requires a series of authentication, the process is very complicated, and the process of authentication is very time-consuming. This will greatly affect the user experience of employees using the client. configuring cache on the server can solve these problems well.
The following code is an example of simulated initialization. You can view the program output on the console, in sequence of Listener, Filter, and Servlet startup. We strongly recommend that you perform Coder operations like me, let's just talk about the code.
[Java] package com. tom. servlet;
 
Import java. util. Timer;
 
Import javax. servlet. ServletContextEvent;
Import javax. servlet. ServletContextListener;
 
Public class HelloListener implements ServletContextListener {

Public HelloListener (){
System. out. println ("Listener ()----------");
}
 
Public void contextDestroyed (ServletContextEvent arg0 ){
System. out. println ("Listener contextDestroyed ()-----------------");
}
 
Public void contextInitialized (ServletContextEvent arg0 ){
System. out. println ("Listener contextInitialized ()-----------------");
Timer timer = new Timer ();
Timer. schedule (new TimerTest02 (), 0, 4000L );
}
 
}

[Java]
Package com. tom. servlet;
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;
 
 
Public class HelloFilter implements Filter {

Private Long seconds;

Public HelloFilter (){
System. out. println ("++ Filter () ++ ");
}
 
Public void init (FilterConfig config) throws ServletException {
System. out. println ("++ FilterInit () ++ ");
This. seconds = Long. parseLong (config. getInitParameter ("seconds "));
TimerTest. initTimer (seconds );
}

Public void doFilter (ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {

System. out. println ("++ doFilter () ++ ");
}

Public void destroy (){
This. seconds = null;
}
 
Public Long getSeconds (){
Return seconds;
}
 
}

[Java]
Package com. tom. servlet;
 
Import java. io. IOException;
Import java. util. Timer;
 
Import javax. servlet. ServletException;
Import javax. servlet. http. HttpServlet;
Import javax. servlet. http. HttpServletRequest;
Import javax. servlet. http. HttpServletResponse;
 
Public class HelloServlet extends HttpServlet {

Private static final long serialVersionUID =-7693562670980406523L;
 
Public HelloServlet (){
System. out. println ("----- Servlet ()-----");
}

Public void init () throws ServletException {
System. out. println ("----- ServletInit ()-----");
Timer timer = new Timer ();
Timer. scheduleAtFixedRate (new TimerTest01 (), 0, 6000L );
}
 
Public void destroy (){
System. out. println ("----- ServletDestroy ()-----");
}
 
Public void doGet (HttpServletRequest request, HttpServletResponse response)
Throws ServletException, IOException {
 
}
 
Public void doPost (HttpServletRequest request, HttpServletResponse response)
Throws ServletException, IOException {
}
 
}

[Java]
Package com. tom. servlet;
 
Import java. util. TimerTask;
Import java. util. concurrent. Executors;
Import java. util. concurrent. TimeUnit;
 
Public class TimerTest {

Private static int I = 0;

Public static void initTimer (Long seconds ){
TimerTest. schedule (new TimerTask (){
Public void run (){
CallUsers ();
}
}, 0, seconds x 1000 );
 
}
Public static void callUsers (){
System. out. println ("Filter refreshed:" ++ I + "times .");
}
 
Public static void schedule (TimerTask task, long delay, long period ){
Executors. newScheduledThreadPool (1). scheduleAtFixedRate (task, delay, period,
TimeUnit. MILLISECONDS );
}
 
}
 
Class TimerTest01 extends TimerTask {
Private static int I = 0;

@ Override
Public void run (){
System. out. println ("Servlet refreshed:" ++ I + "times .");
}
}
 
Class TimerTest02 extends TimerTask {
Private static int I = 0;

@ Override
Public void run (){
System. out. println ("Listener refreshed:" ++ I + "times .");
}
}

[Html]
<? Xml version = "1.0" encoding = "UTF-8"?>
<Web-app version = "2.5" xmlns = "http://java.sun.com/xml/ns/javaee"
Xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance"
Xsi: schemaLocation = "http://java.sun.com/xml/ns/javaee
Http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd>
 
<Filter>
<Filter-name> HelloFilter </filter-name>
<Filter-class> com. tom. servlet. HelloFilter </filter-class>
<Init-param>
<Param-name> seconds </param-name>
<Param-value> 5 </param-value>
</Init-param>
</Filter>
<Filter-mapping>
<Filter-name> HelloFilter </filter-name>
<Url-pattern>/* </url-pattern>
</Filter-mapping>


<Servlet>
<Servlet-name> HelloServlet </servlet-name>
<Servlet-class> com. tom. servlet. HelloServlet </servlet-class>
<Load-on-startup> 0 </load-on-startup>
</Servlet>
 
<Servlet-mapping>
<Servlet-name> HelloServlet </servlet-name>
<Url-pattern>/HelloServlet </url-pattern>
</Servlet-mapping>

<Listener>
<Listener-class> com. tom. servlet. HelloListener </listener-class>
</Listener>

<Welcome-file-list>
<Welcome-file> index. jsp </welcome-file>
</Welcome-file-list>
</Web-app>

The above code is an example of a pure web project and does not rely on any popular development frameworks. Of course, there are some other solutions for project initialization, such as ActionServlet under Struts1.X and FilterDispatcher under Struts2.X, this example will not be repeated. If you like it, you can study it.
The code in this example is for your reference. You are welcome to discuss and criticize and correct it!
To facilitate reading in IDE, we specially package the example project: http://www.bkjia.com/uploadfile/2012/0424/20120424015209986.zip



From TOM4J
 

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.