Use the tag function to write JSP applications (JSTL)

Source: Internet
Author: User
Tags tld

When I first started using JSP for web applications, JSP is used like PHP and ASP, and a lot of scriptlet is included in HTML code. Java code segment. As my "typographical" technology is still acceptable, I thought this JSP application was quite good at the beginning, and the compilation was "efficient" and debugging was convenient!

It was only later known that this was an extremely stupid practice. When JSP was born, it was already clear that it had a line with ASP and PHP. Using javaBean and Servlet can effectively strip the java code segments mixed in HTML, then it is packaged into a "logical processing component" that can be reused on multiple pages ". This is one of the advantages of JSP Over PHP and ASP.

But sometimes even if we use javabean + servlet, We have to embed a very small amount of java code into HTML-indeed, sometimes you have to do this: Because in javabean, you cannot use implicit objects in JSP, such as request, session, and response.

Although JSP objects can be used with Servlets, they cannot be flexibly inserted into html like javaBean.

Therefore, tags can use all JSP implicit objects. The appearance of tags completely solves this problem, this allows you to compile JSP pages of "pure HTML". The benefits are self-evident: Higher maintainability, higher Component Reuse efficiency, and easier maintenance of HTML pages.

I just started to learn JSTL. I think this stuff is really good! I would like to let more beginners know this and apply it to actual JSP application development.
Next, write our first Tag!

** The following is a JSP file using a simple Tag. The running result shows the current time:

 
 
  1. <% @ PageContentType="Text/html; charset = gb2312"%> 
  2. < Html>< Body> 
  3. <% @ TaglibUri="/Tags" Prefix="Visa"%> 
  4. The current time is:< Visa: date/> 
  5. </Body></Html> 

Obviously, the JSP pages using tags are much refreshed-if some complex features such as database operations are encapsulated, the advantages of tags are even more obvious!

** Environment: win2000 server + Tomcat5.019 + j2sdk1.42 + SQLServer 2 k
** To develop a Tag, you need to write two main files:
1-The tag processor is a java class of servlet)
2-tag descriptor: an XML tld file)
After these two files are completed, you can deploy and use them in WEB applications.

Now, let's get started!

1-tag Processor: datetag. java

It acts like a Servlet that accepts requests from the client, but it can be conveniently called in JSP like a javaBean.

 
 
  1. package tag;  
  2.  
  3. import java.util.Date;  
  4. import javax.servlet.jsp.*;  
  5. import javax.servlet.jsp.tagext.*;  
  6.  
  7. public class datetag extends TagSupport{  
  8.  
  9.  public int doStartTag() throws JspException{  
  10.   Date dte=new Date();  
  11.   try{  
  12.   JspWriter out=pageContext.getOut();  
  13.   out.print(dte);  
  14.    }  
  15.    catch(java.io.IOException e)  
  16.    {throw new JspTagException(e.getMessage());}  
  17.  return SKIP_BODY;  
  18. }  
  19. }  

After compiling with javac, we get the datetag. class file, put it in the xxx \ WEB-INF \ classes \ tag directory.

2-compile the tag library descriptor: tags. tld

It is easy to see that the <tag> </tag> part is somewhat like the servlet mapping configuration-the tag ing between the tag name and the tag class is configured here.

 
 
  1. < ?xml version="1.0" encoding="ISO-8859-1"?> 
  2. < taglib> 
  3. < tlib-version>1.0< /tlib-version> 
  4. < jsp-version>1.2< /jsp-version> 
  5.  
  6. < tag> 
  7.  < name>date< /name> 
  8.  < tag-class>tag.datetag< /tag-class> 
  9.  < body-content>empty< /body-content> 
  10. < /tag> 
  11. < /taglib> 
  12.  

Save tags. tld in the xxx \ WEB-INF \ directory.

3-configure your web. xml:

Configure web. xml and register your tag Library: Add the following content between <web-app> and </web-app> in web. xml:
Register your custom tag here. The reference name in JSP is/tags.

 
 
  1. < taglib> 
  2.  
  3. < taglib-uri>/tags< /taglib-uri> 
  4. < taglib-location>/WEB-INF/tags.tld< /taglib-location> 
  5.  
  6. < /taglib> 

4-Start Using JSP applications!

Because you have already registered in web. xml, use/tags to reference your tag library;
The prefix function is like the id in <jsp: useBean/>, which can be defined as a flag)
<Visa: date/> obviously, calling date is equivalent to calling datetag. class:

 
 
  1. <% @ PageContentType="Text/html; charset = gb2312"%> 
  2. < Html>< Body> 
  3. <% @ TaglibUri="/Tags" Prefix="Visa"%> 
  4. The current time is:< Visa: date/> 
  5. </Body></Html> 

** Believe this, some people have doubts: In order to use a Tag, I have done so much "extra" work to write a tld and modify the web. xml, restart tomcat), is it worth it ?!

--- The answer is: this is worth it!

1. If complicated logic functions are encapsulated into tags, they have higher flexibility, more advantages, and more scalability than Servlet and javaBean, easier maintenance --- completely separating the presentation layer from the logic layer!

2. Because the Tag functions are not just those, there are more advanced functions-it is worth learning!

****

This is just a very simple application. Of course, you will have a lot of "confusions". This is normal-there are some unique features in TAG, it is not easy to fully learn tags, but it will save a lot of time for JSP application development.

-- So here, I would like to recommend a good book: "jsp sign library programming guide" by the e-Industry Press; English name: "Professional JSP Tag Libraries")

PS: Using JSTL with javabean, servlet can also effectively protect your source code ---- because, you can deliver a web application without java source code to your customers, because, all java code has been compiled *. class * ^_^ *

[Note]: for versions earlier than Tomcat 5.0x-for example, Tomcat 4.0x: To compile the tag processor, you must upload your servlet. jar (under % atat_home % \ common \ lib) is put into the environment variable CLASSPATH --- (if it is a tomcat5.0x or later version, it does not seem to have this requirement) --- otherwise, a compilation error is prompted.

  1. JSP line feed solution
  2. Simplified code in JSP expressions
  3. Analysis of JSP server
  4. Monitor JVM available memory in JSP
  5. JSP Init and Destory functions improve work efficiency

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.