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–> 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 default is the one that is set.
    If none of the two properties are set, the default is ISO

  2. Import: Guide Package! can appear multiple times
  3. 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

  4. Configuring the error page in Web. xml

    404
    /error/errorpage.jsp


    500
    /error/errorpage.jsp


    Java.lang.RuntimeException
    /index.jsp

  5. 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!
    Buffer: Specifies the size of the buffers, which defaults to 8KB and does not normally need to be modified!

  6. Iselignored: Whether the El expression is ignored, the default value is False, not ignored, that is, support!
  7. Basically 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 session, if False, then the current page does not have the session of this built-in Object!
    Extends: Let the JSP-generated servlet inherit the class specified by this property!
  8. include–> static inclusions

    • Similar to the functionality of the include () method of RequestDispatcher!
    • <% @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
  9. Taglib–> Importing Tag libraries

    • Two properties:

    Prefix: Specifies the prefix of the tag library in this page! by our own name!
    URI: Specifies the location of the tag library!
    <% @taglib prefix= "s" uri= "/struts-tags"%>
    Usage of prefix:

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

Nine built-in objects
  • The output stream of the out–> JSP is equivalent to the Response.getwriter () used to respond to the client
  • Page–> the 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–> a top of 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
      • A top of 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 Object
    Agent Other domains: Pagecontext.setattribute ("xxx", "xxx", pagecontext.session_scope);
    * whole domain 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!
*: Forward! It is the same as the forward method of RequestDispatcher, one is used in servlet, one is used in JSP!
*: Contains: It is the same as the RequestDispatcher include method, one is used in the servlet, one is used in the JSP!

<% @include > And what's the difference!
*: 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) –> corresponding Get/set of the property I can reflect it!

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!

    • –> finding or creating a bean
      • Look for a bean named User1 in the session field, if it does not exist, create the

      • Set the Username property value of this javabean named User1 to admin

      • Gets the property value named username for the JavaBean named User1

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

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 replaces <%= ...%>, 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.
    • Pag eS CoPe.xxx, {requestscope.xxx}, sessIoNS CoPe.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);
    %>

Use El to obtain the EMP of the request domain

${requestscope.emp.address.street}

    1. 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 multi-valued parameters.
      • Header: corresponding to the request header, which is a map, where key represents the header name, value is a single header value, applies to the single-valued request header
      • Headervalues: Corresponds to the request header, which is a map, where key represents the header name, value is a multi-head value, applies to the multi-valued request header
      • Initparam: Get the parameters inside!

        Xxx
        Xxx


        yyy
        YYY

      ${INITPARAM.XXX}

      • Cookie:map
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

      Fun
      Cn.itcast.fn.MyFunction
      Java.lang.String Fun ()

    • Import a tag library in a JSP page
      <%@ taglib prefix= "It" uri= "/web-inf/tlds/itcast.tld"%>

    • To 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.