Head First Servlets & JSP -8-script-free JSP

Source: Internet
Author: User
Tags access properties

Code that previously interacted with the servlet and JSP
    • Example of a servlet code:

    • JSP code example:

What if the property is not a string but a bean?
    • A simple javabean.

    • Example of a servlet code:

    • JSP code example:

      or write this:

Do not appear in the JSP script
    • JSP using the standard action

    • Standard Action Jsp:usebean Detailed

      Scope is the page scope by default, so there may be a problem in the example code above: The person object cannot be found!

    • Standard Action Jsp:getproperty Detailed

    • Jsp:usebean can also create a bean
      The following standard action flags:

      becomes the following code in the _jspservice () method:

    • Jsp:setproperty can give attribute values to the bean generated above

    • Consider more comprehensive: Jsp:usebean conditionally creates a Bean--usebean body
      If the bean attribute cannot be found, then a bean is created:

Bean rule

The common non-enterprise JavaBean specification defines how a class can be considered a javabean. This specification is complex, but the result is that the JSP and servlet use the bean only to know the following rules:

    • Must have a public constructor with no parameters
    • You must name the public get and set methods according to the naming constraints
    • The parameter type of the set method and the return type of the Get method must be the same
    • The property name and type are deduced by the Get method and the Set method
    • The nature type must be string when used in conjunction with JSP. (This can be broken by scriptlet script or El expression, see subsequent documentation)
      In other words, the Java object referenced in the class attribute of the standard action Usebean must satisfy the above bean rules.
To establish a polymorphic bean reference


As above, use the person type, but create an employee object:

    • Add Type property for Jsp:userbean

      Type: Can be a class type, an abstract type, or an interface.
      Class: Must be a subclass of type or a specific implementation.

    • If you use only type, there is no class

      This requires that the person attribute must exist in the page scope, otherwise the java.lang.InstantiationException exception is reported.

      Type vs Class
      type== reference type, which is the type to be declared, can be an abstract class
      The class== object type, which is the class to instantiate, must be a concrete class

Requests go directly to the JSP without the servlet

Since JSP actions can generate beans, it is possible to create and assign objects directly to the JSP page without using a servlet to new an object.

    • Previous Scripting Ugly class practice
      The form is as follows:

      The JSP is as follows:

      or this:

    • Use the Param property to avoid scripting

    • If the HTML and JavaBean have the same attribute name, then param can be omitted, the property can be more refreshing

      The bean tag will automatically convert the base type:
      The above name type is the String,empid type is the Int,bean tag that automatically converts the type.

If the bean nature is not a sring or basic type, such as the nature itself is a bean object?

    • It is possible to use the script of the Ugly class

    • El came--Avoid using scripts

El expression

${person.name}
In the code above, person can use without a declaration, why?
The first named variable in an El expression can be an implicit object (an El implicit object is different from a JSP and an object), or it can be an attribute. The following article details the El Implicit object.

El use dot (.) Operator access properties and mapping values

Access nature: For the JavaBean attribute (bean has nature);
Access Map value: For maps such as map (map with key);

    • Dot number left must be a map or a bean
    • The right side of the dot must be a mapping key or a bean property
    • The right side of the dot must follow the naming rules for Java identifiers
El uses the brackets ([]) operator to access more

In addition to accessing properties and mapping values, you can also access array, List, non-Java specification property names, etc.

    • The variables to the left of the brackets have more options, such as a map, a bean, or a list or an array
    • In parentheses is a string direct (that is, the string that is quoted in quotation marks), which can be an index of a map key, a bean property, a list, or an array
    • A string index in a list or array is cast to an int
    • If the string is not a direct amount in parentheses, it is calculated, and can be nested in an expression
El Implicit Object

    • Request parameters in El
      If the specific parameter name has only one parameter value, it can be used param implicit object;
      ${param.属性名}
      If you have multiple parameter values for a given parameter name, use Paramvalues.
      ${paramValues.属性名[n]},n是索引

    • Get information from a request
      The El's header implicit object guarantees a map of all headers, such as getting host:
      ${header["host"]}或者${header.host}
      This is equivalent to scripting:
      <%= request.getHeader("host") %>

If you want to get the HTTP request method?—— use PageContext for everything else.
Using the script is this:
<%= request.getMethod() %>
However, you cannot use an El implicit object like this: ${requestScope.method},这是不对的,requestScope不是request对象本身
But it needs to be:
${pageContext.request.method}
That is PageContext has a request nature, request has a method nature.

    • The role of implicit scopes
      Since Requestscope is not the request object, it does not get the parameters in request, so what is the effect?
      Consider a situation like this:
      request.setAttribute("foo.person",p);
      To get the "Foo.person" attribute, the El expression cannot be done because "Foo.person" is a full property name.
      ${foo.person.name}Not at all, because the container thinks Foo is a property in a scope, but it is never found.
      Implicit object Requestscope can work:
      ${requestScope["foo.person"].name}

    • Getting a cookie is easier
      To obtain the cookie value of the username attribute, it is convenient to use an implicit object cookie:
      ${cookie.userName.value}
      It's a lot more hassle than using scripts because the request does not have the GetCookie (CookieName) method, only this:

    • Get context parameters, do not confuse with servlet initialization parameters
      If the parameter mainemail is configured in DD:

      Get mainemail using the EL Expression:
      ${initParam.mainEmail}

El functions: Not only attributes or properties, but also Java methods can be called

The following steps are used with the El function:

    1. Write a Java class with a public static method, placed in a directory of web-inf/classes
    2. Write a tag library profile (tag libraries descriptor, TLD) and place it in a web-inf directory
    3. Put a taglib directive in the JSP
    4. Using the EL call function
    • When it comes to the deployment of files such as:
    • Simple examples are:

      The EL function will be described in detail later.
Template multiplexing--include directives

If you want each JSP to display a header or footer:
Standard header file header.jsp:

    • Using the Include directive
      A JSP contact.jsp in a Web application:

    • Using the Include standard action

    • Looks like code, effect is similar, but the principle is different
      The include directive occurs at the time of conversion, although it is converted only once, but the resulting servlet class is larger and is used when the product is released;
      The Include standard action occurs at run time, with the latest header content each time, but each request has a bit of performance overhead.

Servlet generated for JSP using the include directive:

Servlet generated for JSP using the include standard action:

    • Include directives are location-sensitive
      Only the location of this instruction in the JSP will make a difference. For example, if you use the page directive, you can place it anywhere on the page (an Ann convention is placed at the front), but the include directive tells the container exactly where to insert the source of the contained file, such as a header inserted at the beginning, and a footer inserted at the end.
Reusable widgets Remove some HTML tags

Do not put the start and end HTML and body tags in reusable parts! When designing and writing layout template parts (such as headers, navigation bars, and so on), assume that they are included in other pages.

    • Optimized parts and references:
Personalized component Reuse--jsp:param custom included content

If you want a context-sensitive sub-heading on the header, it depends on the page.
Modified Header HEADER.JSPF:

The JSP page that references it can only be used with the include standard action, not the include directive (the instruction is not a dynamic call):

Page Jump--jsp:forward Standard action
    • Conditionally forwarded JSP (HELLO.JSP), but the script is used here, that's not good.
    • Target JSP to which the request was forwarded (HANDLEIT.JSP)

      Why the first request did not print out the "Welcome to Out page!" This sentence before jumping to judge the text?
      Because, using the Jsp:forward standard action, the buffer is emptied before forwarding, that is, all content that is written to the response before forwarding will be cleared out.
      If there is a good person, in the "Welcome to Out page!" After this sentence, use the script to force the output:
      <% out.flush(); %>
      Then it does output, and there is a iilegalstateexception exception, and no jump occurs. Therefore, do not first refresh the output and then forward.

    • Remove script--jstl tags from conditional forwarding

      The following article details the JSP standard tag library.

Points
    • JSP expression Language (EL) review

    • A review of the standard actions related to beans

    • General review

Head First Servlets & JSP -8-script-free JSP

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.