Jsp+el+jstl

Source: Internet
Author: User

First, JSP technology 1. JSP Scripts and annotations

JSP script:

1) <%java Code%>-----Internal Java code translated into the internal of the service method

2) <%=java variable or expression >-----will be translated into service method internal Out.print ()

3) <%!java Code%>----will be translated into the content of the servlet members

JSP annotations: Different annotation visibility ranges are different

1) HTML comment:<!--annotation Content-----Visible range JSP source code, translated servlet, page display HTML source code

2) Java Comment://single-line comment/* Multiline comment */-visible range JSP source translated servlet

3) JSP Comment: <%--comment Content--%>-----Visible range JSP source visible

2. JSP operation principle-----jsp nature is servlet (interview)

When the JSP is first accessed, it is translated by the Web container into a servlet, which executes

Process:

First time access---->helloservlet.jsp---->helloservlet_jsp.java----> Compile Run

PS: The translated servlet can be found in Tomcat's work directory

3. JSP directives (3)

The JSP directive is the command that guides the JSP to translate and run, JSP includes three big instructions:

1) Page directive---The most attribute of the instruction (the page directive default in the actual development)

A directive with the most attributes, which guides the entire page property according to different attributes

Format: <%@ page property Name 1 = "Property value 1" Property Name 2 = "Property value 2" ...%>

Common properties are as follows:

Types of languages that can be embedded in language:jsp scripts

Pageencoding: The current JSP file itself is encoded---internal can contain contenttype

ContentType:response.setContentType (Text/html;charset=utf-8)

Session: Whether the JSP automatically creates a session when translating

Import: Importing Java Packages

ErrorPage: Which page to jump to when the current page is faulted

Iserrorpage: The current page is a page that handles errors

2) include directive

Page contains (statically included) directives that can include one JSP page in another JSP page

Format: <%@ include file= "included file Address"%>

3) taglib Instruction

Introduction of tag libraries in JSP pages (Jstl tag library, struts2 Tag library)

Format: <%@ taglib uri= "tag Library address" prefix= "prefix"%>

4. JSPBuilt/An implicit object (9A----- Written

After the JSP is translated into a servlet , there are 9 objects defined and initialized in theservice method, and we can use the 9 in the JSP script directly. an Object

(1) Out object

Type of Out: JspWriter

Out function is to want the client output content----Out.write ()

The out buffer default 8KB can be set to 0 for closing the out buffer content directly to the respons buffer

(2) PageContext object

The context object for the JSP page, which acts as follows:

The Page object is not the same as the PageContext object

1) PageContext is a domain object

SetAttribute (String name,object obj)

GetAttribute (String name)

Removeattrbute (String name)

PageContext can access data to a specified other domain

SetAttribute (String name,object obj,int scope)

GetAttribute (String name,int scope)

Removeattrbute (String name,int scope)

Findattribute (String name)

---Get Properties from PageContext domain, request domain, session field, application domain, and get it in a domain, not looking backwards

Summary of four scopes:

page domain: Current JSP page range

Request domain: One request

Session Domain: one session

Application domain: Entire Web App

2) Other 8 large implicit objects can be obtained

Example: Pagecontext.getrequest ()

Pagecontext.getsession ()

5. JSP tags (action)

1) page contains (dynamically included): <jsp:include page= "included pages"/>

2) Request Forwarding: <jsp:forward page= "resources to be forwarded"/>

What is the difference between static inclusion and dynamic inclusion?

Static inclusion is the compilation of a JSP file merge, while dynamic inclusion is called to each other to display

Second, El Technology 1. EL Expressions Overview

El (express lanuage) expression can be embedded inside the JSP page, reduce the writing of JSP script, El appears to replace the JSP page script writing.

2. El extracts data from the domain (El's most important role)

JSP script: <%=request.getattribute (name)%>

El expression replaces the above script: ${requestscope.name}

El's main function is to obtain data in four fields, format ${el expression}

El gets the value in the PageContext domain: ${pagescope.key};

El obtains the value in the request domain: ${requestscope.key};

El gets the value in the session field: ${sessionscope.key};

El gets the value in the application domain: ${applicationscope.key};

El obtains a value from four domains ${key};

---also gets the properties from the PageContext domain, the request domain, the session field, the application domain, and gets it in a domain, not looking backwards.

1) Get normal string

2) Get the value of the user object

3) Get the value of list<user>

(emphasis is on omitting get)

${requestscope.company}

${sessionscope.user.name}

${applicationscope.list[1].name}

3. El's Built-in objects 11 X

Pagescope,requestscope,sessionscope,applicationscope

----Get data from a domain in a JSP

Param,paramvalues-Receive parameters.

Equivalent to Request.getparameter () rrquest.getparametervalues ()

Header,headervalues-GET Request header information

Equivalent to Request.getheader (name)

Initparam-Get Global initialization parameters

Equivalent to This.getservletcontext (). Getinitparameter (name)

Cookie-web Cookies in development

Equivalent to Request.getcookies ()---cookie.getname ()---cookie.getvalue ()

Pagecontext-web in the development of PageContext.

PageContext get the other eight big objects

${pagecontext.request.contextpath}

Equivalent

<%=pagecontext.getrequest () .getcontextpath%> This code cannot be implemented

Get the name of your web App

4. El execution expression

For example:

${1+1}

${empty user}

${user==null?true:false}

First, JSTL technology 1. JSTL Overview

JSTL (JSP standard tag Library), JSP standards tag libraries, can be embedded in the JSP page using the form of tags to complete business logic and other functions. The purpose of the JSTL is to replace the script code in the JSP page, as is the case with El. The JSTL standard standard Tag Library has 5 sub-libraries, but with the development, his core library is often used today

Tag Library

The URI of the tag library

Prefix

Core

Http://java.sun.com/jsp/jstl/core

C

i18n[Chen Jihao 1]

Http://java.sun.com/jsp/jstl/fmt

Fmt

Sql

Http://java.sun.com/jsp/jstl/sql

Sql

Xml

Http://java.sun.com/jsp/jstl/xml

X

Functions[Chen Jihao 2]

Http://java.sun.com/jsp/jstl/functions

Fn

2. Jstl Download and Import

Jstl Download:

Download the Jstl jar package from the Apache website. Go to the "http://archive.apache.org/dist/jakarta/taglibs/standard/binaries/" url to download the JSTL installation package.     Jakarta-taglibs-standard-1.1.2.zip, and then unzip the downloaded JSTL installation package, you can see two jar files in the Lib directory, respectively, Jstl.jar and Standard.jar. Where the Jstl.jar file contains the interfaces and related classes defined in the JSTL specification, the Standard.jar file contains the. class file used to implement JSTL and 5 tag Library descriptor files (TLDs) in Jstl


Import two jar packages into the lib of our project

Import the core tag library using JSP taglib directives


3. Common tags for jstl core libraries

1)<c:if test= "" > Label

Where test is the condition that returns a Boolean, Jstl tags are often used in conjunction with El

You can enter this label only if the condition is met.

2)<c:forEach> label

There are two combinations of modes of use:

  


<!--analog enhancements for productlist---list<product>

for (Product product:productlist) {

Syso (Product.getpname ());

}

-

<!--items: A collection or array var: represents an element in a collection--

<c:foreach items= "${productlist}" var= "Pro" >

${pro.pname}

</c:forEach>

Example:

1) Traverse the value of the list<string>

2) traverse the value of the list<user>

3) Traverse the value of the map<string,string>

4) Traverse the value of the map<string,user>

5) Traverse the value of the map<user,map<string,user>>

Entry.key-----User

Entry.value------list<string,user>

[Chen Jihao 1] related to coding, which is related to internationalization

[Chen Jihao 2] using El Invoke

Jsp+el+jstl

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.