Write "Pure HTML" JSP application: Learn to use JSTL

Source: Internet
Author: User
Tags date current time functions html page reference version tld tomcat
Js

Just started using JSP to do Web applications, like using php,asp as with JSP, in the HTML code mixed with a lot of scriptlet---that is, Java code Snippets. Because of my "typesetting" technology can also be, at the beginning feel very good, write "efficiency" high, debugging and convenient!

And then we know---it's a very stupid thing to do.----JSP was born when the clear boundaries with asp,php, using JavaBean, servlet can effectively remove HTML inclusions in the Java code segment, It is then packaged as a "logical processing component" that can be reused on multiple pages---this is one of the advantages of JSP relative to php,asp.

But sometimes, even with the JavaBean + servlet, we have to embed "very little" Java code into HTML----Yes, sometimes you have to: because in JavaBean, you can't use the hidden objects in the JSP, such as request, Session,response and so on.

Using the servlet, although you can use the objects of a JSP, you can't easily insert into HTML like JavaBean--
So, tag (tag) appears (you can use all the JSP suppressed objects), its presence completely solves this problem, can let you write "pure HTML" JSP page number---the benefit of this naturally self-evident: higher maintainability, higher component reuse efficiency, Easier to maintain HTML page ' '

Little brother, just began to learn jstl, think this dongdong really very good! Want to let more beginners know this, and can apply to the actual web development.

Now, let's start writing our first tag!.

* * Below is a JSP file with a simple tag, and the result is the current time:

<%@ page contenttype= "text/html;charset=gb2312"%>
<% @taglib uri= "/tags" prefix= "Visa"%>
Now time is:<visa:date/>.
</body>

It is obvious that the use of the tag JSP page refreshing a lot of---if the database operation, such as some complex functions also encapsulated in, the advantages of the tag is more obvious!

* * Environment: Win2000 Server + Tomcat5.019 + j2sdk1.42 + SQL Server 2k
* * To develop a tag, you need to write 2 main files:
1-label Processor (Java class for a class servlet)
2-Tag descriptor (an XML-style TLD file)
By completing these two files, you can deploy and apply them in a Web application.

OK, let's do it now!

  1-Write Tag processor: Datetag.java

It acts like a servlet, accepting requests from clients, but it can be easily invoked in a JSP like JavaBean.

Package tag;

Import Java.util.Date;
Import javax.servlet.jsp.*;
Import javax.servlet.jsp.tagext.*;

public class DateTag extends tagsupport{

public int doStartTag () throws jspexception{
Date Dte=new date ();
try{
JspWriter out=pagecontext.getout ();
Out.print (DTE);
}
catch (Java.io.IOException e)
{throw new Jsptagexception (E.getmessage ());}
return skip_body;
}
}

After compiling with Javac, you get the Datetag.class file and place it in the Xxx\web-inf\classes\tag directory.

  2-Write Tag library descriptor: TAGS.TLD

It's easy to see that the,<tag></tag> part is a bit like the servlet mapping configuration--it's configured with the mapping between tag's name and the tag class.

<?xml version= "1.0" encoding= "Iso-8859-1"?>
<taglib>
<tlib-version>1.0</tlib-version>
<jsp-version>1.2</jsp-version>

<tag>
<name>date</name>
<tag-class>tag.datetag</tag-class>
<body-content>empty</body-content>
</tag>
</taglib>

Save the Tags.tld in the xxx\web-inf\ directory.

  3-Configure Your Web.xml:

Configure Web.xml to register your tag library: add between Web.xml <web-app> and </web-app>:
Register your custom tag here, the reference in the JSP is named:/tags

<taglib>

<taglib-uri>/tags</taglib-uri>
<taglib-location>/WEB-INF/tags.tld</taglib-location>

</taglib>

  4-Start using in JSP!

Because it has been registered in the Web.xml, through the/tags reference to your tag library;
The role of prefix is like the ID in <jsp:useBean/>, just as a sign (can be defined arbitrarily)
<visa:date/> obviously, by calling date, it's equal to calling Datetag.class:

<%@ page contenttype= "text/html;charset=gb2312"%>
<% @taglib uri= "/tags" prefix= "Visa"%>
Now time is:<visa:date/>.
</body>


* * believe that here, someone has the question: in order to use a tag, I have done so much "redundant" work (write TLD, modify Web.xml, but also restart Tomcat), it is worth it?!

  ---answer is: it's worth it!

1, if the more complex logic functions packaged into the tag, it has more flexibility than servlet,javabean, more advantages, easier to expand, easier to maintain---completely separate the presentation layer and logic layer!

2, because the function of tag is not just this, there are more advanced features---worth learning!

****
This is just a very simple application, of course, you will have a lot of "confusing point", which is normal---there are some unique characteristics in the tag, want to learn to tag completely is not a simple thing.
-So, here, I recommend a good book: the Electronic Industry publishing house "JSP logo Library Programming Guide" (English Name: "Professional JSP Tag Libraries")


PS: Use JSTL with Javabean,servlet can also effectively protect your source code OH----because you can deliver a Web application without Java source code to your customers because all of the Java code has been compiled into *.class * ^_^ *

  [note]: for a version----Tomcat 5.0x: Tomcat 4.0x: To compile the label processor, you must put your Servlet.jar (under% tomcat_home%\common\lib) into the environment variable The---in the CLASSPATH (if the version above is tomcat5.0x, as if not required)---otherwise, a compilation error is prompted.



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.