pageContext 域
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" %>
<!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 > 转发</title >
</head >
<body >
<%
/* 该域对象,只在本页面有效,出了页面就获取不到 */
pageContext.setAttribute("page" , "pageContext" );
%>
<%=pageContext.getAttribute("page" ) %>
<%-- 携带请求的参数(不要在这中间加注释!编译不出来!) --%>
<jsp:forward page ="/2.jsp" >
<jsp:param value ="test" name ="username" />
<jsp:param value ="123" name ="password" />
</jsp:forward >
</body >
</html >
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" %>
<!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 > </title >
</head >
<body >
<%=pageContext.getAttribute("page" ) %>
<%-- <%=request .getAttribute("username" ) %> --%>
<%=request .getParameter("username" ) %>
<%=request .getParameter("password" ) %>
</body >
</html >
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" %>
<!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 >
<%
/* 一般系统方法中,需要一个 int 参数,一般会是常量 */
/* 只要有 pageContext 就可以向各个域中添加值,参数三表示对应域的常量 */
// pageContext.setAttribute("page" , "pageContext" , PageContext.PAGE_SCOPE);
// pageContext.setAttribute("page" , "request" , PageContext.REQUEST_SCOPE);
// pageContext.setAttribute("page" , "session" , PageContext.SESSION_SCOPE);
pageContext.setAttribute("page" , "application" , PageContext.APPLICATION_SCOPE);
%>
<%-- =pageContext.findAttribute("page" ) --%>
${page }
</body >
</html >
<%@page import="com.lanou3g.Address" %>
<%@page import="com.lanou3g.User" %>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" %>
<!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 >
<%
User user = new User();
user.setUsername("test" );
user.setPassword("123" );
request .setAttribute("u" , user);
// 添加地址对象
Address address = new Address();
address.setCity("zh" );
user.setAddress(address);
%>
${u.username }
${u.address.city }
${u["username"] }
${u['username'] }
</body >
</html >
<%@page import="java.util.HashMap" %>
<%@page import="java.util.ArrayList" %>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" %>
<!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 >
<%
ArrayList<Integer> arrayList = new ArrayList<>();
HashMap<String ,ArrayList<Integer>> hashMap = new HashMap<>();
arrayList.add(1 );
arrayList.add(2 );
arrayList.add(3 );
request .setAttribute("list" , arrayList);
hashMap.put("a" , arrayList);
hashMap.put("b" , arrayList);
hashMap.put("c" , arrayList);
request .setAttribute("map" , hashMap);
%>
${list }
${list[0] }
${map }
${map["a"] }
</body >
</html >
<%@page import="java.util.ArrayList" %>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" %>
<!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 >
<%
String str1 = null ;
request .setAttribute("str1" , str1);
String str2 = "" ;
request .setAttribute("str2" , str2);
ArrayList<String > list1 = new ArrayList<>();
list1.add("aaa" );
ArrayList<String > list2 = new ArrayList<>();
ArrayList<String > list3 = null ;
ArrayList<String > list4 = new ArrayList<>();
list1.add("" );
request .setAttribute("list1" , list1);
request .setAttribute("list2" , list2);
request .setAttribute("list3" , list3);
request .setAttribute("list4" , list4);
/* 测试三木运算符 */
String sex = "男" ;
request .setAttribute("sex" , sex);
%>
${empty str1 }
${empty str2 }
${empty list1 }
${empty list2 }
${empty list3 }
${empty list4 }
${empty list3 ? "前面" : "后面" }
<br > <input type ="radio" value ="男" name ="sex" ${sex =="男" ? "checked ='checked' " : " " }>男
<br><input type=" radio" value ="女" name ="sex" ${sex =="女" ? "checked ='checked' " : " " }>女
</body>
</html>
<%@ page language=" java" contentType ="text/html; charset=UTF-8"
pageEncoding ="UTF-8" % >
<!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 >
<%
pageContext.setAttribute("page" , "pageContext" , PageContext.PAGE_SCOPE);
pageContext.setAttribute("page" , "request" , PageContext.REQUEST_SCOPE);
pageContext.setAttribute("page" , "session" , PageContext.SESSION_SCOPE);
pageContext.setAttribute("page" , "application" , PageContext.APPLICATION_SCOPE);
%>
${pageScope.page }
${requestScope.page }
${sessionScope.page }
${applicationScope.page }
</body >
</html >
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" %>
<!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 >
${www }
<form action ="${pageContext.request.contextPath }/9.jsp" method ="post" >
账号:<input type ="text" name ="username" >
<br > 密码:<input type ="password" name ="password" >
<br > <input type ="checkbox" name ="hobby" value ="唱歌" > 唱歌
<input type ="checkbox" name ="hobby" value ="跳舞" > 跳舞
<br > <input type ="submit" value ="提交" >
</form >
</body >
</html >
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" %>
<!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 >
<%
response .setContentType("text/html;charset=UTF-8" );
request .setCharacterEncoding("UTF-8" );
%>
${param.username }
${paramValues.hobby[0] }<br >
${header["User-Agent"] }<br >
${cookie.JSESSIONID }<br >
${cookie.JSESSIONID.value }<br >
</body >
</html >
<%@ 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 >
<c:set var ="num" value ="10" scope ="session" > </c:set >
<c:out value ="${num }" > </c:out >
<c:out value ="${num }" default ="aaa" > </c:out >
<c:remove var ="num" scope ="session" />
${num }
</body >
</html >
<%@ 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 >
<c:set var ="n" value ="10" > </c:set >
<c:if test ="${n > 4 }" >
aaa
</c:if >
<c:if test ="${n < 4 }" >
bbb
</c:if >
<c:choose >
<c:when test ="${n == 5 }" > 5</c:when >
<c:when test ="${n < 5 }" > 10</c:when >
<c:otherwise >
不知道
</c:otherwise >
</c:choose >
</body >
</html >
<%@page import="java.util.ArrayList" %>
<%@ 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 >
<%
ArrayList<String > list = new ArrayList<>();
list.add("aaa" );
list.add("bbb" );
list.add("ccc" );
list.add("ddd" );
list.add("eee" );
request .setAttribute("list" , list);
%>
<c:forEach var ="i" begin ="0" end ="10" step ="3" >
${i }<br >
</c:forEach >
<c:forEach items ="${list }" var ="j" step ="2" >
${j }<br >
</c:forEach >
<c:forEach items ="${list }" var ="l" varStatus ="vs" >
<tr >
<td > ${l }</td >
<td > ${vs.index }</td >
<td > ${l }</td >
<td > ${l }</td >
<td > ${l }</td >
</tr >
</c:forEach >
</body >
</html >
<%@page import="java.util.ArrayList" %>
<%@ 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 >
<style type ="text/css" >
body {
background : purple ;
text-align : center ;
color : white ;
}
table {
margin : 0 auto ;
}
</style >
</head >
<body >
<%
ArrayList<String > list = new ArrayList<>();
list.add("aaa" );
list.add("bbb" );
list.add("ccc" );
list.add("ddd" );
list.add("eee" );
request .setAttribute("list" , list);
%>
<table border ="1" >
<tr >
<td width ="100" > 值</td >
<td width ="100" > 索引</td >
<td width ="100" > 计数</td >
<td width ="100" > 第一个</td >
<td width ="100" > 最后一个</td >
</tr >
<c:forEach items ="${list }" var ="l" varStatus ="vs" >
<tr >
<td > ${l }</td >
<td > ${vs.index }</td >
<td > ${vs.count }</td >
<td > ${vs.first }</td >
<td > ${vs.last }</td >
</tr >
</c:forEach >
</table >
</body >
</html >
public void fun (PageContext pageContext) {
pageContext.getRequest();
pageContext.getSession();
pageContext.getServletContext();
pageContext.getServletConfig();
pageContext.findAttribute("key" );
}
/*
* 设计模式(编写代码)
* 1 .Model1 模式
* JSP + JavaBean <jsp: userBean/> 模式
* 2 .Model2 模式(模拟 MVC 模式)
* JSP (页面和展示数据) + Servlet (逻辑处理) + JavaBean (实体类) 模式
*
* M :Model (数据模型)
* V :View (视图界面)
* C :Controller (控制器)
*/