JSP technology in the eyes of a developer (bottom) _jsp programming

Source: Internet
Author: User
Tags html form
Use a custom Label
Although you can embed Java code in a JSP page and execute it on the server side, the JSP also supports the use of custom tags to insert dynamic content, and it has a mechanism for inserting your own HTML-like tags into your JSP pages. In other words, your JSP Web page can generate dynamic content using a simple tag syntax that inserts Java code. But the usefulness of custom labels is not great.

Creating a custom tag is much more complicated than using a simple scriptlet in a JSP Web page, because custom tags take a few steps to connect your Java components to your JSP code. However, custom tags are easy to distribute and reuse. Support for custom tags will be implemented in the JSP creation tool.

A JSP page that generates dynamic content in the following example uses a custom tag. Note that in this example we no longer need to introduce Java classes, declare variables, or write any Java code:

  

<%@ taglib uri= "/tlds/menudb.tld" prefix= "menu"%>

  

Today ' s Menu



  

Lunch



<%@ include file= "lunch_menu.html"%>

  

Our Special of the day



  

  

As we can see from the above statement, this page is obviously much simpler than the previous Scriptlet example because it does not contain initialization objects and executes the appropriate methods. But the JSP page code is only part of the list, and for each custom tag, the following three components are included:

(1) A Web page that contains a custom label, such as a insertcatchofday custom tag, used in the code snippet above. Before using custom labels, the page must specify TAGLIB directive to provide the location of the tag library descriptor (defined for the label). When the custom tag is executed, the page also has a representative definition of one and more tag attributes (such as "meal" in this example) to determine the dynamic content.

(2) Tag Library descriptor. It is an XML file that defines a custom tag and connects it to the tag handler. A tag library descriptor contains the different attributes of the tag, the name of the associated tag handler (location), and other information that the JSP engine needs to process the custom tag.

(2) Tag Handler. It is a Java class that performs operations in conjunction with custom tags. For example, in the insertcatchofday tag above, tag handler is the Java class that performs a database query to get the corresponding menu item.

We've seen a JSP page with custom tags, let's take a look at the other two components.

Tag Handler

Tag handler is a Java class that is similar to a servlet. However, the servlet can execute the servlet interface and be executed by an HTML get or POST request. The tag handler can also perform a label interface (JAVAX.SERVLET.JSP.TAG) and execute when custom tags are processed by the JSP engine.

If the custom tag contains attributes, then tag handler must define these properties and each Get/set method. For example, when defining the tag handler on the Insertcatchofday custom tag above, we must define the "meal" property and the Get and set methods associated with it:

Private String meal = null;

public void Setmeal (String s) {

Meal = s;

}

Public String Getmeal () {

return meal;

}

Tag Library Descriptor

If you're dealing with Java technology all the time without knowing anything about XML, the tag Library descriptor component may look unfamiliar. But you don't have to worry because you don't need to learn a new programming language. The tag library descriptor uses only the tag syntax similar to HTML to define the name and attributes of the custom tag, which is more like defining an object.

The following tag library descriptor defines the insertcatchofday tag. Note that this file defines the name of the custom tag, attributes, and the associated tag handler class:

   -->

  

  

   Insertcatchofday

   Com.sun.CatchOfDayHandler

  

Queries Menu database for the catch of the.

  

  

   Meal

  

  

  

As with the name of a defined property, the tag library descriptor can also define the data type and specify its attributes (whether or not required), and it allows the JSP engine to do some error checking before the tag handler is executed. There are other information, for example, in order to use the JSP creation tool, the library name and version number can also be included in the tag library.

More examples
In the following example, the first example uses an HTTP request object (HttpServletRequest) in the JSP page to determine the version of the user's browser and return the corresponding content from one of the three HTML pages:

   -->

<%@ page language== "java" info= "Example JSP #1"%>

  

  

<%! String agent; %>

<%

Agent = Request.getheader ("user-agent");

if (Agent.startswith ("mozilla/4.0") {

%>

<%--return content for 4.0 browsers--%>

<%@ include file= "ver4.html"%>

<%

}

else if (Agent.startswith ("mozilla/3.0") {

%>

<%--return content for 3.0 browsers--%>

<%@ include file= "ver3.html"%>

<%

}

else {

%>

<%--return content for Other/unknown browsers--%>

<%@ include file= "other.html"%>

<%

}

%>

  

  

Note: This page can be accessed directly without declaring or initializing the HTTP request object. Both request and response (HttpServletResponse) objects can be implicitly used in JSP pages. Like the servlet, JSP pages can use the request object to obtain parameter values from an HTML form.

   -->

<%@ page language= "java" info= "Example JSP #2"%>

  

  

<%@ include file= "header.html"%>

   -->

<%! String selections[], info; %>

  

Here are your current selections:



<%

   -->

Selections = Request.getparametervalues ("items");

if (selections!= null) {

%>


      <%

      for (int x = 0; x < selections.length/x + +) {

      %>


    • <%= Selections[x]%>: <%= db.getinfo (selections[x))%>

      <%

      }

      %>
<%

}

else {

%>

  

(No items selected)



<%

}

%>

  


<%@ include file= "footer.html"%>

  

  

In this example, when each parameter value is read, the JavaBean component queries the required information. Using a bean in a JSP Web page is an easy way to return dynamic Web content from the database.

Conclusion
If you're looking for a way to easily build a Web program that connects to a server-side Java component, then JavaServer page is exactly what you need. Apart from Ejb,rmi,jdbc and JavaBean, separating HTML representations and Web programs makes it easier for JSP pages to be organized and run. In fact, because web designers can build JSP pages with little help from Java developers, you're no longer worried about creating Web pages and writing HTML code.

< finished full >

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.