欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

实例解析JSP中EL表达式的各种运用

程序员文章站 2023-11-07 12:45:40
el运算符: <%@page language="java" contenttype="text/html;charset=gb2312"%>...

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>