Overview of Jsp and EL expressions

JSP

  **JSP (full name JavaServer Pages)** is a dynamic web technology standard created by Sun Microsystems. JSP is deployed on the web server, can respond to the request sent by the client, and dynamically generate HTML, XML or other format document Web pages based on the requested content, and then return to the requester
. The essence of jsp is that Servlet
jsp can write java code

JSP writing java code (not recommended)

<%@ page import="java.util.Random" %>		//jsp导入包
<% %>		在这里面写的java代码,会生成在service中
<%! %> 		这里面写的java代码会被编译成成员变量
<%= %>		这里面写的是页面输出内容

Four scopes of JSP objects

HttpServletContext  application;   整个项目
HttpSession         session;       一次会话
HttpServletRequest  requst ;        一次请求
HttpPageContext     pageContext;     当前页面

Their main core method is

setAttribute(键,值);	//设置键值对key-value
getAttribute();		//通过key获取value
removeAttribute();	//通过key移除键值对



EL expression

Basic overview

1.EL(Express Lanuage)表达式可以嵌入在jsp页面内部
2.可以替代jsp页面中输出脚本的编写,减少jsp脚本的编写
3.书写方式:${EL表达式内容}

The role of EL expressions

  • Find the specified data from the domain object.
  • Built-in object usage
  • Execution operator

To use EL expressions in jsp, you need to set isELIgnored="false"

<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>

EL expression method to get the value

  The value obtained by the el expression will be taken first from the smallest scope

Scope size: pageContest <request <session <application

el expression method

${
    
    域中的key值}	//通过key获取四个域中的value(优先最小域)
${
    
    对象.属性}		//获取对象中的属性值  (user.name)
${
    
    对象.对象.属性}	//获取对象里面对象的属性值 (user.birthday.year)
${
    
    数组名[0].name}	//通过下标获取数组中的对象,再获取值

Case:

User class

public class User {
    
    
    private String name;
    private int age;
    private String sex;
    private Birthday birthday;
}

Birthday class

public class Birthday {
    
    
    private int year;
    private int month;
    private int day;
}

Jsp page (write java code directly on jsp page here)

Import package

<%@ page import="com.xgf.bean.User" %>
<%@ page import="com.xgf.bean.Birthday" %>

Add values ​​to the action:

	<%
		//存入值
		request.setAttribute("value","");
		//存入对象
        User user = new User("striveday01",20,"男",new Birthday(2000,10,23));
        request.setAttribute("user", user);

		ArrayList<User> userList = new ArrayList<User>();
        userList .add(new User("list_User01",22,"男",new Birthday(1998,12,23)));
        userList .add(new User("list_User02",21,"女",new Birthday(1999,12,23)));
        userList .add(new User("list_User03",20,"女",new Birthday(2000,12,23)));
        //存入数组
		request.setAttribute("userList ", userList );
    %>

Get the value with EL expression in jsp

${
    
    value}			//直接获取值
${
    
    user.name}		//获取对象的name属性值
${
    
    user.birthday.year}	//获取user对象下的birthday对象下的year属性
${
    
    userList[0].name}		//获取数组对象里面的user的name值

Operators of EL expression [emphasis***]

  1. Java operator
    in the operator can write java, billing data '{} operator may write java, billing data `The can to write J A V A transport operator symbol , for the number of data into rows junction Operators ' {}' 1 + 1
  2. empty keyword
    • Used to determine whether the object is empty
    • Determine the number of elements in the set is 0

    String type: empty string and null are empty
    Java Bean object: null is empty
    Collection: if there is no data in the collection, it is empty

EL expression-get project access path[***]

  1. Get the name of the web application in the Servlet:
request.getContextPath();
  1. Get the name of the web application on the jsp page
${
    
    pageContext.request.contextPath}

Guess you like

Origin blog.csdn.net/qq_40542534/article/details/108597182