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 style="color:#003399;">param</span>.username}<br /> 密码:${<span style="color:#003399;">param</span>.password}<br /> 姓名:${<span style="color:#003399;">param</span>.name}<br /> 性别:${<span style="color:#003399;">param</span>.sex}<br /> 爱好:${<span style="color:#003399;">paramValues</span>.interest[0]} ${<span style="color:#003399;">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 style="color:#000000;"><%@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>