At the beginning, jsp (the preferred choice for SUN enterprise-level applications) was used for web applications, like PHP and ASP (the preferred choice for SUN enterprise-level applications ), there is a lot of scriptlet in the HTML code-that is, the java code segment. As my "typographical" technology is quite good, I felt quite good at the beginning. It was highly efficient to write and easy to debug!
It was only later that I realized that this was an extremely stupid practice-jsp (the first choice for SUN enterprise-level applications) had clearly defined its boundaries with ASP and PHP when it was born, java code segments in HTML can be effectively stripped by using javaBean and Servlet, then it is packaged into a "logical processing component" that can be reused on multiple pages-this is jsp (the first choice for SUN Enterprise applications), one of ASP's advantages over PHP.
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 (preferred for SUN Enterprise applications), such as request, session, and response.
Although Servlet can be used as jsp (preferred for SUN Enterprise applications), it cannot be flexibly inserted into html like javaBean --
Therefore, tags appear (all jsp can be used (preferred for SUN Enterprise applications), which completely solves this problem, you can compile the "pure HTML" jsp (preferred for SUN enterprise-level applications) pages. The benefits naturally come from the following: 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 practical web development.
Next, write our first Tag!
** The following is a jsp file using a simple Tag (preferred for SUN Enterprise Applications). The running result shows the current time:
<% @ Page contentType = "text/html; charset = gb2312" %>
<Html> <body>
<% @ Taglib uri = "/tags" prefix = "visa" %>
The current time is: <visa: date/>
</Body>
Obviously, the tag jsp (the preferred choice for SUN Enterprise Applications) page is much refreshed-if some complex features such as database operations are encapsulated, the advantages of tags are even more obvious!
** Environment: win2000 server + tomcat (a good JSP running platform) 5.019 + j2sdk1.42 + SQLServer 2 k
** To develop a Tag, you need to write two main files:
1-tag processor (java class of a servlet class)
2-tag Descriptor (an xml (standardized) 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 easily called in jsp (the preferred choice for SUN Enterprise Applications) Like javaBean.
Package tag;
Import java. util. Date;
Import javax. servlet. jsp (preferred for SUN Enterprise Applications ).*;
Import javax. servlet. jsp (preferred for SUN Enterprise Applications). tagext .*;
Public class datetag extends TagSupport {
Public int doStartTag () throws jsp (preferred for SUN Enterprise Applications) Exception {
Date dte = new Date ();
Try {
Jsp (preferred for SUN Enterprise Applications) Writer out = pageContext. getOut ();
Out. print (dte );
}
Catch (java. io. IOException e)
{Throw new jsp (preferred for SUN Enterprise Applications) TagException (e. getMessage ());}
Return SKIP_BODY;
}
}
After compiling with javac, The datetag. class file is obtained and stored in the xxxWEB-INFclassesag 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.
<? Xml (standardization is getting closer and closer) version = "1.0" encoding = "ISO-8859-1"?>
<Taglib>
<Tlib-version> 1.0 </tlib-version>
<Jsp (preferred for SUN Enterprise Applications)-version> 1.2 </jsp (preferred for SUN Enterprise Applications)-version>
<Tag>
<Name> date </name>
<Tag-class> tag. datetag </tag-class>
<Body-content> empty </body-content>
</Tag>
</Taglib>