<%@page import="java.util.ArrayList"%>
<%@page import="java.util.List"%>
<%@page import="com.zking.entity.Goods"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!-- -->
<%@taglib uri="http://java.sun.com/jsp/jstl/core"prefix="c" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h2>域对象 pageContext request session application</h2>
<%
pageContext.setAttribute("aa", "张三");//作用域 当前jsp界面
request.setAttribute("aa", "李四");//转发有效 当前请求
session.setAttribute("aa", "王二"); //当前的会话( 浏览器打开和关闭)
application.setAttribute("aa", "郑五");//服务器的打开和关闭
//实例化一个商品对象
Goods g = new Goods();
//给对象赋值
g.setGid(1);
g.setGname("啤酒");
g.setGprice(3);
List<Goods>ls= new ArrayList<Goods>();
//把对象放集合中
ls.add(g);
//给对象赋值
Goods g1= new Goods();
g1.setGid(2);
g1.setGname("小龙虾");
g1.setGprice(99);
//把对象放集合中
ls.add(g1);
//把对象存起来
request.setAttribute("myls", ls);
//接收表单的值
request.setCharacterEncoding("utf-8");
String name=request.getParameter("sname");
String pwd=request.getParameter("spwd");
//调用业务逻辑层
//假设法
if("admin".equals(name)&&"123".equals(pwd)){
//说明登录成功
session.setAttribute("uname", name);
}
%>
<h3>jsp表达式取值如下</h3>
1.<%=pageContext.getAttribute("aa") %><br>
2.<%= request.getAttribute("aa") %><br>
3.<%=session.getAttribute("aa") %><br>
4.<%=application.getAttribute("aa") %><br>
<a href="a.jsp">跳转到a.jsp界面</a>
<%-- <jsp:forward page="a.jsp"></jsp:forward> --%>
<h2>EL表达式</h2>
<h3>EL表达式取值</h3>
1.${pageScope.aa}<br>
2.${requestScope.aa}<br>
3.${sessionScope.aa}<br>
4.${applicationScope.aa}<br>
<h3>EL运算符</h3>
${99+99}__${5>3}__${not empty myls}<!-- 判断是否为空 -->
<h3>EL表达式取对象的属性值</h3>
<!--能够点出来的一定是实体类中存在的属性名 -->
${goods.gname}__${goods.gid }__${goods.gprice}
<h3>没登录显示表单,登录显示欢迎你</h3>
<form action="index.jsp"method="post">
用户名:<input type="text"name="sname"><br>
密码:<input type="password"name="spwd"><br>
<input type="submit"value="登录">
<input type="reset"value="清空">
</form>
欢迎${uname}!
<h2>JSTL (jsp标准标签库)</h2>
<h3>通用标签 set out remove</h3>
<c:set var="sb" value="666" scope="session"></c:set>
<c:out value="${sb}"></c:out><br>
移除前:${sb }<br>
<c:remove var="sb" scope="request"/>
移除后:${sb }
<h3>条件 if </h3>
<c:if test="${sb==66 }">
就是这么溜
</c:if>
<h3>迭代 forEach</h3>
打印1-10中的奇数<br>
<c:forEach begin="1"end="10"var="i"step="2">
${i }
</c:forEach>
<!-- 遍历对象集合 -->
<c:forEach items="${myls }"var="g">
${g.gname }
</c:forEach>
<!-- 逗号分割 -->
<c:forTokens items="aa,bb,cc" delims=","var="i">
${i }
</c:forTokens>
</body>
</html>