1, JavaBean
Introduction to JavaBean
JavaBean is a reusable component developed with Java language. In the development of JSP, JavaBean can be used to reduce duplicate code and make the development of the whole JSP code more concise. Using JSP with JavaBeans has the following advantages:
1. HTML and Java code can be separated, which is mainly for the convenience of future maintenance. If all program codes (HTML and Java) are written into JSP pages, the whole program code will be more and more complex, resulting in difficulties in future maintenance.
2. Take advantage of JavaBean. Write commonly used programs into JavaBean components. When JSP is to be used, just call JavaBean components to perform the functions required by users, and there is no need to write the same program again. In this way, the time required for development can be saved.
JavaBean development requirements
1.JavaBean itself is a class, which belongs to Java object-oriented programming.
2. In JSP, if you want to use the tag of Java Bean provided by JSP to operate simple classes, this class must meet the following development requirements:
(1) All classes must be placed in a package. If there is no package in the WEB, it does not exist;
(2) All classes must be declared as public class es so that they can be accessed externally;
(3) All attributes in the class must be encapsulated, that is, use private declaration;
(4) If the encapsulated attribute needs to be operated externally, the corresponding setter and getter methods must be written;
(5) There is at least one parameterless constructor in a JavaBean, which is used by tags in JSP.
First simple JavaBean
package blog.csdn.joywy; public class SimpleBean{ private String name; private int age; public void setName(String name){ this.name = name; } public void setAge(int age){ this.age = age; } public String getName(){ return this.name; } public int getAge(){ return this.age; } }
If a class contains only properties, setter s, and getter methods, the class becomes a simple JavaBean.
For simple JavaBean s, there are also several nouns:
(1)VO: corresponds to a simple Java object and is specially used for the operation of passing values
(2)POJO: simple Java object
(3)TO: transfer the object. During remote transmission, the class of the object must implement the java.io.Serializable interface
2, EL expression
Introduction to EL expressions
The full name of EL is Expression Language. Main functions of EL:
1. Get data
EL expressions are mainly used to replace script expressions in JSP pages to retrieve java objects and obtain data from various types of Web domains. (for an object in a web domain, access javabean properties, list collection, map collection and array) use EL expression to obtain data syntax: "${identifier}"
During the execution of EL expression statement, pageContext.findAttribute method will be called, and the identifier will be used as the keyword to find the corresponding object from the four fields of page, request, session and application respectively. If it is found, the corresponding object will be returned, and if it is not found, it will return "" (note that it is not null, but an empty string).
EL expressions can easily obtain the properties of JavaBean s or the data of arrays, collections, and Map type collections
2. Perform operations
Using EL expression, some basic relational operations, logical operations and arithmetic operations can be performed in JSP pages to complete some simple logical operations in JSP pages. ${user==null}
Syntax: ${operation expression}, EL expression supports the following operators:
(1) Relational operator
(2) Logical operators:
(3) Empty operator: check whether the object is null (empty)
(4) Binary expression: ${user!=null?user.name: ''}
(5) [] and. Sign operators
3. Get common web development objects
EL expression defines some implicit objects. Using these implicit objects, web developers can easily obtain references to common web objects, so as to obtain the data in these objects.
EL expression language defines 11 implicit objects. Using these implicit objects, we can easily obtain some common objects in web development and read the data of these objects.
Syntax: ${implicit object name}: get a reference to an object
4. Calling Java methods
EL expressions allow users to develop custom EL functions to call methods of Java classes through EL expressions in JSP pages.
EL expression syntax allows developers to develop custom functions to call methods of Java classes. Syntax: ${prefix: method (parameters)}
In EL expressions, you can only call static methods of Java class. The static method of Java class needs to be described in TLD file before it can be called by EL expression.
EL custom functions are used to extend the functions of EL expressions, which can complete the functions that ordinary Java program code can complete.
3, EL implicit object
11 implicit objects are defined in the EL expression. Using these implicit objects, you can easily read cookies, HTTP request header fields, request parameters
The information of initialization parameters in the Web application, and the implicit objects in EL expressions are as follows:
1. pageContext object
EL In an expression pageContext Implicit objects and JSP In the page pageContext Corresponding to the object, EL Expressions can be pageContext Implicit object access other JSP Implicit objects, such as access request,response Object properties, you can use expressions ${pageContext.request.requestURI}
Expressions ${pageContext.response.contentType} and so on.
2. Implicit objects representing specific domain properties (4 in total)
EL In an expression pageScope,requestScope,sessionScope and applicationScope Four implicit objects are used to access JSP Page page,request,session,application Properties in the four domains. For example, an expression ${pageScope.userName}return page In scope userName Property, expression ${sessionScope.bookName}return session In scope bookName The value of the property. stay EL Instead of using these implicit objects to specify the lookup fields in the expression, you can directly reference the attribute names in these fields. For example, an expression ${userName}Will be page,request,session,application The four scopes are searched in order userName Property until it is found.
3. Implicit objects representing request parameters (2)
EL Implicit objects in expressions param and paramValues Used to get client access JSP The value of the request parameter passed on page because HTTP The protocol allows a request parameter name to appear multiple times, that is, a request parameter may have multiple values, so, EL The expression provides param and paramValues These two implicit objects are used to obtain a value and all values of the request parameters respectively. Param An implicit object is used to return a value of a request parameter. If the same request parameter has multiple values, it returns the value of the first parameter. paramValues An implicit object is used to return all the values of a request parameter, and the return result is a string array composed of all the values of the parameter, such as an expression ${paramValues.username[0]}Returns the value of the first element in the array.
4. Implicit objects representing HTTP request messages (2)
EL Implicit objects in expressions header and headerValues Used to get client access JSP The value of the request header field passed when the page. because HTTP The protocol allows some request header fields to appear multiple times, that is, a request header field may have multiple values, so, EL The expression provides header and headerValues Two implicit objects are used to obtain a value and all values of the request header field respectively. header An implicit object returns a value of a request header field. If the same request header field has multiple values, the first value is returned. For example, use an expression ${header.referer}It can be obtained very conveniently referer The value of the request header field. headerValues An implicit object is used to return a string array consisting of all the values of the request header field.
5. cookie implicit object
EL Implicit objects in expressions cookie Is a representative of all Cookie Informative Map Gather, Map The keywords of the elements in the collection are Cookie The name of the, and the value is the corresponding Cookie Object. use cookie An implicit object can access a Cookie Objects, these Cookie Object is called HTTPServletRequest.getCookies()Method, if multiple Cookie Shared name, return Cookie The first in the object array Cookie Object. For example, to access a file named userName of Cookie Object, you can use expressions ${cookie.userName}.
6. initParam implicit object
EL In an expression initParam Is a representative Web Of all initialization parameters in the application Map Object, the value of each initialization parameter is ServletContext.getInitParameter(String name)Method. Web The initialization parameters of the application can be configured in two ways, namely server.xml Configuring and in files web.xml The configuration in the file is as follows: (1)stay server.xml Configuration in file Web Application initialization parameters: (2)stay web.xml Configuration in file Web Application initialization parameters:
Some case Codes:
package cn.itcast.chapter07.beanutils; import java.util.HashMap; import java.util.Map; import org.apache.commons.beanutils.BeanUtils; import cn.itcast.chapter07.servlet.Person; public class BeanUtilsDemo { public static void main(String[] args) throws Exception{ Person p=new Person(); Map<String,Object> map=new HashMap<String,Object>(); map.put("name","2005"); map.put("age",2); BeanUtils.populate(p, map); String name=BeanUtils.getProperty(p,"name"); int age=Integer.parseInt(BeanUtils.getProperty(p, "age")); System.out.print(name); System.out.print(age); } }
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> ${empty person}<br/> ${empty user }<br/> -------------------------------------------------<br/> 1>3 Are you? ${1>3 } <br> -------------------------------------------------<br/> 1+3=${1+3 }<br/> -------------------------------------------------<br/> ${user.my-name}<br/> ${user["my-name"]}<br/> -------------------------------------------------<br/> ${person.name }<br/> -------------------------------------------------<br/> <%= (String) request.getAttribute("username") %><br/> <%= (String) request.getAttribute("password") %><br/> -------------------------------------------------<br/> -------${username }---------<br/> ${password }<br/> </body> </html>
```java <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> Get project path: ${pageContext.request.contextPath}<br/> ${pageContext.request.contentType}<br/> ${pageContext.servletContext.servletContextName}<br/> </body> </html>
Case results: