Chapter 12 Create static web page

 

Uses the DQM technology we can create dynamic web page easily, but some cases, the result of execute dqm file will remain unchanged longtime. For example, a notice system, it maybe publish a notice in several months (write notice into database), but many users will read notices every day (read notice from database when user visit it). Because the frequency of update notice is very low, so so most of the time users read the contents of the notice is the same. In this kind of situation, if we generated a static web page when publish new notices every time, then the user read this static web page can know the latest notices. Use this method system will not access database when users read notices via dqm file, so performance improved. Of course, we can manually generate these static web pages, but it will be very troublesome and maintain with difficulty. So next we will introduce use kangaroo-egg server generated the static web page automatically.

 

 

12.1 The ID of static web page

If dqm file need to generate static web page then first must provide an unique static web page ID (for short: static ID), kangaroo-egg will check static web page use static ID, if the static web page which is conresponding to the static ID exists then directly output this static web page, else then server will first generate static web page by this static ID, next output static web page which newly generated. May see in the static web page function this static ID is very important, so how we define static ID? The DQM container has provided a method for dqm file, if user need to define static ID then must override(overwrite) this method.

 

 

public String getStaticPageId(

DRequestItface request, DSessionItface session, DApplicationItface application)

Define a static ID according to the current visit's condition.

Parameters: request - request bulid-in object in current dqm file, refer to chapter 6.

session - session bulid-in object in current dqm file, refer to chapter 8.

application - application bulid-in object in current dqm file, refer to chapter 9.

Returns: a static web page ID, if return null then means disable static web page function.

 

Ok next we need to solve several questions.

First question, “request”, “session” and “application” of these built-in variables can be used directly, so why we still transmit these built-in variables into “getStaticPageId” method? Because it’s a class method, class method must write in <%! %> block. But build-in object only can be used in <% %> block.

The method of “getStaticPageId” is a reserved by kangaroo-egg server, therefore please do not re-definition return type of this method. For example if you defind below method then server will throw error.

public int getStaticPageId(

DRequestItface request, DSessionItface session, DApplicationItface application)

 

Are there will happen problem if we not override “getStaticPageId” method in dqm file? Of course not, if not override it then will use default provides method. The default method will directly return null, that is disable the static web pages function.

What is use of three variables: request, session and application? You may also very puzzled about it. When we generate static ID usually need to use these three variables, for example, a bbs thread page has many pages, user need click “next_page url” to see next page, so the first page’s content and the second page’s content is not the same, then we must generate an unique static ID for each page. But how did we know that which page the user visiting now? This time we may use these three variables to get the page number which use visiting now.

  We give an example, create a dqm file “/ test_folder/mpage.dqm”, it can has many pages, use URL parameter “page” to control display which page’s content. If we visit first page then dqm file will display “Content of page 1”, if we visit second page then it will display “Content of page 2”, and so on.

Below is rule of URL parameter “page”:

“/test_folder/mpage.dqm?page=1” or “/test/viewnews.dqm” Display page 1.

“/test_folder/mpage.dqm?page=2” Display page 2.

“/test_folder/mpage.dqm?page=hd” If value of “page” is not a digit then display page 1 too.

  Therefore if we need to generate static ID for “mpage.dqm”, we must consider these situations.

  Below is source code of “mpage.dqm”:

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

35

<%

int pageid;

String pageIdStr = request.getParameter("page");

if (pageIdStr != null) {

  try {

    //Get page number which user current visiting from parameter "page"

    pageid = Integer.parseInt(pageIdStr);

  }

  catch(NumberFormatException e) { //If page number is not a digit

    pageid = 1;

  }

}

else { //If URL not include parameter "page" then default set first page

  pageid = 1;

}

 

out.println("Content of page "+ pageid);

%>

 

<%!

public String getStaticPageId(DRequestItface request, DSessionItface session, DApplicationItface application) {

  String pageIdStr = request.getParameter("page");

  if (pageIdStr != null) {

    try {

      return "/test_folder/mpage.dqm_page" + Integer.parseInt(pageIdStr);

    }

    catch(NumberFormatException e) { //If page number is not a digit

      return "/test_folder/mpage.dqm_page" + 1;

    }

  }

  else { //If URL not include parameter "page" then default set first page

    return "/test_folder/mpage.dqm_page" + 1;

  }

}

%>

 

At source code line 4, it check parameter “page”, if URL include parameter “page” then check it’s value, if it’s value is a digit then get it (source code line: 7).

If URL not include parameter “page” (source code line: 13 to 15) or parameter’s value is not a digit (source code line: 9 to 11) then default set page number to 1. At source code line 17, it output current page’s content.

 

At source code line 20 to 35, we override the “getStaticPageId” method to enable static web page function. At source code line 22, we get parameter which name is “page” from “request” variable. If we can get parameter “page” from “request” variable then check it’s value, if it’s value is a digit then return static ID of current page number (source code line: 25). If we can’t get parameter “page” from “request” variable (source code line: 31 to 33) or parameter’s value is not a digit (source code line: 27 to 29) then return first page’s static ID.

 

Next we discuss how to assign static pages ID. At source code line 25, 28 and 32, all will return static ID, first we must know the usefulness of static ID before know how to assign static ID. In actually the mean of static ID is very simple, the static ID represent static page web page, you may understand that static ID is the static web file’s filename. In the present example, when user visit first page then program will return "/test_folder/mpage.dqm_page1" as static ID (source code line: 28 and 32), so server will seek static web page file which corresponds this ID, if static web page file exists then output it, if it not exist first generate static web page by this static ID, next time user visit it again can directly output the static web file which alread generated. Users can try to visit the above dqm file’s first page, you will find server will automatically create “static_page” folder under “WEB-INF” directory. At the same time you will find that there are three files under “static_page” folder, see below:

Picture 12-1-1

 

We can seen that these three files main name is the same, all are “e0006b1ab3bc729274f010eefd0e73bb”.

Why can be that strange name? In fact, this strange name is MD5 code by “/test_folder/mpage.dqm_page1”. You can understand that MD5 code is an another kind of written format. About static id: “/test_folder/mpage.dqm_page1”, it returns from source code line 28 or 32.

  Next another question, why are there three files? In section 4.3 we know that the server can supports two kinds of compression output, gizp and deflate. So server will create three static files, two are the compressed file (extension name is “gzip” and “deflate”), one is the uncompressed file (extension name is “no”). If current browser support gzip compression, then static file which extension name is “gzip” will be output.

 

Next we listed all possible situations of visit “viewnews.dqm” first page:

Browser supports compression format

Static ID

Static file name(code static ID to MD5)

gzip

/test_folder/mpage.dqm_page1

e0006b1ab3bc729274f010eefd0e73bb.gzip

deflate

/test_folder/mpage.dqm_page1

e0006b1ab3bc729274f010eefd0e73bb.deflate

not support the above compression

/test_folder/mpage.dqm_page1

e0006b1ab3bc729274f010eefd0e73bb.no

Table 12-1-2

 

From the above explanation readers should know why server will generate three static page files. If static page file’s extension name is “gzip” then means that its content to be compressed on gzip format. If static page file’s extension name is “deflate” then means that its content to be compressed on deflate format. If static page file’s extension name is “no” then means that its content not to be compressed. Each static ID have three static files, it looks really complicated enough, but for users who do not need to care about this, because the system will automatically handle these documents, for users these static files are all transparent, so we introduced it only for user can better understanding static page function.

Ok next we return to discuss the static ID. From source code of “viewnews.dqm”, we can see this dqm file every page’s content is not the same, so we must base on each page to generate static pages. Will server generate 100 static web files if this dqm file has 100 pages? Yes, because the content of these 100 pages are not same, so we need to provide 100 unique static IDs for generate 100 static web pages. What will happen when we provide reduplicate static ID? Ok, we give an example, if we provide a same static ID for viewnews.dqm’s first page and second page, then if server first generate static file of first page, next even visit this dqm file’s second page but you will find it will still display the content of first static page. If server first generate static file of second page, then even visit this dqm file’s first page but you will find it will still display the content of second static page.

From above introduced that we can see the static ID is core of static web page. From “viewnews.dqm” source code line 25, 28 and 32, you can see we use dqm file’s absolute path and visit page number to generate unique static ID. We recommend use this method to generate static ID, because use dqm file’s absolute path can guarantee unique static ID to be generated. In fact, we can automatically obtain the absolute path, see below 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

35

<%

int pageid;

String pageIdStr = request.getParameter("page");

if (pageIdStr != null) {

  try {

    //Get page number which user current visiting from parameter "page"

    pageid = Integer.parseInt(pageIdStr);

  }

  catch(NumberFormatException e) { //If page number is not a digit

    pageid = 1;

  }

}

else { //If URL not include parameter "page" then default set first page

  pageid = 1;

}

 

out.println("Content of page "+ pageid);

%>

 

<%!

public String getStaticPageId(DRequestItface request, DSessionItface session, DApplicationItface application) {

  String pageIdStr = request.getParameter("page");

  if (pageIdStr != null) {

    try {

      return request.getNowPath() + "_page" + Integer.parseInt(pageIdStr);

    }

    catch(NumberFormatException e) { //If page number is not a digit

      return request.getNowPath() + "_page" + 1;

    }

  }

  else { //If URL not include parameter "page" then default set first page

    return request.getNowPath() + "_page" + 1;

  }

}

%>

 

Of course, users can generate static ID according to your actual situation. The “viewnews.dqm” dqm file only need to use “request” build-in object when generating static ID.

 

 

12.2 Regist static web page

The previous section introduced how to assign static ID in order to open the static web page function, but how many static web pages in current host, we can know? Of course, user can know how many static page current has produced? Because static ID and it page’s information will be registered in the system, so user can read these registration information from system at any time.

But how to read these registration information from system? Do you remember command object which introduced at chapter 10. Command object include methods which operate static web page.

 

 

1.       public int getStaticPageCount()

Returns the number of static web pages in current host.

Returns: the number of static web pages in current host.

 

2.       public boolean clearStaticPage(String staticPageId)

Removes the static page of the specified staticPageId from current host, if it is present (optional operation).

Parameters: Static page to be removed from current host, if present

Returns: true if current host contained the specified static web page.

 

3.       public void clearAllStaticPage()

Removes all of the static web page from current host.

 

The first method is to be understood easy, it can get the number of static web pages in current host. But what is the usefulness of second and third methods? Because the system will not inspect the dqm file whether already expired, so we must use these 2 methods to manually delete static web pages, like this system will re-created static web page when we visit dqm file again.

For example, the dqm file “/test_folder/readfile.dqm” will load “content.txt” file and output it’s content, source code as below:

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

<pre>

<%

File readFile = new File(request.getNowFolder() + File.separatorChar + "content.txt");

if (readFile.exists()) {

  BufferedReader br = null;

  try {

    br = new BufferedReader(new InputStreamReader(new FileInputStream(readFile), "GBK"));

    String line;

    out.println("The following is the content which reads from content.txt:");

    while ((line = br.readLine()) != null) {

      out.println(line, "GBK");

    }

  }

  finally {

    if (br != null) {

      br.close();

    }

  }

}

else {

  out.print("File does not exist!");

}

%>

</pre>

<%!

public String getStaticPageId(DRequestItface request, DSessionItface session, DApplicationItface application) {

  return "/test_folder/readfile.dqm";

}

%>

 

At source code line 3 we define the “content.txt” file which need to load, at line 4 we inspect the “content.txt” file whether to exist, it it dose not exist then inform user(line 21), else then output file’s content(line 4 to 19).

At source code line 25 to 29, we override “getStaticPageId” method, that means enable static web page function.

Next we show the content of “content.txt” file:

Shemin Dunne

Hirota, Yoshinobu

James Gao

 

So we visit readfile.dqm, the result is:

The following is the content which reads from content.txt:

Shemin Dunne

Hirota, Yoshinobu

James Gao

 

From the above result, we can see the program correctly output the content. Ok next we start to do the experiment, add a new line into “content.txt”, as follow showed revised content:

Shemin Dunne

Hirota, Yoshinobu

James Gao

Li Dong

 

  Next we visit “readfile.dqm” again, but the result has not changed, why? Because “readfile.dqm” enable static web page function, but the system will not inspect the static page which earlier generated whether already expired, therefore, system will return the static page which earlier generated when visit this dqm again. How can we solve the problem? We can manually delete static web pages, like this system will re-created static web page when we visit dqm file again.

 

  The dqm file “clearStaticPage.dqm” can operate the static page which alread generated, it 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

35

36

37

38

39

40

41

42

 

43

44

45

 

46

47

48

49

50

51

52

<%@page command="true"%>

<%

String password = "123456";

if (request.getParameter("B1") == null) {

%>

 

<form method="POST" action="">

       <p align="center"><b><font size="5">The tool of remove static web pages</font></b></p>

       <p align="center">Operation password: <input type="password" name="psw" size="20"></p>

       <p align="center"><input type="radio" name="system" value="getSize" checked>Return the number of static web pages in current host</p>

       <p align="center"><input type="radio" name="system" value="clearAll">Removes all of the static web page from current host</p>

       <p align="center"><input type="radio" name="system" value="clear">Removes the static page of the specified staticPageId from current host. The specified staticPageId is: <input type="text" name="staticID" size="20"></p>

       <p align="center"><input type="submit" value="Submit" name="B1"><input type="reset" value="Reset" name="B2"></p>

</form>

 

<%}

else {%>

 

<p align="center"><a href="<%=request.getNowUrlFolder() + request.getNowFile().getName()%>">Return</a></p>

 

<%

if (!password.equals(request.getParameter("psw"))) {

  out.println("The operation password is wrong!!");

  return;

}

//

String system = request.getParameter("system");

 

if ("getSize".equals(system)) {

  out.println("How many static web pages in the current host: " + command.getStaticPageCount());

  return;

}

 

if ("clearAll".equals(system)) {

  command.clearAllStaticPage();

  out.println("Removed all static web pages from current host.");

  return;

}

 

if ("clear".equals(system)) {

  if (command.clearStaticPage(request.getParameter("staticID"))) {

    out.println("Removes the specified static web page from current host, successful implementation.");

  }

  else {

    out.println("Removes the specified static web page from current host, successful failure.");

  }

  return;

}

 

%>

 

<%}%>

 

The interface of running the program as follows:

Picture 12-2-1

 

This program has three functions, user can use three buttons to choose the three functions, but must first input the correct operation password. In here password is “123456”, it’s defined by “password” variable (source code line 3), if you want to change password please change this variable’s value.

 

We introduce the first function (source code line: 29 to 32), it can get the quantity of current host's static web pages, this function was used “getStaticPageCount()” method, the result is quantity of registered static web pages in current host after executed this function.

 

If restart the server to ensure that current host does not contain any static web pages, then visit “readfile.dqm” file, next visit “clearStaticPage.dqm” file and use current function to check quantity of static web pages, you will see current has 1 static web page, it’s explained that static web page of “readfile.dqm” has been generated and registered. If this time we visit “readfile.dqm” again then system will directly to return static web page which already generated. User also can try to visit “mpage.dqm” (refer to section 12.1), then will find that quantity of static web page increased.

 

Ok next we introduce the second function (source code line: 34 to 38) it can remove all static web pages from current host, this function was used “clearAllStaticPage()” method.

 

We can try to visit “readfile.dqm” and “mpage.dqm”, then use first function to get current static web pages quantity, next we use current function to remove all static web pages, next if we use first function to get current static web pages quantity again, we will find current host has zero static web page. This shows that all of the static web pages have been deleted. Now if the content of “content.txt” has been revised, then we can use second function to manually remove old static web page.

Sometimes we just want to remove the specified static web page from current host, not need to remove all static web pages, this time we can use “clearStaticPage(String staticPageId)” method. The third function can remove the specified static web page (source code line: 40 to 48). We make a test, first we need to remove all static web pages in current host, then we visit “readfile.dqm” and “mpage.dqm”. Ok this time we can use first function to get quantity of static web pages, next we try ro remove static web page of “readfile.dqm”, only input “/test_folder/readfile.dqm” into “The specified staticPageId” item. Ok finally we use first function to get current quantity of static web page again, will find a static web page was removed.

From the above presentation, we known how to renew the static web page which already expired, but need to manually renew, not automatic. If in the operating process will change the original static web page’s content then need to manually delete that original static web page. For example, the “a.dqm” will need to read datum from database “A” and display these datum. By the way “a.dqm” enable static web page function. The “b.dqm” will modify record of database “A”, then need to add operation of delete a.dqm’s static web page into “b.dqm”. The purpose of the to do this is to generate newest static page of “a.dqm” after modify record of database “A”.

  We can remove the static page by specified staticPageId, but if there are too many static web pages then maybe we can’t remember all static IDs, therefore next we will introduce how to read all static IDs from current host.

 

4.       public String[] getAllStaticID()

Read all static IDs from current host.

Returns: all static IDs from current host.

 

5.       public String getStaticPageFileName(String staticPageId)

Get static web page’s full name by specified staticPageId from current host.

Parameters: Static ID whose associated static web page’s full name is to be returned

Returns: the static web page’s full name by specified static ID. Returns null if static ID not exist.

 

For example, “allStaticID.dqm” will display current host’s all static IDs and corresponding static web page’s name. 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

<%@page command="true"%>

<html>

 

<head>

<meta http-equiv="Content-Language" content="zh-cn">

<meta http-equiv="Content-Type" content="text/html; charset=gb2312">

<title>Static ID</title>

</head>

 

<body>

 

<table border="0" width="100%" id="table1">

       <tr>

              <td bgcolor="#C0C0C0" width="275" align="center"><b>Static ID</b></td>

              <td bgcolor="#C0C0C0" align="center"><b>Name of static web page</b></td>

       </tr>

       <%

       String[] staticIds = command.getAllStaticID();

       for (int i = 0; i < staticIds .length; i++) {

       %>

       <tr>

              <td width="275"><%=staticIds[i]%></td>

              <td><%=command.getStaticPageFileName(staticIds[i])%></td>

       </tr>

       <%}%>

      

</table>

 

</body>

 

</html>

 

First we use “getAllStaticID” method to read all static IDs (source code line 19 to 25), next use static ID to get static web page file name (source code line 23, use “getStaticPageFileName” method). Following is my results of this program:

Picture 12-2-2

 

  From above picture we can seen there are three static ID and corresponding static web page’s name. But please note that static web page filename not include the extension.

  Example, above picture first static web page file name is:

C:\kgserver\webapp\WEB-INF\static_page\c74eee8f52fffe5ecb2456e543117739

  So the actual existence of the static page file should have three, there are:

C:\kgserver\webapp\WEB-INF\static_page\c74eee8f52fffe5ecb2456e543117739.gzip

C:\kgserver\webapp\WEB-INF\static_page\c74eee8f52fffe5ecb2456e543117739. deflate

C:\kgserver\webapp\WEB-INF\static_page\c74eee8f52fffe5ecb2456e543117739.no

  User can find these files from above path, but because server installation directory different so different environmental results will be different.

  Perhaps some users will ask that the usefulness of static web page filename, very simple we can directly delete corresponding three static web pages, it’s effect is same to clearStaticPage(String staticPageId).

 

 

12.3 The flow of static web page

This section we will be presenting the implementation process of static web pages, so users can better understand static web page function. We introduced override “getStaticPageId” method can return the static ID, if static ID which is null then mean disable static web page function. In fact, this has already summarized the static web page function flow, here we will detailed to explain this process again.

 

  When the user requests a dqm file, server first will execute this method:

public String getStaticPageId(

DRequestItface request, DSessionItface session, DApplicationItface application)

  If this method return null then server consider static web page function disabled, so next execute dqm file and output result.

  But if this method not return null then server consider static web page function enabled, so next corresponding flow also changed. First server will inspect the static ID whether already to register, if yes then next inspect the corresponding static web page whether to exist, only these 2 conditions were satisfied then server will directly output the static page. So this is the result of why delete static web page file can let the server re-generate static page.

  If the above two conditions even one not be satisfied then corresponding flow will be changed. At this time server will continue to implement the dqm file which current visited, next output the result, but please note that server will register the static ID and save the result to corresponding three static web pages at same time. Then, when the user visit this dqm again server will directly output the static page by first flow,.

  By the way that all static web pages registration information will be lost after restarted server, so all static web pages will be re-generated and re-registered.

 

 

 

12.4 Matters needing attention

This section we will introduce static web page function matters needing attention.

  In section 5.1 and 5.7 we introduced “redirect” and “option output” function, if the dqm file used these two functions then don’t enable static web page function, otherwise might get incorrect results.

In section 5.4 we introduced “encodeURL” method, if the dqm file used this method then please don’t enable static web page, because maybe URL which in static web page file’s content will with initial sessionID.

If enable static web page function (return to non-null static ID), then even if you have disabled buffer function, server will force to enable the buffer function of current dqm file (buffer function please refer to section 3.2). Another point, even if you have enabled compress function, server will force to disable compress function of current dqm file when first generating static web page files (compress function please refer to section 4.3)

  Under certain circumstances, the performance of static web page function maybe will lower than dqm file to be directly executed, therefore the user must measure it.