Chapter 9 Built-in object : application

 

Application Object is used to share the data with all application pages. Thus, all users share information of a given application using the Application object. The session like private storage bin in the classroom, only the owner can use it. The application like public storage bin in the classroom, all the classmates can use it. So each host (virtual host) has its own application, but A virtual host can’t visit B virtual host’s application.

 

 

 

9.1  Write and read data into application

  The application does not look like the session, it is always exist, do not expire, from server's start until server's stop, but if the server restart, then the application of information would be lost.

 

1.       public void setAttribute(String name, Object value)

Store the object with the given object name in the application. Any existing binding with the same name is replaced.

Parameters: name -  the name to which the data object will be bound.

           value - the data object to be bound, if it’s null then equal to “removeAttribute(name)” method.

Throws: If value of parameter “name” is null then throws IllegalArgumentException.

 

2.       public Object getAttribute(String name)

Returns the object bound to the given name in the application's application layer data. Returns null if there is no such binding.

Parameters: name - the name of the binding to find.

Returns: the value bound to that name, or null if the binding does not exist.

 

3.       public void removeAttribute(String name)

Removes the object bound to the given name in the application's application layer data. Does nothing if there is no object bound to the given name.

Parameters: name - the name of the object to remove

 

 

Notice: All of the above methods are synchronized, so multiple threads at the same time to use these methods are safe.

 

 

9.2 How to use application

The most typical application of “application” is chat room, put everybody's speech content into “application”, so each other can see the speech content from “application”.

Now look at a simple example of the chat room, this example use the upper and lower frame, total of three documents.

The “chat.htm” is framework file, source code:

1

2

3

4

5

6

7

8

9

<html>

<head>

  <title>Chat room</title>

</head>

  <frameset cols="72%,*">

    <frame name = "message" src="chat1.dqm">

    <frame name = "say" src="chat2.dqm">

  </frameset>

<html>

 

From above source code line 6 and 7 you can see this framework file include two dqm files: chat1.dqm and chat2.dqm.

 

The dqm file “chat2.dqm” can put speech content into application, it’s source code:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

 

17

18

19

20

21

22

23

24

25

26

27

28

29

 

30

31

32

33

34

<%@page import = "java.util.ArrayList;java.util.Date" %>

 

<html>

<head>

  <title>speak</title>

</head>

 

<body>

  <form name="form1" method="post" action="">

    Please speak: <input type="text" name="pronunciation" size="30">

    <input type="submit" value="send">

  </form>

 

<%

String tempLine;

if ((tempLine = request.getParameter("pronunciation")) != null) { //If has the speech content

  ArrayList<String> content = (ArrayList)application.getAttribute("chatContent");

  if (content == null) {

    content = new ArrayList<String>();

    application.setAttribute("chatContent", content);

  }

  int overNo = content.size() - 39;

  if (overNo > 0) { //If application have too many speech contents, then delete it.

    for (int i = 0; i < overNo; i++) {

      content.remove(i);

    }

  }

  Date date = new Date();

  content.add(request.getRemoteAddr() + "<font color=\"#FF0000\">(" + date + ")</font><br>" + tempLine + "<br>");

}

%>

 

</body>

</html>

 

From above source code line 29 you can see speech content, speech time and IP of speaker all will save in a variable (this variable name is “content”, type is ArrayList). And “content” variable will put into “application” object, so id first execute this dqm then will create a new “content” variable into “application” (source code line 17 to 21). The “content” variable most preserves 40 records of speech (source code line 22 to 27).

 

The dqm file “chat1.dqm” can display speech content, source code:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

<%@page import = "java.util.ArrayList" %>

 

<html>

<head>

  <title>speech content</title>

  <meta http-equiv="refresh" content="5">

</head>

 

<body>

  <%

  ArrayList<String> content = (ArrayList)application.getAttribute("chatContent");

  if (content != null) {

    for (int i = content.size() - 1; i >= 0; i--) {

      out.println(content.get(i) + "<br>");

    }

  }

  %>

</body>

</html>

 

This page will automatic refresh every 5 seconds (above source code line 6). Automatic refresh can constantly display the latest speech content (source code line 10 to 17).

 

  The picture of implementation:

Picture 9-2-1

 

You can through different client to visit this chat room, it will like multi-people to chat.