JSP directives & standard actions

Three jsp directives

1. the page directive

first, refere the explaination of it in jsp specificiation:

the page directive defines a number of page dependent properties and communicates these to the JSP container.

 this directive is so common that always can  be see in the top of JSP pages, and used just like this:

 

<%@ page contentType="text/html;charset=utf-8" language="java" %>

  the directive above indiciates that the scripting language is based on java, and the MIME type is 'text/html' and the character encoding of output is 'utf-8'.

Now that the directive involved the concept of encoding. i will simply explain the difference between charset and pagEcoding .

 In jsp specificiation, the two conceptions are  described as follows:

contentType

Defines the MIME type and the character encoding for the response of the JSP page, and is also used in determining the  character encoding of the JSP page.
Values are either of the form “TYPE” or “TYPE;charset= CHARSET”with an optional white space after the “;”. “TYPE” is a MIME type, see the IANA registry at http://www.iana.org/assignments/media-types/index.html
for useful values. “CHARSET”, if present, must be the IANA name for a character encoding. The default value for “TYPE” is “text/html” for JSP pages in standard syntax, or “text/xml” for JSP documents in XML syntax. If “CHARSET” is not specified, the response character encoding is determined as described in
Section JSP.4.2, “Response Character Encoding”. See Chapter JSP.4 for complete details on character
encodings.


pageEncoding

Describes the character encoding for the JSP page. The value is of the form “CHARSET”, which must be the IANA name for a character encoding. For JSP pages in standard syntax, the character encoding for the JSP page is the charset given by the pageEncoding attriute if it is present, otherwise the charset given by the contentType attribute if it is present, otherwise “ISO-8859-1”. For JSP documents in XML syntax, the character encoding for the JSP page is determined as described in section 4.3.3 and appendix F.1 of the XML specification. The pageEncoding attribute is not needed for such documents. It is a translation-time error if a document names different encodings in its XML prolog / text declaration and in the pageEncoding attribute. The corresponding JSP configuration element is page-encoding (see Section JSP.3.3.4, “Declaring Page Encodings”). See Chapter JSP.4 for complete details on character encodings.

 as we kown,  JSP page be compiled to servlet in the server side. the value of pageEncoding specifies the encoding would be used to compile the jsp page, otherwise, the web container will make use of the value of file.encoding.  So 'pageEncoding' is the key factor of avoid chinese problem. if we didnt specify it,  the application work correctly when deployed on windows would  occur chinese character problem if was deployed on linux.  On the other side,  ' charset' specifies the encoding of the response of the jsp page. That is to say, the server encoded the data using the encoding specified by 'charset' before sending it to client.

2. the taglib directive

the jsp specificiation defines it as follow:

the taglib directive in a jsp page declares that the page uses a tag library, uniquely identifies the tag library using a URI and associates a tag prefix that will distinguish usage of the actions in the library.

 typically, this directive is necessary if we wanna use JSTL and struts defined tag library in jsp page.

3. the include directive

the include directive is used to substitute text and/or code at JSP page translation-time . It inserts the text of the specified resource into the page or tag file.

 beside the include directive, there is also another way to do the same thing-- using the <jsp:include> action.

A <jsp:include.../> action provides for the inclusion of static and dynamic resources in the same context as the current page.

 So, ws the difference of the two ways ?  Speak in breaf, the include directive would inserts the text of the included resource to the generated servlet when compiling, in contrast, the other would interpret the response of the included file, and plus it to the itself when visiting the page. In another word, the former is suitable for static resource, and the latter  is suitable for both static and dynamic object.  An example usage listed as follows:

<%@ include file=”relativeURI”%>
//the value of flush set to 'true'  indiciates that the buffer is flushed now.
<jsp:include page=”relativeURI” flush=”true” />

 One important point is that the URI of file/page is relatice to the current jsp file/page. If the uri is started with symbol '/' , it means that the uri is relative to the context of web application.

你可能感兴趣的:(jsp,xml,linux,servlet,struts)