JSP simple exercise-a simple counter and jsp exercise counter

Source: Internet
Author: User

JSP simple exercise-a simple counter and jsp exercise counter

In JSP, the program code written between "<%" and "%>" becomes a java program piece.

A JSP page can contain multiple java program slices. Note that the variables declared in the Java program are valid in the program and expressions used on the JSP page where they are located. Based on this, you can divide a large program into several small ones, and insert HTML statements into the program to make the program code more readable.

Variables declared in the program are only valid on the page and are local variables. They cannot be shared when different customers access the same page. However, if it is in "<%! Variables declared between "and" %> "can be shared among different customers. The valid range is the entire Web application. After the server is disabled, the variables will be released.

"<% =" And "%>" can be used to directly output the value of a variable or expression. The value of a variable or expression will be output as a string in the browser. This method is commonly used in JSP programming, especially when mixed with HTML tags. The following code:

<% @ Page contentType = "text/html; charset = gb2312" %> This program first runs in "<%! A counter variable and counting method are declared between "and" %> ". The counter variable will be shared among customers until the server is closed. The counting method is actually adding 1 to the counting variable. The sychronized keyword is added before the method. This keyword can prevent conflict when the customer calls this method to change the value of the counting variable at the same time, therefore, the method is serialized.



[Jsp] uses session to create a simple counter, but there are still problems ......

Request. getSession (true); this sentence is useless. You 'd better do this:

Object rValue = session. getValue ("usercount ");
If (rValue = null)
{
Count = 1;
Usercount = String. valueOf (count );
Session. putValue ("usercount", usercount );
}
Out. print ("you are here" + rValue. toString ());

JSP uses the getparameter () method to compile a simple counter and compute the continuous sum, square, and cubic operations from 1 to n.

This is saved as a jsp file. I passed the test under tomcat.

<% @ Page language = "java" import = "java. util. *" pageEncoding = "GBK" %>
<%
String path = request. getContextPath ();
String basePath = request. getScheme () + "://"
+ Request. getServerName () + ":" + request. getServerPort ()
+ Path + "/";
%>

<! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN">
<Html>
<Head>
<%
Int count = 0;
Int num = 0;
If (request. getParameter ("count ")! = Null ){
Count = Integer. parseInt (request. getParameter ("count "));
Count ++;
}
If (request. getParameter ("num ")! = Null ){
Num = Integer. parseInt (request. getParameter ("num "));
}
Num = num * num; // replace other algorithms here.
%>
</Head>

<Body>
<Center>
<Form name = "form1" method = "post">
<Table>
<Tr>
Enter a number:
</Tr>
<Tr>

<Input name = "num" type = "text" value = "<% = num %>"/>
</Tr>
<Tr>

<Input type = "submit" value = "cube"/>
</Tr>
<Tr>
<Td>
Count:
</Td>
<Td>

<Input name = "count" type = "text" value = "<% = count %>"/>
</Td>
</Tr>
</Table>

</Form>
</Body>
</Html>... the remaining full text>

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.