To customize a JSP tag, follow these steps:
Create Tag Processor
Create a TLD File
Edit web. xml
Use JSP custom tags on the JSP page
Here we mainly discuss and learn not how to develop custom tags, so we can find the specific development examples Baidu or google and will not repeat them here.
We have used custom tags. Perhaps the greatest experience is their complexity. However, the following usage changes our understanding of tags (the example introduces the struts version)
In a web application, save the database query result set ResultSet rs to the session or reueest range, and pass it to the JSP page for display. This is okay, but when there are too many users to query at the same time, the pressure on the server is very high. If we use a JSP custom tag and return the result set to the JSP page, the situation will be different. The specific implementation is as follows:
Datatag. java
- packagegetdata;
- importjava.io.*;
- importjavax.servlet.jsp.*;
- importjavax.servlet.jsp.tagext.*;
- importjava.sql.*;
- publicclassDatatagextendsTagSupport
- {
- publicintdoStartTag()throwsJspException
- {
- ResultSetrs=(ResultSet)this.pageContext.getSession().getAttribute
("resultSet");
- try{
- JspWriterout=pageContext.popBody();
- while(rs.next())
- {
- out.println("<tr>");
- out.println("<form>");
- out.println("<tdaligntdalign='center'><inputtypeinputtype='checkbox'
- name='checkbox'value='checkbox'></td>");
- out.println("<td>"+rs.getString("receiver")+"</td>");
- out.println("<td><ahrefahref=showdatail.jsp>"+rs.getString("title")
+"</a></td>");
- out.println("<td><ahrefahref=upload/"+rs.getString("annex")+">"+rs
.getString("annex")+"</a></td>");
- out.println("<td>"+rs.getString("date")+"</td>");
- out.println("</form>");
- out.println("</tr>");
- }
- }
- catch(Exceptionex)
- {
- ex.printStackTrace();
- }
- returnSKIP_BODY;
- }
- publicintdoEndTag()throwsJspException
- {
- returnEVAL_PAGE;
- }
- }
Data. tld
- < ?xml version="1.0" encoding="UTF-8"?>
- < !DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN"
- "http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd">
- < taglib>
- < tlib-version>1.0< /tlib-version>
- < jsp-version>1.1< /jsp-version>
- < tag>
- < name>result< /name>
- < tag-class>getdata.Datatag< /tag-class>
- < body-content>jsp< /body-content>
- < /tag>
- < /taglib>
Web. xml
- < taglib>
- < taglib-uri>/WEB-INF/data.tld< /taglib-uri>
- < taglib-location>/WEB-INF/data.tld< /taglib-location>
- < /taglib>
DisplayAction. do
- String strSql=new String("select * from yonghu");
- HttpSession session=httpServletRequest.getSession();
- session.setAttribute("strSql",strSql) ;
- return actionMapping.findForward("success") ;
If you are not using struts, you can use other methods to implement this step!
Display. JSP
- < %@ page contentType="text/html; charset=GBK" %>
- < %@ taglib uri="/WEB-INF/data.tld" prefix="app" %>
- < html>
- < head>
- < title>
- display
- < /title>
- < /head>
- < body bgcolor="#ffffff">
- < app:result />
- < /body>
- < /html>
In this way, you only need to call <app: result/> to output the query result in each JSP!
The above code is not necessarily correct, but the idea is very clear! In large projects, this mode is highly respected! It embodies the idea of java code reuse, and makes operations related to result sets not directly exposed to users, but also achieves security.
- Get database connection in JSP
- Introduction to JSP Action
- Simplified code in JSP expressions
- Detailed explanation of JSP-to-Servlet Conversion
- Introduction to JSP Elements