Jsp forward action tag

Source: Internet
Author: User

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:


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.