<%@ page contentType="text/html" pageEncoding="GBK"%> <html> <head><title>设置同名属性</title></head> <body> <% pageContext.setAttribute("info", "page属性范围"); request.setAttribute("info", "request属性范围"); session.setAttribute("info", "session属性范围"); application.setAttribute("info", "application属性范围"); %> <h3>${info}</h3> <!--按顺序输出第一个是page属性--> <h3>page属性内容:${pageScope.info}</h3> <!--表达式输出--> <h3>request属性内容:${requestScope.info}</h3> <h3>session属性内容:${sessionScope.info}</h3> <h3>applicatino属性内容:${applicationScope.info}</h3> </body> </html>
亲,原理又是反射哦!!!
还有contextPage这家伙有号召力,可以取得request、session、application的实例(实力)!
<%@ page contentType="text/html" pageEncoding="GBK"%> <html> <head><title>调用内置对象方法</title></head> <body> <h3>IP地址:${pageContext.request.remoteAddr}</h3> <h3>session ID:${pageContext.session.id}</h3> <h3>是否是新的session:${pageContext.session.new}</h3> </body> </html>
<%@ page contentType="text/html" pageEncoding="GBK"%> <html> <head><title>接收参数</title></head> <body> <h3>通过内置对象接收参数:<%=request.getParameter("ref")%></h3> <h3>通过表达式取得参数:${param.ref}/<h3> </body> </html>
取得一组参数:
<html> <head><title>定义表单,复选框含有多个参数</title></head> <body> <form action="getParams_demo.jsp" method="post"> 兴趣: <input type="checkbox" name="inst" value="编程">编程 <input type="checkbox" name="inst" value="唱歌">唱歌 <input type="checkbox" name="inst" value="跳舞">跳舞 <input type="checkbox" name="inst" value="游泳">游泳 <input type="checkbox" name="inst" value="看电影">看电影 <input type="submit" value="提交"> </form> </body> <html>
提交后,见证奇迹的时刻:
<%@ page contentType="text/html" pageEncoding="GBK" import="java.util.*"%> <html> <head><title>内容</title></head> <% request.setCharacterEncoding("GBK");%> <body> <% Map map = new HashMap(); map.put("zz", "ZhangZe"); map.put("csdn", "www.csdn.net"); map.put("sb", "sb is a sb"); request.setAttribute("info", map); // 在request范围内保存集合 %> <h3>Key为zz的内容:${info.zz}</h3> <h3>Key为csdn的内容:${info["csdn"]}</h3> <h3>Key为sb的内容:${info.sb}</h3> </body> </html>
Unterminated ${ tag 未终止
String literal is not properly closed by a double-quote
双引号
还是反射!!!
EL的强大在于可以直接通过反射方式调用保存在属性范围中的Java对象内容。
现定义一个VO类
package zz.el; public class Info{ private String name; private float sal; public void setName(String name){ this.name = name; } public String getName(){ System.out.println("运用反射调用getter()方法"); return this.name; } public void setSal(float sal){ this.sal = sal; } public float getSal(){ System.out.println("运用反射调用getSal()方法"); return this.sal; } }
将此对象保存在属性范围中,并通过表达式输出――print_vo.jsp
<%@ page contentType="text/html" pageEncoding="GBK" import="zz.el.Info"%> <html> <head><title>内容</title></head> <% request.setCharacterEncoding("GBK");%> <body> <% Info info = new Info(); info.setName("张三四"); info.setSal(Float.parseFloat("8888.8")); request.setAttribute("zinfo", info); // 保存 %> <h3>姓名:${zinfo.name}</h3> <h3>工资:${zinfo.sal}</h3> </body> </html>
观察Tomcat后台,打印出在Getter()方法中系统输出语句,证明:表达式可以直接通过反射调用保存在属性范围内的Java对象内容!
ELListServlet.java
package zz.el; import java.util.List; import java.util.ArrayList; import java.io.IOException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.ServletException; import zz.el.Emp; public class ELListServlet extends HttpServlet{ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, java.io.IOException{ List<Emp> all = new ArrayList<Emp>(); // 实例化List对象 Emp emp = null; // 定义Emp对象 emp = new Emp(); // 重新实例化vo对象?如何区分 emp.setName("张五六"); // 设置相应属性 emp.setSal(5656); all.add(emp); // 向集合List中增加emp属性 emp = new Emp(); // 实例化vo对象 emp.setName("张三四"); // 设置相应属性 emp.setSal(88888); all.add(emp); // 向集合List中增加emp属性 request.setAttribute("allEmp", all); // 设置request属性 request.getRequestDispatcher("emp_list.jsp") .forward(request, response); // 通过服务器跳转传递request属性 } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, java.io.IOException{ this.doGet(request, response); } }
配置D:\ProgramFiles\webdemo\WEB-INF\web.xml
<servlet> <servlet-name>ELListServlet</servlet-name> <servlet-class>zz.el.ELListServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>ELListServlet</servlet-name> <url-pattern>/10_El/ELListServlet</url-pattern> <!--页面的映射路径--> </servlet-mapping>
运用表达式输出集合D:\ProgramFiles\webdemo\10_El \emp_list.jsp
<%@ page contentType="text/html" pageEncoding="GBK" import="java.util.*"%> <html> <head> <title>运用表达式输出集合</title> </head> <body> <% List all = (List)request.getAttribute("allEmp"); // 接收List集合 if (all != null){ // 判断集合是否为空,否则会出现NullPointerExcepiton异常 %> <table border="1" width="60%"> <tr> <td>姓名</td> <td>工资</td> </tr> <% Iterator iter = all.iterator(); while (iter.hasNext()){ pageContext.setAttribute("emp", iter.next()); // 设置page属性 %> <tr> <td>${emp.name}</td> <td>${emp.sal}</td> </tr> <% } %> </table> <% } %> </body> </html>
An error occurred at line: 9 in the jsp file: /10_El/emp_list.jsp
Emp cannot be resolved to a type
6:
7: <body>
8: <%
9: List<Emp> all = (List)request.getAttribute("allEmp"); // 接收List集合
在jsp页面输出时。不需要再是泛型!
将每一个取出的对象(Object)存放在page范围中(因为每一个要输出的内容只有在本页面才有效)之后再通过表达式输出即可。
à少一个all.add(emp); //向集合List中增加emp属性
易错:<servlet>爱打错成<servelt>
忘记这是什么<url-pattern>/10_El/ELListServlet</url-pattern> <!--页面的映射路径-->
比较:
http://localhost/demo/10_El/emp_list.jsp
无结果输出
http://localhost/demo/10_El/ELListServlet
注释掉第二个实例化语句,观察结果:
//emp = new Emp(); //重新实例化vo对象?如何区分
前面设置的属性被覆盖