In a traditional JSP program, we write HTML code with Java code together, which is convenient, but also leads to difficult to maintain, HTML developers and JSP developers are aggravated, we can make this tradition technology Page pull data technology. How can I achieve HTML development and JSP development? The answer is to use TAG technology. By using TAG technology, we can do not appear in the page program, where you need data, you will first set the label, then replace these tags, display data. I call this technology called to push data, and the page will only be defined. In this way, we can make HTML developers focus on the appearance of the page, while Java programmers do not need to collect page display, focus on background procedures, greatly improve programmability and convenience. Easy to develop cooperative development between programs. First you have to understand some TAG technology, then you can read this article.
The following is the sample program: First, the first is the Replace function // replace the string function // string strsource - source string // string strfrom - Subtrive // String STRTO - Replace the string Public Static String Replace (String strsource, string strfrom, string string) {// If the sub-string to be replaced is empty, then return the source string IF (strFrom == null || strfrom.equals (")) Return STRSOURCE; String strDest = ""; // to replace the substring length INT INTFromlen = strfrom.length (); int idos; // cycle replacement string while ((intpos = strsource.indexof (strfrom))! = -1) { / / Get the left s string strDest = strDest strsource.substring (0, intpos); // plus the replacement substring strDest = strDest strdo; // Modify the source of the slave String strsource = strsource.substring (intpos intFromlen);} / / Plus no matching substrings strDest = strDest strsource; // Return Return strDest;} II. TLD (MyBookTag.TLD) Define your label XML Version = "1.0" encoding = "ISO-8859-1">
import javax.servlet.jsp.JspWriter; import javax.servlet.ServletRequest; public class ListBookTag extends BodyTagSupport {// mark start position performed public int doStartTag () {return EVAL_BODY_BUFFERED;} // end position flag perform public int doEndTag () throws JSptagexception {int max = 0; string listbody = null; int number = 1; // Get the page information information, which is the content in the Request object string serialno = pageContext.getRequest (). GetParameter ("serialno); // Convert to Integer try {number = integer.parseint (serialno);} catch (exception e) {number = 1;} if (Number <1) Number = 1; // Get the data set saved in the session, of course, here can also Data BookVector = (Vector) PageContext.getSession (). GetAttribute ("BookVector"); if (Number * 10 // Out of Try {pageContext.get (). Print (body);}}}}}}}}}}}}}} 4, JSP page (BookList.jsp) <% @ Page ContentType = Text / html; charSet = GBK "%> <% @ Taglib URI =" / mybooktag "prefix =" MyBookTag "%>