Forward action Tag:
<Jsp: forward page = "page to be switched">
</Jsp: forward>
Or
<Jsp: forward page = "page to be switched"/>
The purpose of this command is to stop the execution of the current page from this command and switch to another JSP page.
In the JSP page in the following example, you first obtain a random number. If the number is greater than 0.5, the system switches to example2.jsp. Otherwise, the system switches to fileoutput. jsp.
<% @ Page contentType = "text/html; charset = GB2312" %> <HTML> <BODY> <% double I = Math. random (); if (I> 0.5) {%> <jsp: forward page = "example2.jsp"/><%} else {%> <jsp: forward page = "fileoutput. jsp "/> <% }%> <p> can this sentence be output with the value of the following expression? <% = I %> </BODY> </HTML>
Example2.jsp
<% @ Page contentType = "text/html; charset = GB2312" %> <HTML> <body bgcolor = cyan> <FONT Size = 4> <%! Int I = 0; %> <% I ++; %> <p> you are the customer accessing this site. <P> </FONT> </BODY> </HTML>
Fileoutput. jsp
<% @ Page contentType = "text/html; charset = GB2312" %> <% @ page import = "java. io. * "%> <HTML> <body bgcolor = cyan> <FONT Size = 4> <%! Int number = 0; File file = new File ("count.txt"); synchronized void countPeople () {if (! File. exists () {number ++; try {file. createNewFile (); FileOutputStream out = new FileOutputStream ("count.txt"); DataOutputStream dataOut = new DataOutputStream (out); dataOut. writeInt (number); out. close (); dataOut. close () ;}catch (IOException ee) {}} else {try {FileInputStream in = new FileInputStream ("count.txt"); DataInputStream dataIn = new DataInputStream (in ); number = dataIn. readInt (); number ++; in. close (); DataIn. close (); FileOutputStream out = new FileOutputStream ("count.txt"); DataOutputStream dataOut = new DataOutputStream (out); dataOut. writeInt (number); out. close (); dataOut. close () ;}catch (IOException ee) {}}%> <% countPeople (); %> <p> you are the <% = number %> customer accessing this site. <P> </FONT> </BODY> </HTML>Running effect: