J2EE读书笔记,概念摘要 2 JSP Basic

    技术2022-05-11  63

    -JSP: a complement of servlets, easier to write and cause less mistakes than servlets. (You may think JSP is a simple method of servlet at first, but you will see the advantage of JSP later);  JSP is translated into servlet(java code) by continer

    -Life cycle: jspInit() -> _jspService() -> jspDestroy()

    -Elements and Template Data: template is taken and sent directly to a client as it appears on the JSP; elements are not sent directly but interpreted by a JSP container and defines special actions that sould be taken when generating a response, elements are what make JSP dynamic.

    -Two dirrerent formats of JSP syntax (will talk later in JSP in XML sntax): normal(original,classic) and xml-compatible

    -Scripting elements: directly embedding bits of Java code between blocks of template text; three diferent types availiable : scriptlets, expressions and declarations

    -Scriptlets: diretly inserting bits of Java code between chunks of templet text, use the syntax: <% code %> ; scriptlets are great for providing low-level fuctionality such as iteration,loops and condition statements, for many reasons complex scriptlets should be avoided.

    -Expressions: provide an easy method of sending out dynamic strings to a client, use the syntax: <%= expression %>

    -Declarations: is used like a scriptlet to embed code in a JSP, but outside of _jspService() method (image it has been translated into java code); can be used to declare new methods and global class variables. not thread safe ();use the syntax: <%! declarations %>

    -Directives: used to define page attributes, do not send output to a client; use the syntax : <%@ directive {attribute = "value"}* %>; three different directives: page, taglib and include

    --Page directive <%@ page %>: provides page-specific information to a JSP container; attributes like language="Java", extends="", import="", session="ture/false" ...

    --include directive <%@ include %> (translation-time includes) (see include action will be talked later): include text or code at translation time of a JSP (translation-time includes); syntax: <%@ include file = "relativeURL" %>; a good example to use is including a common header and footer with multiple pages of content(header.jsp, footer.jsp)

    --taglib directive <%@ taglib %>: used for custom actions(custom tag libraries); syntax : <%@ taglib uri = "uri" prefix = "prefixOfTag" %> ; note that "uri" attribute value resolves to a location the container understands and the prefix attribute informs a continer what bits of markup are custom actions

    -JSP Configuration: directives can only configure one JSP page, to configure more pages, use <jsp-config> element in web.xml. There are two sub elements : <taglib> and <jsp-property-group> (similar to directives, for example very useful children elements <include-prelude> & <include-coda> help you include header&footer automatically)

    -JSP Actions: method of linking dynamic code to simple markup that appears in a JSP: two types of actions : standard and custom, : all actions follow the syntax : <prefix:element {attribute="value"}*/> . : a complete action is an xml-compatible tag;    : Standard JSP actions are specified by JSP specification and aviliable for use by and JSP continer, Custom actions are created by developers themselves, must be installed with a web application

    -Standard JSP Action

    --include action: <jsp:include page = "page" />: difference between include directive<%@ include %> (translation-time includes) and include action<jsp:include/> (run-time include) : include action is always current with the source code, while include directive is only current with the resource as it was at the time of tanslation.

    --plugin action: <jsp:plugin/>:the plugin action represents one applet that should be embedded in a HTML page

    -- forward action: equivalent to the equestDispather.forward(): syntax <jsp:forward url = "relativeURL"/>

    -- param action: <jsp:forward/> and<jsp:include/>parameters: <jsp:forward page = "examplePage.jsp"> <jsp:param name = "foo1" value = "bar"/> <jsp:param name = "foo2" value = "<%= foo %>"/>  </jsp:forward>

    -- other actions: <jsp:useBean /> , <jsp:getProperty /> and <jsp:setProperty />actions all relate to JavaBeans: <jsp:attribute /> , < jsp:body /> , <jsp:doBody /> and <jsp: invoke /> are JSP standard actions exist for use with custom tags

    -whitespace Preservation: whitespace is preserved as it appear in a JSP. Formatting is retained by the JSP.

    -Attributes: two methods for specifying attributes in JSP elements : run time value and translation time or static values

    -comments: <%-- comments --%>

    -escape characers: ' is escaped as /'; " is escaped as /"; %> is escaped as %/> <% is escaped as </%

    -implicit objects: config, request, response, session, application, pageContext, page, out...: assuming they already exist within a scripting element

    -config: web.xml : the only change from servlet is : replacing the previous line <servlet-classes> with <jsp-file>: all of the child elements of the servlet element are still valid

    -JSP in XML syntax: the reason to use XML syntax was to keep JSP current with the widespread adoption of XML. XML compliant JSP can be created and manipulated using any existing XML: the JSP XML syntax is not easy to use: HTML used to be the dominant technology on the web but no longer

    -XML Documents: JSP actions and custom actions are already in XML-compatible syntax.: scripting elements and directives need to be converted to an XML form.: scripting elements <% %> , <%= %>and <%! %> should be converted into <jsp:scriptlet> , <jsp:expression> and <jsp:declaration> resceptively;: Embedding an expression tag for an attribute value such as : A link to a <a herf = "<jsp:expression>link</jsp:expression>">webside</a> , is not allowed. has to include a specific ncapsulation of the templet text with XML CDATA sections or represent the problematic content with entities. 

    *reference:Falkner & Jones, Servlet and Java Server Pages,


    最新回复(0)