Talking about the application of Listener Servlet

xiaoxiao2021-03-06  39

Talk Listener Servlet application of: Xiaohua issued a document time: 2004.11.23

Listener is a SERVLET listener that listens to the client's request, the server's operation, and the like. Through the listener, some operations can be automatically excited, such as listening to the number of users online. When an HTTPSession is added, the sessioncreated (httpSessionEvent se) method is excited, so that the number of online people can be added 1. The commonly used listening interface has the following: ServletContextttributeListener monitors the operation of the servletContext property, such as adding, deleting, modifying properties. ServletContextListener listens servletContext. When you create servletContext, the ContextInitialized (ServletContext SCE) method is excited; when servletContext is destroyed, the ContextDestroyed (ServletContextEvent SCE) method is excited. HttpSessionListener listens to HttpSession. When creating a session, the Session Created (HttpSessionEvent Se) method is excited; when you destroy a session, the SessionDestroyed (HttpSessionEvent SE) method is excited. HttpSessionTtributeListener listens to the operation of the properties in the httpsession. When adding an attribute Session, excitation attributeAdded (HttpSessionBindingEvent se) method; When you delete an attribute in the Session, excitation attributeRemoved (HttpSessionBindingEvent se) method; is reset when the Session properties, excitation attributeReplaced (HttpSessionBindingEvent se) method. Below we develop a specific example, this listener can count the number of people online. Print the corresponding information in the server console when servletContext is initialized and destroyed. When the attributes in the servletContext increase, change, delete, print the corresponding information in the server console. To get the above features, the listener must implement the following three interfaces: HttpSessionListener ServletContextListener ServletContexttRibuteListener We see the specific code, see Example 14-9. [Program source code]

1 // ==================== Program discrtiption ======================

2 // Program Name: Example 14-9: Encodingfilter .java

3 // Program: Learn to use the listener

4 // ================================================================================== ================ 5 Import javax.servlet.http. *;

6 Import javax.servlet. *;

Seduce

8 Public Class OnlineCountListener Implements HttpSessionListener,

ServletContextListener, ServletContextAttributeListener

9 {

10 privat.

11 private servletcontext context = null;

12

13 Public OnlineCountListener ()

14 {

15 count = 0;

16 // setContext ();

17}

18 // Create a session

19 Public Void Sessioncreated (HttpSessionEvent SE)

20 {

21 count ;

22 setContext (se);

twenty three

twenty four }

25 // Stimulates when a session is invalid

26 Public Void SessionDestroyed (HttpSessionEvent SE)

27 {

28 count -;

29 setContext (se);

30}

31 // Set the properties of Context, which will inspire AttributeReplaced or AttributeAdded method

32 Public Void SetContext (HttpSessionEvent SE)

33 {

34 se.getations (). GetServletContext ().

SetAttribute ("Online", New Integer (count);

35}

36 // Increases a new attribute

37 public void attributeadded (servletContextAttributeEvent Event) {

38

39 log ("AttributeAdded ('" event.getname () "" "

40 Event.getValue () ")");

41

42}

43

44 // Removal when a new attribute is deleted

45 Public Void AttributeMoved (ServletContextAttributeEvent Event) {

46

47 log ("AttributeRemoved ('" event.getname () "', '"

48 Event.getValue () "')");

49

50}

51

52 // Attributes excited when it is replaced

53 Public Void AttributeReplaced (servletContextAttributeEvent Event) {54

55 log ("AttributeReplaced ('" event.getname () "', '"

56 Event.getValue () "')");

57}

58 // context is stimulated when deleting

59 public void contextdestroyed (servletContextevent Event) {

60

61 log ("" ContextDestroyed () ");

62 this.Context = NULL;

63

64}

65

66 // context initialization

67 public void contextinitialized (servletContextevent Event) {

68

69 this.Context = event.getServletContext ();

70 log ("" ContextInitialized () ");

71

72}

73 private void log (string message) {

74

75 System.out.println ("ContextListener:" Message);

76}

77}

[Program Note] In OnlineCountListener, use count to represent the current number of current online, the onlineCountListener will automatically execute when the web server is started. When OnlineCountListener is constructed, set the count to 0. Each additional session, OnlineCountListener automatically invokes the sessioncreated (HttpSessionEvent SE) method; each destroyed a session, OnlineCountListener automatically calls the sessionDestroyed (HttpSessionEvent SE) method. When calling the sessioncreated (httpSessionEvent SE) method, there is another customer in the request, at which time the number of people online is added 1, and the count is written to the servletContext. The information of servletContext is shared by all clients, so that each client can read the number of people currently online. In order to take effect in the listener, you need to configure in web.xml, as shown below:

OnlineCountListener

test program:

<% @ Page ContentType = "text / html; charset = GB2312"%>

Currently online number:

<% = getServletContext (). GetAttribute ("online")%>

Exit the session:

GetServletContext (). GetAttribute ("Online") gains the specific value of COUNT. Client call

<% session.invalidate ();%>

Make the session, so that the listener will reduce COUNT 1. [Running program] Web.xml Do more above, put OnlineCountListener in the web-inf / class directory, start the web server, enter the following URL in the browser (depending on the specific situation): http: //127.0.0.1 : 8080 / ch14 / listener.jsp browser will print the current number of people. There are below the server:

...

ContextListener: contextinitialized ()

CONTEXTLISTENER: AttributeReplaced ('org.apache.

Catalina.welcome_files', '[ljava.lang.string; @ 1d98a')

...

ContextListener: attributeadded ('Online', '1')

ContextListener: AttributeReplaced ('Online', '1')

ContextListener: AttributeReplaced ('Online', '0')

ContextListener: AttributeReplaced ('Online', '1')

ContextListener: AttributeReplaced ('Online', '2')

(T111) This article is selected from the Freescale Book "Proficient Java Core Technology"

转载请注明原文地址:https://www.9cbs.com/read-54397.html

New Post(0)