Examples of defining and using methods in JSP introduce _JSP programming

Source: Internet
Author: User
Tags stringbuffer
In JSP, if you need to work with complex data, defining a Java method above is a very effective solution, pushing the responsibility for processing data to the method, and then the mainstream of the JSP page will not be affected. Of course, you can also use the JS definition method to deal with, JS seems to be better at defining such a method to deal with the foreground data, but sometimes JSP and JS between the data will be garbled or reported some errors can not find the object, so the mutual transfer between their values or the less the better.

look at the way the JSP defines a string processing:
Copy Code code as follows:

<%!
String splitstring (string str, int a) {
if (str!= null && str.trim (). Length () > 0 && a > 0) {
Gets the byte length of the string
int length = Str.getbytes (). length;
All excluding Chinese characters
if (str.length () = length) {
If the intercept length is within the length of the string, it is substring, otherwise the string is taken
if (A < Str.length ()) {
Return str.substring (0, a);
} else {
return str;
}
}//contains Chinese characters
else {
StringBuffer sb = new StringBuffer ();
The Intercept algorithm traverses the string and monitors a value
for (int i = 0; i < str.length () && a > 0; i++) {
If the Chinese character is counted 2 lengths.
if (Str.charat (i) >= ' \u4e00 ' && str.charat (i) <= ' \u9fa5 ') {
If it is a Chinese character and is not the last character, add it or not
if (a > 1) {
Sb.append (Str.charat (i));
A-= 2;
}
Not Chinese characters, only one length.
} else {
Sb.append (Str.charat (i));
a--;
}
}
return sb.tostring ();
}
}
Return "input error";
}

%>

The function of the method is to pass in a string and a length, and return the intercepted string, the length will be according to the Chinese characters two, alphanumeric one way to statistics, if the last one is Chinese characters, then the Chinese character will give up, must not appear half of Chinese characters.

To define methods in the JSP, you need to be aware of the following points:

1, need to use <%! %> Such labels are wrapped, which are flags that define variables or methods in the JSP.

2, if you need to use a built-in object out in a method, be sure to pass it in the parameter list of the method, and let the method run out of an IO exception.

3, the scope of the method does not define, anyway is internal use, direct use of the default range can be.
Related Article

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.