El expression in JSP

Source: Internet
Author: User

start with JSP built-in objects

The so-called built-in object is that we can use these objects directly in the JSP page. No need to create.

    • PageContext

    • Request

    • Session

    • Application

The above 4 are scope objects,

difference of four scopes
    • PageContext "PageContext"

Scopes are limited to the current page.

You can also get to the other eight built-in objects.

    • Request "HttpServletRequest"

Scopes are limited to one request only, as long as the server responds to the request. The value stored in this field is gone.

    • Session "HttpSession"

Scopes are limited to one session (multiple requests and responses).

    • Application "ServletContext"

The entire project can be accessed, and the server cannot be accessed after it has been shut down.

Other built-in objects
    • Out "JspWriter"

      • Response "HttpServletResponse"

    • Exception "Throwable"

      • The page "Object"---is the instance object of the Java class that this JSP translates into

        • Config "ServletConfig"

El expression

is to simplify our JSP code, specifically to simplify the Java code written in the JSP.

    • notation format

${-expression}

If a value is taken from a scope, it is first taken from a small scope, and if not, it is taken to the next scope. The four scopes have been taken out of all the time, and there is no display.

How to use1. Remove the values stored in the 4 scopes.

<%
Pagecontext.setattribute ("Name", "page");
Request.setattribute ("name", "request");
Session.setattribute ("name", "session");
Application.setattribute ("Name", "Application");
%>

value <br> by ordinary means;

<%= pagecontext.getattribute ("name")%>
<%= Request.getattribute ("name")%>
<%= session.getattribute ("name")%>
<%= application.getattribute ("name")%>

<br> use the EL expression to take out the values in the scope <br>

${pagescope.name}
Span class= "Cm-tab" > ${requestscope.name}
${sessionscope.name}
${applicationscope.name}
    1. If the array is stored in the domain

      <% String [] a = {"AA", "BB", "CC", "DD"}; Pagecontext.setattribute ("array", a); %>

          Use the EL expression to remove the value of the array in the scope <br>

      ${array[0]}, ${array[1]},${array[2]},${array[3]}
    1. If a collection is stored in the domain

   ? 
${li[0]}, ${li[1]},${li[2]},${li[3]}
?
<%
map map = new HashMap ();
Map.put ("name", "Zhangsna");
Map.put ("age",);
Map.put ("address", "Beijing..");

Map.put ("Address.aa", "Shenzhen..");

Pagecontext.setattribute ("map", map);
%>
    1. Remove the value of the map collection

      <% Map map = new HashMap (), map.put ("name", "Zhangsna"), map.put ("Age"), map.put (" Address "," Beijing.. ");


      Map.put ("Address.aa", "Shenzhen..");

Pagecontext.setattribute ("map", map); %> use El expression to fetch the value of map in scope <br> ? {map.age}, ? {map["Address.aa"]}

Value Details:
    1. take values from the domain, save the values.

      <% 
      //pagecontext.setattribute ("name", " Zhangsan ");
      session.setattribute ("name", "Lisi ...");
      %> 

      <BR> The direct designation says, go to this scope to find this name<br> ${Pagescope.name}

<br>//Search from page, do not go to request, go to session, go to application <br> ${name}

<br> Specify value from session <br> ${sessionscope.name}

    1. How to take value

If the value is subscript, use it directly []


<%
String [] array = {"AA", "BB", "CC"}
Session.setattribute ("array", array);
%>

If there is no subscript, directly use the way to fetch


<%
User user = New User ("Zhangsan", 18);

Session.setattribute ("U", user);
%>
?

El expressions are generally used, and more often than not, the property values are taken out of an object, such as the name of a student.

the 11 built-in objects of the El expression.

${object name. Member}

    • PageContext

Scope-related objects

    • Pagescope

    • Requestscope

    • Sessionscope

    • Applicationscope

Header information related objects

    • Header

    • Headervalues

Parameter information related objects

    • Param

    • Paramvalues

    • Global initialization parameters of cookies

    • Initparam

JSTL

Full name: JSP standard TAG Library JSP standards Tag libraries

Simplifies code writing for JSPs. Replace <%%> wording. General and El expression mates

How to use
    1. importing jar files to the project's Webcontent/web-inf/lib Jstl.jar Standard.jar

    2. On the JSP page, use the TAGLIB directive to introduce the tag library

    3. Note: If you want to support El expressions, the introduced tag library must select version 1.1, and the 1.0 version does not support El expressions.


<%@ taglib prefix= "C" uri= "Http://java.sun.com/jsp/jstl/core"%>
Common Tags

C:set/c:set<c:if test= "" >/c:ifc:foreach/c:foreach

    • C:set


<!--declares an object name, the value of the object is Zhangsan, is stored to the page (default), and the specified session--
<c:set var= "name" value= "Zhangsan" scope= "Session" ></c:set>

${sessionscope.name}
    • C:if

Determine if the expression in test is satisfied, if satisfied, executes the output in the c:if tag, c:if is not else.


<c:set var= "Age" value= "></c:set>"
<c:if test= "${Age >" >
Age is more than 26 years old ...
</c:if>

<c:if test= "${age <=" >
Younger than 26 years of age ...
</c:if>

------------------------------
?
Define a variable name flag to receive the value of the preceding expression, and then exist in the session field
?
<c:if test= "${Age >" var= "Flag" scope= "Session" >
Age is more than 26 years old ...
</c:if>
    • C:foreach



<c:foreach begin= "1" end= "ten" var= "I" step= "2" >
${i}
</c:forEach>

-----------------------------------------------
?

var: each element that is traversed is received with the user. -
<c:foreach var= "user" items= "${list}" >
${user.name}----${user.age}
</c:forEach>

El expression in 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.