1.Student.java
package com.bean; public class Student { private String sno; private String name; private String age; public String getSno() { return sno; } public void setSno(String sno) { this.sno = sno; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAge() { return age; } public void setAge(String age) { this.age = age; } }
2.myBean.jsp
1 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> 2 <% 3 String path = request.getContextPath(); 4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 5 %> 6 7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 8 <html> 9 <head> 10 <base href="<%=basePath%>"> 11 12 <title>My JSP 'myBean.jsp' starting page</title> 13 14 <meta http-equiv="pragma" content="no-cache"> 15 <meta http-equiv="cache-control" content="no-cache"> 16 <meta http-equiv="expires" content="0"> 17 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> 18 <meta http-equiv="description" content="This is my page"> 19 <!-- 20 <link rel="stylesheet" type="text/css" href="styles.css"> 21 --> 22 23 </head> 24 25 <body> 26 <jsp:useBean id="stu" class="com.bean.Student"></jsp:useBean> 27 <% 28 session.setAttribute("stu", stu); 29 %> 30 <jsp:setProperty property="name" name="stu" value="zhangsan"/> 31 <jsp:setProperty property="age" name="stu" value="21"/> 32 33 姓名:<%=stu.getName() %> 34 年龄: <%=stu.getAge() %> 35 </body> 36 </html>