JSP EL表达式使用:

Servlet:

package com.stono.servlet;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.stono.servlet.bean.Dog;
import com.stono.servlet.bean.Person;
import com.stono.servlet.bean.Toy;
public class TestServlet6 extends HttpServlet {
private static final long serialVersionUID = 1L;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
Person person = new Person();
Dog dog = new Dog();
Toy toy = new Toy();
toy.setName("toyName");
dog.setName("dogName");
dog.setToy(toy);
person.setDog(dog);
person.setName("personName");
req.setAttribute("person", person);
String[] musics = new String[] { "m1", "m2", "m3" };
req.setAttribute("musics", musics);
Map<String, Object> map = new HashMap<String, Object>();
map.put("a", "ma");
map.put("b", "mb");
map.put("c", "mc");
req.setAttribute("map", map);
Map<String, Object> map2 = new HashMap<String, Object>();
map2.put("a", "m2a");
req.setAttribute("map2", map2);
req.setAttribute("aref", "a");
RequestDispatcher dispatcher = req.getRequestDispatcher("index016.jsp");
dispatcher.forward(req, resp);
}
}

JSP:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
pageContext.setAttribute("pca", "pageContextAttribute");
//session.setAttribute("s", "session");
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>016</title>
</head>
<body>
<p>JSP生成的java文件中,EL表达式使用统一的方式进行数据读取;</p>
<p>EL result is : ${pageScope.pca }</p>
<p>person.name is : ${person.name }</p>
<p>person.dog.name is : ${person.dog.name }</p>
<p>musics[0] is : ${musics[0]}</p>
<p>musics["1"] is : ${musics["1"]}</p>
<p>musics["2"] is : ${musics[1+1]}</p>
<p>map[a] is : ${map[a] }</p>
<p>map["a"] is : ${map["a"] }</p>
<p>map.a is : ${map.a }</p>
<p>[]中没有引号,就会计算括号中的内容:</p>
<p>map2[aref] is : ${map2[aref] }</p>
<p>map2["aref"] is : ${map2["aref"] }</p>
<p>person.dog.toy.name is : ${person.dog.toy.name}</p>
<p>XXX is : ${XXX }</p>
<p>
${pageScope }
${requestScope }
${sessionScope }
${applicationScope }
${param }
${paramValues }
${header }
${headerValues }
${cookie }
${initParam }
${pageContext }
</p>
</body>
</html>

JSP转换生成的Java:

/*
* Generated by the Jasper component of Apache Tomcat
* Version: Apache Tomcat/7.0.35
* Generated at: 2015-10-30 00:31:49 UTC
* Note: The last modified time of this file was set to
* the last modified time of the source file after
* generation to assist with modification tracking.
*/
package org.apache.jsp; import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.jsp.*;
import java.util.*; public final class index016_jsp extends org.apache.jasper.runtime.HttpJspBase
implements org.apache.jasper.runtime.JspSourceDependent { private static final javax.servlet.jsp.JspFactory _jspxFactory =
javax.servlet.jsp.JspFactory.getDefaultFactory(); private static java.util.Map<java.lang.String,java.lang.Long> _jspx_dependants; private javax.el.ExpressionFactory _el_expressionfactory;
private org.apache.tomcat.InstanceManager _jsp_instancemanager; public java.util.Map<java.lang.String,java.lang.Long> getDependants() {
return _jspx_dependants;
} public void _jspInit() {
_el_expressionfactory = _jspxFactory.getJspApplicationContext(getServletConfig().getServletContext()).getExpressionFactory();
_jsp_instancemanager = org.apache.jasper.runtime.InstanceManagerFactory.getInstanceManager(getServletConfig());
} public void _jspDestroy() {
} public void _jspService(final javax.servlet.http.HttpServletRequest request, final javax.servlet.http.HttpServletResponse response)
throws java.io.IOException, javax.servlet.ServletException { final javax.servlet.jsp.PageContext pageContext;
javax.servlet.http.HttpSession session = null;
final javax.servlet.ServletContext application;
final javax.servlet.ServletConfig config;
javax.servlet.jsp.JspWriter out = null;
final java.lang.Object page = this;
javax.servlet.jsp.JspWriter _jspx_out = null;
javax.servlet.jsp.PageContext _jspx_page_context = null; try {
response.setContentType("text/html;charset=UTF-8");
pageContext = _jspxFactory.getPageContext(this, request, response,
null, true, 8192, true);
_jspx_page_context = pageContext;
application = pageContext.getServletContext();
config = pageContext.getServletConfig();
session = pageContext.getSession();
out = pageContext.getOut();
_jspx_out = out; out.write('\r');
out.write('\n'); String path = request.getContextPath();
String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
pageContext.setAttribute("pca", "pageContextAttribute");
//session.setAttribute("s", "session"); out.write("\r\n");
out.write("<!DOCTYPE html>\r\n");
out.write("<html>\r\n");
out.write("<head>\r\n");
out.write("<meta charset=\"UTF-8\">\r\n");
out.write("<title>016</title>\r\n");
out.write("</head>\r\n");
out.write("<body>\r\n");
out.write("\t<p>EL result is : ");
out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${pageScope.pca }", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null, false));
out.write("</p>\r\n");
out.write("\t<p>person.name is : ");
out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${person.name }", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null, false));
out.write("</p>\r\n");
out.write("\t<p>person.dog.name is : ");
out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${person.dog.name }", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null, false));
out.write("</p>\r\n");
out.write("\t<p>musics[0] is : ");
out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${musics[0]}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null, false));
out.write("</p>\r\n");
out.write("\t<p>musics[\"1\"] is : ");
out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${musics[\"1\"]}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null, false));
out.write("</p>\r\n");
out.write("\t<p>musics[\"2\"] is : ");
out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${musics[1+1]}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null, false));
out.write("</p>\r\n");
out.write("\t<p>map[a] is : ");
out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${map[a] }", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null, false));
out.write("</p>\r\n");
out.write("\t<p>map[\"a\"] is : ");
out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${map[\"a\"] }", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null, false));
out.write("</p>\r\n");
out.write("\t<p>map.a is : ");
out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${map.a }", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null, false));
out.write("</p>\r\n");
out.write("\t<p>[]中没有引号,就会计算括号中的内容:</p>\r\n");
out.write("\t<p>map2[aref] is : ");
out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${map2[aref] }", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null, false));
out.write("</p>\r\n");
out.write("\t<p>map2[\"aref\"] is : ");
out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${map2[\"aref\"] }", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null, false));
out.write("</p>\r\n");
out.write("\t<p>person.dog.toy.name is : ");
out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${person.dog.toy.name}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null, false));
out.write("</p>\r\n");
out.write("\t<p>\r\n");
out.write("\t\t");
out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${pageScope }", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null, false));
out.write("\r\n");
out.write("\t\t");
out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${requestScope }", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null, false));
out.write("\r\n");
out.write("\t\t");
out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${sessionScope }", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null, false));
out.write("\r\n");
out.write("\t\t");
out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${applicationScope }", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null, false));
out.write("\r\n");
out.write("\t\t");
out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${param }", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null, false));
out.write("\r\n");
out.write("\t\t");
out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${paramValues }", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null, false));
out.write("\r\n");
out.write("\t\t");
out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${header }", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null, false));
out.write("\r\n");
out.write("\t\t");
out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${headerValues }", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null, false));
out.write("\r\n");
out.write("\t\t");
out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${cookie }", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null, false));
out.write("\r\n");
out.write("\t\t");
out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${initParam }", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null, false));
out.write("\r\n");
out.write("\t\t");
out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${pageContext }", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null, false));
out.write("\r\n");
out.write("\t</p>\r\n");
out.write("</body>\r\n");
out.write("</html>\r\n");
} catch (java.lang.Throwable t) {
if (!(t instanceof javax.servlet.jsp.SkipPageException)){
out = _jspx_out;
if (out != null && out.getBufferSize() != 0)
try { out.clearBuffer(); } catch (java.io.IOException e) {}
if (_jspx_page_context != null) _jspx_page_context.handlePageException(t);
else throw new ServletException(t);
}
} finally {
_jspxFactory.releasePageContext(_jspx_page_context);
}
}
}

JSP EL表达式使用的更多相关文章

  1. Jsp,EL表达式的入门

    Jsp,EL表达式的入门 *Servlet/JSP 是两种动态的WEB资源的两种技术 使用Servlet生成HTML的页面是可以的 response.getWriter("<form ...

  2. jsp EL表达式 字符串的比较

    jsp EL表达式 字符串的比较 跟JavaScript一样,直接使用两个等于号即可:== 代码如下: <c:if test="${highLight == 'visa'}" ...

  3. JSP EL表达式 获得 request的GET/POST方法

    JSP EL表达式 获得 request的GET/POST方法: 不在requestScopse中: <p>得到request的方法</p> <p>pageCont ...

  4. JSP EL表达式 param、paramValues的使用

    JSP EL表达式 param.paramValues的使用: <%@ page language="java" import="java.util.*" ...

  5. JSP EL表达式忽略方法

    JSP EL表达式忽略方法: web.xml中,和jsp中:jsp中的等级要高一些: web.xml: <?xml version="1.0" encoding=" ...

  6. jsp EL表达式和JSTL标签if-else if-else用法

    java web javaEE ,jsp EL表达式和JSTL标签if-else if-else四种用法一.条件运算符 ${user.gender==?'男':'女'} 二.if() <c:if ...

  7. 11 JSP/EL表达式/EL函数

    JSP      * 概述: JSP(Java Server Pages)与Java Servlet一样,是在服务器端执行的不同的是先由服务器编译部署成Servlet执行      * JSP的运行原 ...

  8. JSP EL表达式(转)

    一.EL简介 1.语法结构     ${expression}2.[]与.运算符     EL 提供.和[]两种运算符来存取数据.    当要存取的属性名称中包含一些特殊字符,如.或?等并非字母或数字 ...

  9. JSP EL表达式

    1 EL表达式简介 EL 全名为Expression Language.EL主要作用: 1.获取数据 EL表达式主要用于替换JSP页面中的脚本表达式,以从各种类型的web域 中检索java对象.获取数 ...

随机推荐

  1. ecos的mvcl

    m 数据模型抽象层 v 视图 c 控制器 l 业务逻辑 mvc与mvcl区别 mvc中的m是mvcl中m+l

  2. PAT (Advanced Level) 1029. Median (25)

    scanf读入居然会超时...用了一下输入挂才AC... #include<cstdio> #include<cstring> #include<cmath> #i ...

  3. JavaScript(三)---- 控制流程语句

    常用的控制流程语句有判断语句.分支语句.循环语句.基本用法都和java中的一致,switch有几点特殊. 1.判断语句 格式:        if(判断条件){            符合条件执行的代 ...

  4. PHP学习笔记--1,不总结,不掌握,不明白!

    不总结,不掌握,不明白! 前言: 学php一开始就是语法,变量,数组,函数,OOP(面向对象[封装,继承,多态,抽象]) 这些都是最基础的东西,但你还要懂一些在实际开发中要用的东西,比如基本的HTML ...

  5. css一行内容过多显示省略号

    四个缺一不可. 效果如下图

  6. css中元素水平垂直居中4种方法介绍

    table-cell轻松设置文本图片水平垂直居中 让一个元素垂直居中的思路:把这个元素的容器设置为table-cell,也就是具有表格单元格的特性,再使用vertical-align(这个属性对blo ...

  7. UWSGITOP-----监控uwsgi 性能

    启动 uwsgi -x etc/bfdds_cookiemapping_conf.xml --stats /tmp/stats.socket 查看 uwsgitop /tmp/stats.socket ...

  8. Quick Cocos2dx controller的初步实现

    很久没有记笔记了,今天记一下,最近都在瞎忙活,都不知道自己干了些啥. 我的Controller是在官方的mvc sample的里面的PlayerDualController上更改的,所以很多地方还没来 ...

  9. unity3d 之本地推送

    1. 本地推送主要包括在android和ios上,下面所有的代码都是本人写的,经过测试是没有问题的,已经运用到项目中了.首先是接口INotification: using System; public ...

  10. sed与正则用法收集

    1.将文本每行最后七个字符换成!号 sed   -n  's#.\{7\}$#!#p' ooo 在文本的每一行前添加#符号 sed  's/^.\?/#&/' passwd &  替代 ...