Step for customizing JSP labels

Source: Internet
Author: User
Tags tld

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

 
 
  1. packagegetdata;  
  2. importjava.io.*;  
  3. importjavax.servlet.jsp.*;  
  4. importjavax.servlet.jsp.tagext.*;  
  5. importjava.sql.*;  
  6. publicclassDatatagextendsTagSupport  
  7. {  
  8. publicintdoStartTag()throwsJspException  
  9. {  
  10. ResultSetrs=(ResultSet)this.pageContext.getSession().getAttribute
    ("resultSet");  
  11. try{  
  12. JspWriterout=pageContext.popBody();  
  13. while(rs.next())  
  14. {  
  15. out.println("<tr>");  
  16. out.println("<form>");  
  17. out.println("<tdaligntdalign='center'><inputtypeinputtype='checkbox' 
  18. name='checkbox'value='checkbox'></td>");  
  19. out.println("<td>"+rs.getString("receiver")+"</td>");  
  20. out.println("<td><ahrefahref=showdatail.jsp>"+rs.getString("title")
    +"</a></td>");  
  21. out.println("<td><ahrefahref=upload/"+rs.getString("annex")+">"+rs
    .getString("annex")+"</a></td>");  
  22. out.println("<td>"+rs.getString("date")+"</td>");  
  23. out.println("</form>");  
  24. out.println("</tr>");  
  25. }  
  26. }  
  27. catch(Exceptionex)  
  28. {  
  29. ex.printStackTrace();  
  30. }  
  31. returnSKIP_BODY;  
  32. }  
  33. publicintdoEndTag()throwsJspException  
  34. {  
  35. returnEVAL_PAGE;  
  36. }  

Data. tld

 
 
  1. < ?xml version="1.0" encoding="UTF-8"?> 
  2. < !DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN"  
  3. "http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd"> 
  4. < taglib> 
  5. < tlib-version>1.0< /tlib-version> 
  6. < jsp-version>1.1< /jsp-version> 
  7. < tag> 
  8. < name>result< /name> 
  9. < tag-class>getdata.Datatag< /tag-class> 
  10. < body-content>jsp< /body-content> 
  11. < /tag> 
  12. < /taglib> 

Web. xml

 
 
  1. < taglib> 
  2. < taglib-uri>/WEB-INF/data.tld< /taglib-uri> 
  3. < taglib-location>/WEB-INF/data.tld< /taglib-location> 
  4. < /taglib> 

DisplayAction. do

 
 
  1. String strSql=new String("select * from yonghu");  
  2. HttpSession session=httpServletRequest.getSession();  
  3. session.setAttribute("strSql",strSql) ;  
  4. return actionMapping.findForward("success") ; 

If you are not using struts, you can use other methods to implement this step!

Display. JSP

 
 
  1. < %@ page contentType="text/html; charset=GBK" %> 
  2. < %@ taglib uri="/WEB-INF/data.tld" prefix="app" %> 
  3. < html> 
  4. < head> 
  5. < title> 
  6. display  
  7. < /title> 
  8. < /head> 
  9. < body bgcolor="#ffffff"> 
  10. < app:result /> 
  11. < /body> 
  12. < /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.

  1. Get database connection in JSP
  2. Introduction to JSP Action
  3. Simplified code in JSP expressions
  4. Detailed explanation of JSP-to-Servlet Conversion
  5. Introduction to JSP Elements

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.