User类
package cn.com.user;
public class User {
int id;
String name;
String sex;
public User(int id, String name, String sex) {
super();
this.id = id;
this.name = name;
this.sex = sex;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
@Override
public String toString() {
return "User [id=" + id + ", name=" + name + ", sex=" + sex + "]";
}
public User() {
super();
// TODO Auto-generated constructor stub
}
}
index.jsp
<%@page import="cn.com.user.User"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<%
pageContext.setAttribute("value", "pagecontext");
request.setAttribute("value", "request");
session.setAttribute("value", "session");
application.setAttribute("value", "application");
%>
<h2>本页面获取值</h2>
<%
out.print("pagecontext:"+pageContext.getAttribute("value")+"<br>");
out.print("request:"+request.getAttribute("value")+"<br>");
out.print("session:"+session.getAttribute("value")+"<br>");
out.print("application:"+application.getAttribute("value")+"<br>");
%>
<%
User u1=new User(1,"小明","男");
User u2=new User(2,"小红","男");
User u3=new User(3,"消防","女");
List<User> u=new ArrayList<User>();
u.add(u1);
u.add(u2);
u.add(u3);
session.setAttribute("user", u);
%>
<br>
${user[1].name}<br>
<c:out value="${user}" default="出错"/><br>
<c:set var="abc" value="${user}" scope="session" /><br>
<%=session.getAttribute("abc")%>
<a href="index1.jsp"> 跳转页面</a>
</body>
</html>
index1.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index1.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<%
out.print("pagecontext:"+pageContext.getAttribute("value")+"<br>");
out.print("request:"+request.getAttribute("value")+"<br>");
out.print("session:"+session.getAttribute("value")+"<br>");
out.print("application:"+application.getAttribute("value")+"<br>");
%>
</body>
</html>