JSP three instructions, nine built-in objects, JSP action tags, el expression and function library

Source: Internet
Author: User
Tags tld

JSP three major instructions
----------------------------------------------
A JSP page, you can have a definition of 0~n instructions!

1. Page-and most complex: format: <% @page language= "java" info= "xxx" ...%>
* Pageencoding and ContentType:
> pageencoding: It specifies the current JSP page encoding, as long as not to lie, there will be no garbled! You need to use pageencoding! when the server wants to compile the JSP into. Java
> ContentType: It means adding a response header: content-type! Equivalent to Response.setcontenttype ("Text/html;charset=utf-8");
> If two properties only provide one, then the other defaults to the one that is set.
> If two properties are not set, the default is ISO
* Import: Guide Package! can appear multiple times
* ErrorPage and Iserrorpage
> errorpage: If the current page throws an exception, which page to forward to, by ErrorPage to specify
> iserrorpage: It specifies whether the current page is an error-handling page! When this property is true, the page will be set to a status code of 500! And this page can use the exception! in 9 large built-in objects

* Configure error page in Web. xml
<error-page>
<error-code>404</error-code>
<location>/error/errorPage.jsp</location>
</error-page>
<error-page>
<error-code>500</error-code>
<location>/error/errorPage.jsp</location>
</error-page>
<error-page>
<exception-type>java.lang.RuntimeException</exception-type>
<location>/index.jsp</location>
</error-page>
* AutoFlush and buffer
> AutoFlush: Specifies whether the JSP output stream buffer is full, automatically refreshed! The default is true, and if False, throws an exception when the buffer is full!
> Buffers: Specifies the buffer size, which defaults to 8kb and does not usually need to be modified!
* Iselignored: Whether to ignore the El expression, the default value is False, not ignored, that is, support!
* Basic useless:
> Language: Specifies the language type of the current JSP compilation and the default value is java.
> Info: Info!
> IsThreadSafe: Does the current JSP support concurrent access!
> session: Whether the current page supports the session, if False, then the current page has no session this built-in Object!
> extends: Let the JSP-generated servlet inherit the class specified by this property!

2. Include--static inclusions
* Similar to the features of RequestDispatcher's include () Method!
* <% @include%> It is done when the JSP is compiled into a Java file! Together they generate a Java (that is, a servlet) file and then generate a class!
* RequestDispatcher's include () is a method that contains and is comprised of two servlets, or two. class! They just merge the content of the response at run time!
* Role: Decomposition of the page, the use of a combination of methods, such a page is a constant part of a separate JSP, and we only need to deal with the change of the page. To implement page reuse
* Unique attribute file

3. Taglib---Import Tag Library
* Two properties:
> prefix: Specify the prefix of the tag library in this page! by our own name!
> URI: Specify the location of the tag library!
> <% @taglib prefix= "s" uri= "/struts-tags"%>
Prefix usage:<s:text>

==========================================================

Nine built-in objects
--------------------------------------------
* Out-of-the-JSP output stream, equivalent to Response.getwriter () to respond to the client
* page-to-current JSP Object! Its reference type is object, which is the following code in the flesh: Object page = this;
* config--it corresponds to the ServletConfig object in the flesh!
* PageContext--One Top 9!
* Request--Httpservleteequest
* Response-HttpServletResponse
* Exception--throwable You can use this object only on the error page
* Session---HttpSession, not every JSP page can be used
* Application-ServletContext

1. PageContext
* One Top 9!
* There are three domains in the servlet, and there are four fields in the JSP, which is the last domain Object!
> ServletContext: The entire application
> Session: Entire conversation (only one user in a session)
> Request: A chain of requests!
> PageContext: a JSP page! This field is used to share data between the current JSP page and the tags currently in use in the current JSP page!
> Domain Objects
> Agent Other domains: Pagecontext.setattribute ("xxx", "xxx", pagecontext.session_scope);
>***** Global Search: Pagecontext.findattribute ("xxx"); From small to large, rely on find! (Lookup range is a JSP four domain)
> Get 8 more built-in objects: through the GetXXX () method
Note: Only exception and session objects in the nine built-in objects can specify whether they are available


JSP Action Tags (20)
--------------------------------------------
The action tags of these jsps are essentially different from the tags provided by HTML.
* Action tag is performed by Tomcat (server) to interpret! It is executed on the server side, just like Java code!
* HTML is executed by the browser!
* &LT;JSP:FORWARD&GT;: Forward! It is the same as the forward method of RequestDispatcher, one is used in servlet, one is used in JSP!
* &LT;JSP:INCLUDE&GT;: Contains: It is the same as the include method of RequestDispatcher, one is used in the servlet, one is used in the JSP!
> <% @include > and <jsp:include> what's different!
* &LT;JSP:PARAM&GT;: It is used as a sub-label for forward and include! Used to pass parameters to forwarded or included pages!


JavaBean
--------------------------------------------
Specifications for JavaBean:
1. You must have a default constructor! (i.e., without reference)
2. Provide the Get/set method, if only get method can also, then this property is read-only property!
3. Attribute: A member with the Get/set method is called a property, and there can be no member, only the Get/set method. The property name is determined by the Get/set Method! Instead of the member name!
4. The method name satisfies a certain specification, then it is the attribute!
A Boolean property whose read method can be the start of an IS, or a get start!


Introspection:
Introspection Class (Introspector)--bean information (BeanInfo)--Property descriptor (PropertyDescriptor)--Get/set corresponding method! ---> can be reflected!

-----------------------

Commons-beanutils, it is dependent on introspection to complete!
* Guide Package:
> Commons-beanutils.jar
> Commons-logging.jar

Beanutils.getproperty (Object Bean, String PropertyName)
Beanutils.setproperty (Object Bean, String propertyname, String propertyvalue)
Beanutils.populate (map map, Object Bean)

Commontutils.tobean (map map, class Class)

-----------------------

The action tag associated with JavaBean in the JSP!

* <jsp:useBean> Find or create a bean
* <jsp:usebean id= "user1" class= "Cn.itcast.domain.User" scope= "session"/> Look for a bean named User1 in the session field, if it does not exist, create the
* <jsp:setProperty>
* <jsp:setproperty property= "username" name= "user1" value= "admin"/> Set the User1 attribute value for this javabean named username to admin
* <jsp:getProperty>
* <jsp:getproperty property= "username" name= "user1"/> Get the property value named User1 for JavaBean named username

===========================================
El expression
--------------------------------------------
1. El is a JSP built-in expression language!
* jsp2.0 starts by not using Java scripts, but instead uses El expressions and dynamic tags instead of Java scripts!
* El Replacement is <%= ...%>, that is, El can only do the output!

2. El expression to read four fields
* ${xxx}, the global lookup property named XXX, if it does not exist, outputs an empty string instead of NULL.
* ${pagescope.xxx}, ${requestscope.xxx}, ${sessionscope.xxx}, ${applicationscope.xxx}, specify domain Get Properties!

3. JavaBean Navigation
<%
Address address = new address ();
Address.setcity ("Beijing");
Address.setstreet ("Xisanqi");

Employee EMP = new Employee ();
Emp.setname ("Li Xiaoxi");
Emp.setsalary (123456);
Emp.setaddress (address);

Request.setattribute ("EMP", EMP);
%>

${requestscope.emp.address.street}<!--request.getattribute ("emp"). GetAddress (). Getstreet ()--><br/>


4. El can output everything in 11 built-in objects! 11 built-in objects, 10 of which are map! PageContext is not a map, it is the PageContext type, 1 Top 9.
* We have studied four: Pagescope,requestscope,sessionscope,applicationscope.
* param: The corresponding parameter, which is a map, where the key parameter name, value is the parameter value, applies to single-valued parameters.
* Paramvalues: The corresponding parameter, which is a map, where the key parameter name, value is more than one parameter value, applies to the multi-valued parameter.
* Header: corresponding to the request header, it is a map, where key represents the header name, value is a single header value, applicable to the single-valued request header
* Headervalues: corresponding to the request header, it is a map, where key represents the header name, value is a multi-head value, applicable to the multi-valued request header
* Initparam: Get the parameters in <context-param>!
<context-param>
<param-name>xxx</param-name>
<param-value>XXX</param-value>
</context-param>
<context-param>
<param-name>yyy</param-name>
<param-value>YYY</param-value>
</context-param>

${INITPARAM.XXX}

* cookie:map<string,cookie> type, where key is the cookie Name,value is the cookie object. ${cookie.username.value} example: Get Sessionid,${cookie. Jsessionid.value}
* PageContext: It is PageContext type! ${pagecontext.request.contextpath}
Note: Map.key This is El syntax, map[' key ' can also operate map, also EL syntax

=========================================

El function library (provided by JSTL)
--------------------------------------------
* Import Tag library: <%@ tablib prefix= "FN" uri= "Http://java.sun.com/jsp/jstl/functions"%>
String toUpperCase (String input): Converts a parameter to uppercase
String toLowerCase (String input): Converts arguments to lowercase
int indexOf (string input, String substring): From a large string, output a small string of positions!
Boolean contains (string input, String substring): To see if a string is included in a large string
Boolean containsignorecase (string input, string substring): Ignores case, contains
Boolean StartsWith (string input, String substring): is prefixed with a small string
Boolean EndsWith (string input, string substring): Whether to use small strings as suffixes
String substring (string input, int beginindex, int endIndex): Intercept substring
String Substringafter (string input, string substring): Gets the string that follows the position of the small string in a large string
Substringbefore (string input, string substring): Gets the string in front of the small string in the large string
String EscapeXML (String input): Escapes input "<", ">", "&", "" "," ""
String trim (String input): Remove spaces before and after
String replace (string input, String substringbefore, String substringafter): Replace
String[] Split (string input, string delimiters): Splits a string to get an array of strings
int length (Object obj): You can get the lengths of strings, arrays, various collections!
String join (String array[], string separator): union string Array!

=========================================

Custom Function Libraries
--------------------------------------------
* Write a Java class, the class can be defined 0~n methods, but must be static, and there is a return value!
* Create a TLD file under the Web-inf directory
<function>
<name>fun</name>
<function-class>cn.itcast.fn.MyFunction</function-class>
<function-signature>java.lang.string Fun () </function-signature>
</function>

* Import Tag Library in JSP page
<%@ taglib prefix= "It" uri= "/web-inf/tlds/itcast.tld"%>
* Use a custom function in a JSP page: ${it:fun ()}

JSP three instructions, nine built-in objects, JSP action tags, el expression and function library

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.