EL運算符:
<%@page language="java" contentType="text/html;charset=gb2312"%> <%@page import="java.sql.*" %> <!DOCTYPE html> <html> <head> <title>El算數運算符</title> </head> <body> <table border=1> <tr> <td>算數運算符</td> <td>結果</td> </tr> <tr> <td>加法運算符\${1 + 1}</td> <td>結果${1+1}</td> </tr> <tr> <td>減法運算符\${1 - 1}</td> <td>結果${1-1}</td> </tr> <tr> <td>乘法運算符\${1 * 2}</td> <td>結果${1 * 1}</td> </tr> <tr> <td>除法運算符\${3 / 2}</td> <td>結果${3 / 2}</td> </tr> <tr> <td>求余運算符\${3 % 2}</td> <td>結果${3 % 2}</td> </tr> </table> <table border=1> <tr> <td>關系運算符</td> <td>結果</td> </tr> <tr> <td>等於運算符\${1 == 1}</td> <td>結果${1==1}</td> </tr> <tr> <td>不等於運算符\${1 != 1}</td> <td>結果${1!=1}</td> </tr> <tr> <td>小於運算符\${1 < 2}</td> <td>結果${1 < 2}</td> </tr> <tr> <td>大於運算符\${3 > 2}</td> <td>結果${3 > 2}</td> </tr> <tr> <td>小於等於運算符\${1 <= 2}</td> <td>結果${1 <= 2}</td> </tr> <tr> <td>大於等於運算符\${3 >= 2}</td> <td>結果${3 >= 2}</td> </tr> </table> <table border=1> <tr> <td>邏輯運算符</td> <td>結果</td> </tr> <tr> <td colspan="2">邏輯運算符&&</td> </tr> <tr> <td>\${true && true}</td> <td>結果${true && true}</td> </tr> <tr> <td>\${true && false}</td> <td>結果${true && false}</td> </tr> <tr> <td>\${false && false}</td> <td>結果${false && false}</td> </tr> <tr> <td colspan="2">邏輯運算符||</td> </tr> <tr> <td>\${true || true}</td> <td>結果${true || true}</td> </tr><tr> <td>\${true || false}</td> <td>結果${true || false}</td> </tr><tr> <td>\${false || false}</td> <td>結果${false || false}</td> </tr> <tr> <td colspan="2">邏輯運算符!</td> </tr> <tr> <td>\${!true}</td> <td>結果${!true}</td> </tr><tr> <td>\${!false}</td> <td>結果${!false}</td> </tr> </table> <table border=1> <tr> <td>條件運算符</td> <td>結果</td> </tr> <tr> <td>\${2>1?"Yes":"No"}</td> <td>結果${2>1?"Yes":"No"}</td> </tr> <tr> <td>\${2<1?"Yes":"No"}</td> <td>結果${2<1?"Yes":"No"}</td> </tr> </table> <table border=1> <tr> <td>驗證運算符</td> <td>結果</td> </tr> <tr> <td>\${empty param.username}</td> <td>結果${empty param.username}</td> </tr> <tr> <td>\${empty param.password}</td> <td>結果${empty param.password}</td> </tr> </table> <table border=1> <tr> <td>括號運算符及運算符的優先級</td> <td>結果</td> </tr> </table> </body> </html>
EL處理內置對象:
“.”和"[]"符號是用來存取數據的
${applicationScope.user.username} ${applicationScope.user[0].username} ${applicationScope.user["user-name"]}
通過變量來傳值就必須使用[]
${applicationScope.user["data"]}
與存儲有關的內置對象
<!DOCTYPE html> <html> <head> <title>與存儲有關的內置對象</title> </head> <body> <% //application范圍設置屬性name,值為application_name application.setAttribute("name","application_name"); //session范圍設置屬性name,值為session_name session.setAttribute("name","session_name"); //request范圍設置屬性name,值為request_name request.setAttribute("name","request_name"); //page范圍設置屬性name,值為page_name pageContext.setAttribute("name","page_name"); %> <%--獲取page范圍內的name屬性--%> page范圍內的那麼屬性的值為:${pageScope.name}<br /> <%--獲取request范圍內的name的屬性--%> request范圍內的那麼屬性的值為:${requestScope.name}<br /> <%--獲取session范圍內的name的屬性--%> session范圍內的那麼屬性的值為:${sessionScope.name}<br /> <%--獲取application范圍內的name屬性--%> application范圍內的那麼屬性的值為:${applicationScope.name}<br /> </body> </html>
與輸入有關的內置對象
<!DOCTYPE html> <html> <head> <title>用戶表單</title> </head> <body> <form action="UserDemo.jsp" method="post"> 用戶名:<input type="text" name="username" /><br /> 密碼:<input type="password" name="password" /><br /> 姓名:<input type="text" name="name" /><br /> 性別:<input type="radio" name="sex" value="男">男<input type="radio" name="sex" value="女">女<br /> 愛好:<br /> <input type="checkbox" name="interest" value="打籃球">打籃球<br /> <input type="checkbox" name="interest" value="看書">看書<br /> <input type="checkbox" name="interest" value="旅行">旅行<br /> <input type="checkbox" name="interest" value="編程">編程<br /> <input type="submit" value="提交"> <input type="reset" value="重置"> </form> </body> </html> <!DOCTYPE html> <html> <head> <title>接收用戶參數</title> </head> <body> <% //設置頁面編碼格式 request.setCharacterEncoding("gb2312"); %> <%--接收用戶參數--%> 用戶名:${<span >param</span>.username}<br /> 密碼:${<span >param</span>.password}<br /> 姓名:${<span >param</span>.name}<br /> 性別:${<span >param</span>.sex}<br /> 愛好:${<span >paramValues</span>.interest[0]} ${<span >paramValues</span>.interest[1]} </body> </html>
cookie內置對象
<!DOCTYPE html> <html> <head> <title>設置cookie的值</title> </head> <body> <% //設置cookie的值 Cookie c = new Cookie("username","root"); //添加cookie到客戶端 response.addCookie(c); %> <a href="getCookieDemo.jsp">顯示cookie的值</a> </body> </html> <span ><%@page language="java" contentType="text/html;charset=gb2312"%> <!DOCTYPE html> <html> <head> <title>取得cookie的值</title> </head> <body> cookie中的username的值為:${cookie.username.value} </body> </html>
header內置對象
<!DOCTYPE html> <html> <head> <title>取得header的值</title> </head> <body> ${header["host"]}<br /> ${header["user-agent"]}<br /> </body> </html>
initParam內置對象:獲取web站點中設置的環境變量
<%@page language="java" contentType="text/html;charset=gb2312"%> <!DOCTYPE html> <html> <head> <title>獲得環境參數</title> </head> <body> username參數值:${initParam.username}<br /> </body> </html> <?xml version="1.0" encoding="ISO-8859-1"?> <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5"> <context-param> <param-name>username</param-name> <param-value>Zhangdapeng</param-value> </context-param> </web-app>
pageContex內置對象
用來取得有關用戶請求和頁面的詳細信息
<%@page language="java" contentType="text/html;charset=gb2312"%> <!DOCTYPE html> <html> <head> <title>pageContext演示</title> </head> <body> <table border="1"> <tr> <td>取得請求的參數的字符串</td> <td>${pageContext.request.queryString}</td> </tr><tr> <td>取得請求URL</td> <td>${pageContext.request.requestURL}</td> </tr><tr> <td>取得web應用名稱</td> <td>${pageContext.request.contextPath}</td> </tr><tr> <td>取得HTTP請求方式(POST/GET)</td> <td>${pageContext.request.method}</td> </tr><tr> <td>取得使用的協議</td> <td>${pageContext.request.protocol}</td> </tr><tr> <td>取得用戶IP地址</td> <td>${pageContext.request.remoteAddr}</td> </tr><tr> <td>判斷session是否為新</td> <td>${pageContext.session.new}</td> </tr><tr> <td>取得session的id</td> <td>${pageContext.session.id}</td> </tr> </table> </body> </html>
EL存取器:
使用存取器讀取JavaBean中的數據
package com.javaweb.ch08; import java.util.*; //一個簡單的JavaBean示例 public class Person{ //name屬性 private String name; //age屬性 private int age; //sex屬性 private String sex; //friends屬性 private ArrayList friends; //無參構造方法 public Person(){ } //自定義構造的方法 public Person(String name,int age,String sex){ this.name = name; this.age = age; this.sex = sex; } //獲取name的屬性值 public String getName(){ return name; } //獲取age屬性值 public int getAge(){ return age; } //獲取sex的屬性值 public String getSex(){ return sex; } //設置name的屬性 public void setName(String name){ this.name = name; } //設置age的屬性值 public void setAge(int age){ this.age = age; } //設置sex的屬性值 public void setSex(String sex){ this.sex = sex; } //設置friends的值 public void setFriends(ArrayList friends){ this.friends = friends; } //獲取friends的屬性值 public ArrayList getFriends(){ return friends; } }
<%@page language="java" contentType="text/html;charset=gb2312"%> <%@page import="java.util.*,com.javaweb.ch08.*" %> <!DOCTYPE html> <html> <head> <title>設置JavaBean中的屬性</title> </head> <body> <% //實例化一個Person Person person = new Person("zhangdapeng",24,"男"); //創建一個friends實例 ArrayList friends = new ArrayList(); //添加值 friends.add("wang wu"); friends.add("li si"); person.setFriends(friends); //存儲在session范圍內 session.setAttribute("person",person); %> <a href="GetJavaBean.jsp">跳轉到GetJavaBean.jsp</a> </body> </html> <%@page language="java" contentType="text/html;charset=gb2312"%> <!DOCTYPE html> <html> <head> <title>取得JavaBean中的屬性值</title> </head> <body> 姓名:${sessionScope.person.name}<br /> 年齡:${sessionScope.person.age}<br /> 性別:${sessionScope.person.sex}<br /> 朋友:${sessionScope.person.friends[0]},${sessionScope.person.friends[1]}<br /> </body> </html>
使用存儲器讀取Map中的數據
<%@page language="java" contentType="text/html;charset=gb2312"%> <%@page import="java.util.*,com.javaweb.ch08.*" %> <!DOCTYPE html> <html> <head> <title>設置Map頁面</title> </head> <body> <% //新建一個HashMap HashMap userInfo = new HashMap(); //在HashMap中設置值 userInfo.put("username","zhangdapeng"); userInfo.put("password","zhnagda123"); //將值存儲到session范圍中 session.setAttribute("userInfo",userInfo); %> <a href="GetMapDemo.jsp">跳轉到GetMapDemo.jsp</a> </body> </html> <%@page language="java" contentType="text/html;charset=gb2312"%> <!DOCTYPE html> <html> <head> <title>使用存儲器讀取map中的數據</title> </head> <body> 用戶名:${sessionScope.userInfo.username}<br /> 密碼:${sessionScope.userInfo.password}<br /> </body> </html>
使用存儲器讀取數組中的數據
<%@page language="java" contentType="text/html;charset=gb2312"%> <%@page import="java.util.*,com.javaweb.ch08.*" %> <!DOCTYPE html> <html> <head> <title>設置Array頁面</title> </head> <body> <% String[] names = {"zhangda1","zhangda2","zhangda3"}; //將值存儲到session范圍中 session.setAttribute("names",names); %> <a href="GetMapDemo.jsp">跳轉到GetArrayDemo.jsp</a> </body> </html> <%@page language="java" contentType="text/html;charset=gb2312"%> <!DOCTYPE html> <html> <head> <title>使用存儲器讀取map中的數據</title> </head> <body> 用戶名1:${sessionScope.names[0]}<br /> 用戶名2:${sessionScope.names[1]}<br /> </body> </html>
存儲器的復雜應用
<%@page language="java" contentType="text/html;charset=gb2312"%> <%@page import="java.util.*,com.javaweb.ch08.*" %> <!DOCTYPE html> <html> <head> <title>設置Array頁面</title> </head> <body> <% ArrayList<Person> persons = new ArrayList<Person>(); Person person1 = new Person("wang wu",24,"男"); Person person2 = new Person("wang liu",24,"女"); persons.add(person1); persons.add(person2); session.setAttribute("persons",persons); %> <a href="GetMapDemo.jsp">跳轉到GetArrayDemo.jsp</a> </body> </html> <%@page language="java" contentType="text/html;charset=gb2312"%> <!DOCTYPE html> <html> <head> <title>使用存儲器讀取map中的數據</title> </head> <body> 用戶名1:${sessionScope.persons[0].name},${sessionScope.persons[0].age},${sessionScope.persons[0].sex}<br /> 用戶名2:${sessionScope.persons[1].name},${sessionScope.persons[1].age},${sessionScope.persons[1].sex}<br /> </body> </html>