In jsp, if you need to process complex data, defining a java method above is a very effective solution, and pushing the responsibility for processing data to this method, then the main process of the jsp page will not be affected. Of course, you can also use js definition methods for processing. js seems to be better at defining such methods to process Foreground Data, however, sometimes garbled characters or errors that cannot find objects may occur during data dumping between jsp and js. Therefore, the less the mutual value between them, the better. Let's take a look at the method for defining a string in jsp: [java] <%! String splitString (String str, int a) {if (str! = Null & str. trim (). length ()> 0 & a> 0) {// get the byte length of the string int length = str. getBytes (). length; // if (str. length () = length) {// if the truncation length is less than the string length, substring. Otherwise, the string if (a <str. length () {return str. substring (0, a) ;}else {return str ;}// contains the Chinese character else {StringBuffer sb = new StringBuffer (); // intercepts the algorithm Traversal string, and monitoring a value for (int I = 0; I <str. length () & a> 0; I ++) {// calculate the length of two Chinese characters. If (str. charAt (I)> = '\ u4e00' & str. charAt (I) <= '\ u9fa5') {// if it is a Chinese character and not the last character, add it. Otherwise, if (a> 1) {sb is not added. append (str. charAt (I); a-= 2;} // It is not a Chinese character but a length} else {sb. append (str. charAt (I); a --;} return sb. toString () ;}} return "incorrect input" ;}%> the function of this method is to input a string and length, and return the truncated string, the length will be measured in the form of two Chinese characters and one letter or number. If the last one is a Chinese character, the Chinese character will be discarded and there will never be a half Chinese character. When defining methods in jsp, pay attention to the following points: 1. Use <%! %> This label is a flag for defining variables or methods in jsp. 2. If you need to use the built-in object out In the method, you must pass it in the parameter list of the method and let the method run out of an IO exception. 3. Do not define the scope of the method. It is used internally. Use the default range directly.