"Head First Servlets & JSP" Use Jstl

Source: Internet
Author: User
Tags session id tld

Instructions for installing JSTL1.1

JSTL1.1 is not part of the JSP2.0 specification, access to the servlet and JSP APIs does not imply access to jstl.
Before using JSTL, you need to install the Jstl.jar file into the Web-inf/lib directory of your web app, where each web app requires a copy of Jstl.

Do not use scripts to implement a loop--c:foreach

Servlet Code:

    • It's not good to use scripts to implement
      To display each of these elements in a JSP page, use the script as follows:
    • Implemented with Jstl C:foreach
      The C:foreach tag provides an easy way to iterate over arrays and collections, such as collection, map, or string separated by commas.
      Then the JSP page code becomes:

      You can even use the optional Varstatus property to get the loop counter:

      Varstatus creates a new variable that holds an instance of Javax.servlet.jsp.jstl.core.loopTagStatus, and the Looptagstatus class has a Count property, all in the JSTL specification document.

      The C:foreach tag can also be nested, such as a list in which several arrays are saved, when the elements in these arrays are traversed ....

Use the C:IF completion criteria to include

For a comment page, only members can participate in comments, and non-members can only view comments.
Comment List (commentlist.jsp):

Participation in the review (inputcomments.jsp):

With the Else judgment: C:choose and its little partners C:when and C:otherwise


After all, C:if has no else, although it can be used to deal with multiple if, but it is very difficult to see, moreover, there is no default choice (that is, the role of otherwise).

    • Part of the code in the JSP:

      Unlike switch in Java, C:choose can only run one of the branches (including when and otherwise), and switch may run multiple branches when a break is missing.

property setting C:set Tag

Since the Jsp:setproperty tag can only set the nature of the bean, Jstl's c:set can assign values to beans, maps, variables, and more.

    • Set the attribute variable var with c:set

      Note that the value evaluates to null and the variable is deleted. If there is a variable property named Fido, and ${person.dog} evaluates to NULL, the Fido property is deleted, even if scope is not specified, and the fido! is found and deleted sequentially in the page scope, request scope, session scope, context scope, and so on.

    • Use C:set for beans and maps

      Target must be counted as an Object! Instead of a string representing the name of the property, the direct amount.
      This means that target needs an El expression (above) or a script expression (<%=%>), or Jsp:attribute.

C:remove to delete a property

Although C:set can delete a property when the value is null, the property is specifically deleted with C:remove:

The third way to reuse pages--c:import
    • Review the first two ways
      First approach: Include directives
      <%@ include file="Header.html"%>It is usually a static layout template, such as an HTML page, so use the file property.
      Second approach: Include standard actions
      <jsp:include page="Header.jsp">JSP is generally dynamic content, so use the page property.
    • The third way: Jstl's C:import Mark
      <c:import url="http://www.google.com" />Here is the URL property.
      Unlike the first two, it can be out of the scope of the current container!
    • Contains personalization parameters C:param
About session Tracking: URL rewriting/Encoding--c:url

The session trace in the JSP occurs automatically (unless the session property is specified as session= "false"). However, when the customer does not support cookies, the way to rewrite the URL is to add the session ID to the URL.

    • Previously, URL rewriting in the servlet was like this
    • You can do the same thing in JSP.
    • URL encoding--c:param
      The rewrite procedure above has no special symbols, so URL encoding is not necessary. However, if you include a query string, you are not likely to have a special symbol, like this:

      Then use C:param in the C:url body to complete the encoding:

      This allows you to view the encoded URL:
      ${inputURL}
Create your own error page
    • The specified error page errorpage.jsp
    • The bad page badpage.jsp will throw an exception:

      When accessing badpage.jsp, it jumps to errorpage.jsp because it throws an exception.
    • The error-page tag in DD allows you to specify a variety of error pages for the entire Web application
      A normal error page for DD configuration:

      More explicit Exception declaration DD configuration:

      Or, the error page is declared according to the HTTP status code:

      The error-page in DD is global, and if a JSP page specifies an explicit errorpage page directive, then the container takes precedence over the page instruction for error pages.

An additional implicit object for the error page: exception

You can use the implicit object exception in the error page if you want to give the user an error message (which is usually not prompted):

Normal pages do not have implicit object exception, only pages that explicitly use the iserrorpage= "ture" property in the page directive have this implicit object.

Want to catch an exception yourself rather than throw an error--c:catch
    • Catching exceptions using C:catch tags
    • The exception you catch is accessible, as long as you set an exception name. Even if it is not an exception page (iserrorpage= "Ture"):
    • As with the try block, after an exception occurs, the remainder of the C:catch body is no longer running
Using a non-jstl tag library--Custom Library

If you want to use a custom library, you must read the TLD. Of course, this is just the use of it, if the development of a custom library (that is, the development of supporting markup Java code), not the task here.

    • Understanding TLD
      The following TLD file describes a tag: advice
    • JSP using a tag and its corresponding relationship to the TLD file

    • Note Rtexprvalue
      The Tag/attribute/rtexprvalue node is important because it tells you whether the value of the property is evaluated at the time of conversion or at run time.
      If the value is false or undefined, then the property value can only be a string direct, not a user= "${username}" in the expression!
      Rtexprvalue not just for El expressions, there are several ways to do it:

    1. El expression
    2. Script expression
    3. Attribute Standard action

      Note that even if Tag/body-content is declared empty, you can still use Jsp:attribute to place attributes in the body of the tag!

    • What can I put in the Body-conten body?
      The tag body body-conten can be placed in the following values, only if the tag body-content element value is not empty, the tag can have the body.

      For unmarked tags (that is, Body-conten is empty), there are 3 calling methods (including the above attribute standard actions):

    • Taglib in the URI, just a name
      The URI above is randomthings, which is a name. Even a long string of uri= "Http://java.sun.com/jsp/jstl/core" is just a name, not a location.

    • Where are the TLD file locations declared?
      Before JSP2.0, there is a taglib-location entry in Web. XML, but it is not needed now. The container will find the TLD in 4 locations:

    1. Find directly in the Web-inf directory
    2. Find directly in a subdirectory of Web-inf
    3. Find in the Meta-inf directory in the jar file under Web-inf/lib
    4. Find in subdirectories of the Meta-inf directory in the jar file under Web-inf/lib

    • When a JSP uses more than one tag library
      Each TLD must have a separate TAGLIB directive;
      Ensure that the TAGLIB directive name is unique;
      Do not use reserved prefixes such as JSP/JSPX/JAVA/JAVAX/SERLVET/SUN/SUNW, etc.

Tag processor, TLD, and JSP relationships

Custom Tag Processors

The above is how to use the advice custom tag, then now a little bit to see how the tag's support code is written.

    • The Java class that completes the tagging work Foo.AdvisorTagHandler.java
      This simple markup processor extends the Simpletagsupport and implements two key methods: Dotag () and Setuser ().
      Dotag () is the way to do the work, and SetUser () is the method that accepts the value of the property. Details are as follows:

El function vs Tag processor

  • Are all Java classes
  • Need to be placed under a path under the Web-inf/classes file
  • are mapped in the TLD file
  • The mapping of the El function in the TLD is
  • The mapping of the tag processor in the TLD is
  • The El function can define a static method of any name
  • The token processor method name must be Dotag ()

"Head First Servlets & JSP" Use Jstl

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.