<%@ page language="java" import="java.util.*" pageEncoding="UTF-"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@page import="me.gacl.domain.Person"%>
<%@page import="me.gacl.domain.Address"%>
<!DOCTYPE HTML>
<html>
<head>
<title>el表达式获取数据</title>
</head>
<body>
<%
request.setAttribute("name","孤傲苍狼");
%>
<%--${name}等同于pageContext.findAttribute("name") --%>
使用EL表达式获取数据:${name}
<hr>
<!-- 在jsp页面中,使用el表达式可以获取bean的属性 -->
<%
Person p = new Person();
p.setAge();
request.setAttribute("person",p);
%>
使用el表达式可以获取bean的属性:${person.age}
<hr>
<!-- 在jsp页面中,使用el表达式可以获取bean中的。。。。。。。。。的属性 -->
<%
Person person = new Person();
Address address = new Address();
person.setAddress(address);
request.setAttribute("person",person);
%>
${person.address.name}
<hr>
<!-- 在jsp页面中,使用el表达式获取list集合中指定位置的数据 -->
<%
Person p = new Person();
p.setName("孤傲苍狼");
Person p = new Person();
p.setName("白虎神皇");
List<Person> list = new ArrayList<Person>();
list.add(p);
list.add(p);
request.setAttribute("list",list);
%>
<!-- 取list指定位置的数据 -->
${list[].name}
<!-- 迭代List集合 -->
<c:forEach var="person" items="${list}">
${person.name}
</c:forEach>
<hr>
<!-- 在jsp页面中,使用el表达式获取map集合的数据 -->
<%
Map<String,String> map = new LinkedHashMap<String,String>();
map.put("a","aaaaxxx");
map.put("b","bbbb");
map.put("c","cccc");
map.put("","aaaa");
request.setAttribute("map",map);
%>
<!-- 根据关键字取map集合的数据 -->
${map.c}
${map[""]}
<hr>
<!-- 迭代Map集合 -->
<c:forEach var="me" items="${map}">
${me.key}=${me.value}<br/>
</c:forEach>
<hr>
</body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@page import="me.gacl.domain.User"%>
<!DOCTYPE HTML>
<html>
<head>
<title>el表达式运算符</title>
</head>
<body>
<h>el表达式进行四则运算:</h>
加法运算:${+}<br/>
减法运算:${-}<br/>
乘法运算:${*}<br/>
除法运算:${/}<br/>
<h>el表达式进行关系运算:</h>
<%--${user == null}和 ${user eq null}两种写法等价--%>
${user == null}<br/>
${user eq null}<br/>
<h>el表达式使用empty运算符检查对象是否为null(空)</h>
<%
List<String> list = new ArrayList<String>();
list.add("gacl");
list.add("xdp");
request.setAttribute("list",list);
%>
<%--使用empty运算符检查对象是否为null(空) --%>
<c:if test="${!empty(list)}">
<c:forEach var="str" items="${list}">
${str}<br/>
</c:forEach>
</c:if>
<br/>
<%
List<String> emptyList = null;
%>
<%--使用empty运算符检查对象是否为null(空) --%>
<c:if test="${empty(emptyList)}">
对不起,没有您想看的数据
</c:if>
<br/>
<h>EL表达式中使用二元表达式</h>
<%
session.setAttribute("user",new User("孤傲苍狼"));
%>
${user==null? "对不起,您没有登陆 " : user.username}
<br/>
<h>EL表达式数据回显</h>
<%
User user = new User();
user.setGender("male");
//数据回显
request.setAttribute("user",user);
%>
<input type="radio" name="gender" value="male" ${user.gender=='male'?'checked':''}>男
<input type="radio" name="gender" value="female" ${user.gender=='female'?'checked':''}>女
<br/> </body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-"%>
<!DOCTYPE HTML>
<html>
<head>
<title>el隐式对象</title>
</head>
<body>
<br/>---------------、pageContext对象:获取JSP页面中的pageContext对象------------------------<br/>
${pageContext}
<br/>---------------、pageScope对象:从page域(pageScope)中查找数据------------------------<br/>
<%
pageContext.setAttribute("name","孤傲苍狼"); //map
%>
${pageScope.name}
<br/>---------------、requestScope对象:从request域(requestScope)中获取数据------------------------<br/>
<%
request.setAttribute("name","白虎神皇"); //map
%>
${requestScope.name}
<br/>---------------、sessionScope对象:从session域(sessionScope)中获取数据------------------------<br/>
<%
session.setAttribute("user","xdp"); //map
%>
${sessionScope.user}
<br/>---------------、applicationScope对象:从application域(applicationScope)中获取数据------------------------<br/>
<%
application.setAttribute("user","gacl"); //map
%>
${applicationScope.user}
<br/>--------------、param对象:获得用于保存请求参数map,并从map中获取数据------------------------<br/>
<!-- http://localhost:/JavaWeb_EL_Study_/ELDemo.jsp?name=aaa -->
${param.name}
<!-- 此表达式会经常用在数据回显上 -->
<form action="${pageContext.request.contextPath}/servlet/RegisterServlet" method="post">
<input type="text" name="username" value="${param.username}">
<input type="submit" value="注册">
</form>
<br/>--------------、paramValues对象:paramValues获得请求参数 //map{"",String[]}------------------------<br/>
<!-- http://localhost:/JavaWeb_EL_Study_/ELDemo.jsp?like=aaa&like=bbb -->
${paramValues.like[]}
${paramValues.like[]}
<br/>--------------、header对象:header获得请求头------------------------<br/>
${header.Accept}<br/>
<%--${header.Accept-Encoding} 这样写会报错
测试headerValues时,如果头里面有“-” ,例Accept-Encoding,则要headerValues[“Accept-Encoding”]
--%>
${header["Accept-Encoding"]}
<br/>--------------、headerValues对象:headerValues获得请求头的值------------------------<br/>
<%--headerValues表示一个保存了所有http请求头字段的Map对象,它对于某个请求参数,返回的是一个string[]数组
例如:headerValues.Accept返回的是一个string[]数组 ,headerValues.Accept[]取出数组中的第一个值
--%>
${headerValues.Accept[]}<br/>
<%--${headerValues.Accept-Encoding} 这样写会报错
测试headerValues时,如果头里面有“-” ,例Accept-Encoding,则要headerValues[“Accept-Encoding”]
headerValues["Accept-Encoding"]返回的是一个string[]数组,headerValues["Accept-Encoding"][]取出数组中的第一个值
--%>
${headerValues["Accept-Encoding"][]}
<br/>--------------、cookie对象:cookie对象获取客户机提交的cookie------------------------<br/>
<!-- 从cookie隐式对象中根据名称获取到的是cookie对象,要想获取值,还需要.value -->
${cookie.JSESSIONID.value} //保存所有cookie的map
<br/>--------------、initParam对象:initParam对象获取在web.xml文件中配置的初始化参数------------------------<br/>
<%--
<!-- web.xml文件中配置初始化参数 -->
<context-param>
<param-name>xxx</param-name>
<param-value>yyyy</param-value>
</context-param>
<context-param>
<param-name>root</param-name>
<param-value>/JavaWeb_EL_Study_</param-value>
</context-param>
--%>
<%--获取servletContext中用于保存初始化参数的map --%>
${initParam.xxx}<br/>
${initParam.root}
</body>
</html>
package me.gacl.web.controller;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class RegisterServlet extends HttpServlet {
/*
* 处理用户注册的方法
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//、接收参数
String userName = request.getParameter("username");
/**
* 、直接跳转回/ELDemo.jsp页面,没有使用request.setAttribute("userName", userName)将userName存储到request对象中
* 但是在ELDemo.jsp页面中可以使用${param.username}获取到request对象中的username参数的值
*/
request.getRequestDispatcher("/ELDemo.jsp").forward(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
package me.gacl.util;
/**
* @ClassName: HtmlFilter
* @Description: html转义处理工具类
* @author: 孤傲苍狼
* @date: -- 上午::
*
*/
public class HtmlFilter {
/**
* @Method: filter
* @Description: 静态方法,html标签转义处理
* @Anthor:孤傲苍狼
*
* @param message 要转义的内容
* @return 转义后的内容
*/
public static String filter(String message) {
if (message == null)
return (null);
char content[] = new char[message.length()];
message.getChars(, message.length(), content, );
StringBuffer result = new StringBuffer(content.length + );
for (int i = ; i < content.length; i++) {
switch (content[i]) {
case '<':
result.append("<");
break;
case '>':
result.append(">");
break;
case '&':
result.append("&");
break;
case '"':
result.append(""");
break;
default:
result.append(content[i]);
}
}
return (result.toString());
}
}
<?xml version="." encoding="UTF-"?> <taglib version="." xmlns="http://java.sun.com/xml/ns/jee" xmlns:xsi="http://www.w.org//XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/jee web-jsptaglibrary__.xsd"> <tlib-version>.</tlib-version> <short-name>EL Function</short-name> <!-- 自定义EL函数库的引用URI, 在JSP页面中可以这样引用:<%@taglib uri="/ELFunction" prefix="fn" %> --> <uri>/ELFunction</uri> <!--<function>元素用于描述一个EL自定义函数 --> <function> <description>html标签转义处理方法</description> <!--<name>子元素用于指定EL自定义函数的名称--> <name>filter</name> <!--<function-class>子元素用于指定完整的Java类名--> <function-class>me.gacl.util.HtmlFilter</function-class> <!--<function-signature>子元素用于指定Java类中的静态方法的签名, 方法签名必须指明方法的返回值类型及各个参数的类型,各个参数之间用逗号分隔。--> <function-signature>java.lang.String filter(java.lang.String)</function-signature> </function> </taglib>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-"%>
<%--引入EL自定义函数库 --%>
<%@taglib uri="/ELFunction" prefix="fn" %>
<!DOCTYPE HTML>
<html>
<head>
<title>使用EL调用Java方法</title>
</head>
<body>
<%--使用EL调用filter方法--%>
${fn:filter("<a href=''>点点</a>")}
</body>
</html>
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有