package com.Student; /** * 类的封装 在jsp中称为 javaBean * @author MZFAITHDREAM * */ public class Student { private String id; private String name; private int age; private String sex; private String phone; private String addr; //无惨构造 public Student() { // TODO Auto-generated constructor stub } //参数构造 public Student(String id, String name, int age, String sex, String phone, String addr) { super(); this.id = id; this.name = name; this.age = age; this.sex = sex; this.phone = phone; this.addr = addr; } //get set 方法 public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } public String getAddr() { return addr; } public void setAddr(String addr) { this.addr = addr; } //toStrinng方法 @Override public String toString() { return "Student [id=" + id + ", name=" + name + ", age=" + age + ", sex=" + sex + ", phone=" + phone + ", addr=" + addr + "]"; } }
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <style type="text/css"> *{ font-size: 30px; font-weight: bold; } h1{ color: red; } </style> <title>Insert title here</title> </head> <body> <h1>--------------------对象二 request的获取</h1> <% //方法一 request对象自己直接获得数据 String b=(String)request.getAttribute("b"); //方法二 pageContext帮request获得数据 //String b=(String)pageContext.getAttribute("b", PageContext.REQUEST_SCOPE); out.write("我获得了AA.jsp页面的request对象的数据:"+b); %> <h1>--------------------对象一不能获取成功</h1> <% /* String info=(String)pageContext.getAttribute("aaa"); out.write(info); */ %> <hr> <h1>--------------------对象三的获取</h1> <% //方法一 使用session直接获得数据 String c=(String)session.getAttribute("ccc"); //方法二 pageContext帮session获得数据 String cc=(String)pageContext.getAttribute("cc", PageContext.SESSION_SCOPE); out.write("我获得了AA.jsp页面的session对象的数据:"+c); out.write("我获得了AA.jsp页面的session对象的数据:"+cc); %> <hr> <h1>--------------------对象四的获取</h1> <% //方法一 //String ddd=(String)application.getAttribute("ddd"); String ddd=(String)pageContext.getAttribute("ddd",PageContext.APPLICATION_SCOPE); out.write("我获得了AA.jsp页面的application对象的数据:"+ddd); String e=(String)application.getAttribute("e"); pageContext.getOut().write(e+"这是对象e"); %> <hr> <h1> </body> </html>
<%@page import="com.Student.Student"%> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> <style type="text/css"> *{ font-size: 18px; } </style> </head> <%-- pageContrxt request session application --%> <!-- 比如 学生信息 : 学号 手机号 年龄 家庭地址 性别 --> <h1>==============方案一存储数据============</h1> <% session.setAttribute("name", "小YI"); session.setAttribute("id", "007"); session.setAttribute("phone", "123456789123"); session.setAttribute("age", "21"); session.setAttribute("addr", "江西南昌"); session.setAttribute("sex", "男"); %> <h1>==============方案二存储数据============</h1> <!-- //因为存储的数据会随着项目的功能而不断增加,因此再数据量非常多的时候,不适合采用每一个数据单独存储 //使用类的封装----JavaBean --> <% Student student=new Student("007","小明二号",20,"男","18188888888","江西省南昌市"); session.setAttribute("student", student); %> <h1>方案三:利用一个JSP动作将数据存储起来</h1> <jsp:useBean id="a" class="com.Student.Student" scope="session"></jsp:useBean> <%-- 使用的id id表示使用的类的对象 class表示完整包的路径--%> <!--等价于:Student student =new Student() session.setAttribute("student", student); stu 对象明 scope 四大域 scope="session" --> <% a.setId("1003"); a.setName("小明三号"); a.setAge(23); a.setSex("男"); a.setAddr("南昌"); a.setPhone("1244444444444"); %> <h1>方案四:利用二个JSP动作将数据存储起来</h1> <hr> <!--完全使用jsp动作将数据放入四大域 存储起来 --> <jsp:useBean id="stu1" class="com.Student.Student" scope="session"></jsp:useBean> <!-- 赋值 --> <%-- property: 属性名称 name:对象明 value:数据 --%> <jsp:setProperty property="id" name="stu1" value="1004"/> <jsp:setProperty property="name" name="stu1" value="小红四号"/> <jsp:setProperty property="age" name="stu1" value="23"/> <jsp:setProperty property="sex" name="stu1" value="男"/> <jsp:setProperty property="phone" name="stu1" value="123456678"/> <jsp:setProperty property="addr" name="stu1" value="江西南昌"/> </body> </html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>数据类</title> </head> <body> <h1>方案四:利用二个JSP动作将数据存储起来</h1> <!--完全使用jsp动作将数据放入四大域 存储起来 --> <jsp:useBean id="people" class="com.People.People" scope="application"></jsp:useBean> <jsp:setProperty property="id" name="people" value="1001"/> <jsp:setProperty property="name" name="people" value="小红一号"/> <jsp:setProperty property="age" name="people" value="23"/> <jsp:setProperty property="sex" name="people" value="男"/> <jsp:setProperty property="phone" name="people" value="123456678"/> <jsp:setProperty property="addr" name="people" value="江西南昌"/> <jsp:setProperty property="height" name="people" value="189cm"/> <jsp:setProperty property="weight" name="people" value="64kg"/> <jsp:setProperty property="phone" name="people" value="123456678"/> <jsp:setProperty property="a" name="people" value="a属性是人皮肤的色彩pink"/> <jsp:setProperty property="b" name="people" value="b属性是人穿衣服的color"/> <jsp:setProperty property="c" name="people" value="b属性是人类未知的属性"/> <hr> <h2>创建第二个人类</h2> <jsp:useBean id="people2" class="com.People.People" scope="application"></jsp:useBean> <jsp:setProperty property="id" name="people2" value="1002"/> <jsp:setProperty property="name" name="people2" value="小明二号"/> <jsp:setProperty property="age" name="people2" value="23"/> <jsp:setProperty property="sex" name="people2" value="男"/> <jsp:setProperty property="phone" name="people2" value="123456678"/> <jsp:setProperty property="addr" name="people2" value="江西南昌"/> <jsp:setProperty property="height" name="people2" value="189cm"/> <jsp:setProperty property="weight" name="people2" value="64kg"/> <jsp:setProperty property="phone" name="people2" value="123456678"/> <jsp:setProperty property="a" name="people2" value="a属性是人皮肤的色彩green"/> <jsp:setProperty property="b" name="people2" value="b属性是人穿衣服的color"/> <jsp:setProperty property="c" name="people2" value="b属性是人类未知的属性"/> <h2>创建第三个人类</h2> <jsp:useBean id="people3" class="com.People.People" scope="application"></jsp:useBean> <jsp:setProperty property="id" name="people3" value="1003"/> <jsp:setProperty property="name" name="people3" value="小户三号"/> <jsp:setProperty property="age" name="people3" value="23"/> <jsp:setProperty property="sex" name="people3" value="男"/> <jsp:setProperty property="phone" name="people3" value="123456678"/> <jsp:setProperty property="addr" name="people3" value="江西南昌"/> <jsp:setProperty property="height" name="people3" value="189cm"/> <jsp:setProperty property="weight" name="people3" value="64kg"/> <jsp:setProperty property="phone" name="people3" value="123456678"/> <jsp:setProperty property="a" name="people3" value="a属性是人皮肤的色彩red"/> <jsp:setProperty property="b" name="people3" value="b属性是人穿衣服的color"/> <jsp:setProperty property="c" name="people3" value="b属性是人类未知的属性"/> </body> </html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> <style type=""> *{ color: black; font-size: 30px; font-weight: bold; } </style> </head> <body> <h1> 取出数据内容</h1> <jsp:useBean id="people" class="com.People.People" scope="application"></jsp:useBean> <P>出生编号:<jsp:getProperty property="id" name="people"/></P> <P>姓名:<jsp:getProperty property="name" name="people"/></P> <P>年龄:<jsp:getProperty property="age" name="people"/></P> <P>性别:<jsp:getProperty property="sex" name="people"/></P> <P>手机号:<jsp:getProperty property="phone" name="people"/></P> <P>家庭地址:<jsp:getProperty property="addr" name="people"/></P> <P>身高:<jsp:getProperty property="height" name="people"/></P> <P>体重:<jsp:getProperty property="weight" name="people"/></P> <P>属性a:<jsp:getProperty property="a" name="people"/></P> <P>属性b:<jsp:getProperty property="b" name="people"/></P> <P>属性c:<jsp:getProperty property="c" name="people"/></P> <hr> <jsp:useBean id="people2" class="com.People.People" scope="application"></jsp:useBean> <P>出生编号:<jsp:getProperty property="id" name="people2"/></P> <P>姓名:<jsp:getProperty property="name" name="people2"/></P> <P>年龄:<jsp:getProperty property="age" name="people2"/></P> <P>性别:<jsp:getProperty property="sex" name="people2"/></P> <P>手机号:<jsp:getProperty property="phone" name="people2"/></P> <P>家庭地址:<jsp:getProperty property="addr" name="people2"/></P> <P>身高:<jsp:getProperty property="height" name="people2"/></P> <P>体重:<jsp:getProperty property="weight" name="people2"/></P> <P>属性a:<jsp:getProperty property="a" name="people2"/></P> <P>属性b:<jsp:getProperty property="b" name="people2"/></P> <P>属性c:<jsp:getProperty property="c" name="people2"/></P> <hr> <jsp:useBean id="people3" class="com.People.People" scope="application"></jsp:useBean> <P>出生编号:<jsp:getProperty property="id" name="people3"/></P> <P>姓名:<jsp:getProperty property="name" name="people3"/></P> <P>年龄:<jsp:getProperty property="age" name="people3"/></P> <P>性别:<jsp:getProperty property="sex" name="people3"/></P> <P>手机号:<jsp:getProperty property="phone" name="people3"/></P> <P>家庭地址:<jsp:getProperty property="addr" name="people3"/></P> <P>身高:<jsp:getProperty property="height" name="people3"/></P> <P>体重:<jsp:getProperty property="weight" name="people3"/></P> <P>属性a:<jsp:getProperty property="a" name="people3"/></P> <P>属性b:<jsp:getProperty property="b" name="people3"/></P> <P>属性c:<jsp:getProperty property="c" name="people3"/></P> </body> </html>
<%@page import="com.Student.Student"%> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <style type="text/css"> *{ font-size: 20px; font-weight: bold; } </style> <title>Insert title here</title> </head> <body> <h1>==============方案一拿数据============</h1> <p><h1>学生个人信息</h1></p> <P>学号:<% String id= (String)session.getAttribute("id"); out.write(id);%></P> <P>姓名:<% out.write((String)session.getAttribute("name"));%></P> <P>年龄:<%=((String)session.getAttribute("age"))%></P> <P>性别:<%=((String)session.getAttribute("sex"))%></P> <P>手机号:<%=((String)session.getAttribute("phone"))%></P> <P>家庭地址:<%=((String)session.getAttribute("addr"))%></P> <%-- 数据量不断增加 在数据量非常多的时候 不适合采用没一个数据单独放置 --%> <!-- 类的封装 JAVA Bean --> <h1>==============方案二拿数据============</h1> <!-- 从session中找到存储大量数据的对象 --> <% Student student=(Student)session.getAttribute("student"); %> <p>学号:<%=student.getId() %></p> <p>姓名:<%=student.getName() %></p> <p>年龄:<%=student.getAge() %></p> <p>性别:<%=student.getSex() %></p> <p>手机号:<%=student.getPhone()%></p> <p>家庭地址:<%=student.getAddr() %></p> <h1>==============方案三拿数据============</h1> <h1>方案三:利用一个JSP动作将数据存储起来</h1> <jsp:useBean id="a" class="com.Student.Student" scope="session"></jsp:useBean> <P>学号:<%a.getName();%></P> <P>姓名:<%a.getName();%></P> <P>年龄:<%=a.getAge()%></P> <P>性别:<%=a.getSex()%></P> <P>手机号:<%=a.getPhone()%></P> <P>家庭地址:<%=a.getAddr()%></P> <h1>================方案四拿数据=================</h1> <jsp:useBean id="stu1" class="com.Student.Student" scope="session"></jsp:useBean> <P>学号:<jsp:getProperty property="id" name="stu1"/></P> <P>姓名:<jsp:getProperty property="name" name="stu1"/></P> <P>年龄:<jsp:getProperty property="age" name="stu1"/></P> <P>性别:<jsp:getProperty property="sex" name="stu1"/></P> <P>手机号:<jsp:getProperty property="phone" name="stu1"/></P> <P>家庭地址:<jsp:getProperty property="addr" name="stu1"/></P> </body> </html>
代码运行结果图