編寫一個封裝學生信息的JavaBean對象,在頁面中調用該對象,並將學生信息輸出在頁面中。
1 package com.sp.test; 2 3 public class Student { 4 private String name; 5 private int xuehao; 6 private String sex; 7 private String banji; 8 public String getName() { 9 return name; 10 } 11 public void setName(String name) { 12 this.name = name; 13 } 14 public int getXuehao() { 15 return xuehao; 16 } 17 public void setXuehao(int xuehao) { 18 this.xuehao = xuehao; 19 } 20 public String getSex() { 21 return sex; 22 } 23 public void setSex(String sex) { 24 this.sex = sex; 25 } 26 public String getBanji() { 27 return banji; 28 } 29 public void setBanji(String banji) { 30 this.banji = banji; 31 } 32 public Student(String name, int xuehao, String sex, String banji) { 33 super(); 34 this.name = name; 35 this.xuehao = xuehao; 36 this.sex = sex; 37 this.banji = banji; 38 } 39 @Override 40 public String toString() { 41 return "Student [姓名=" + name + ", 學號=" + xuehao + ", 性別=" + sex + ", 班級=" + banji + "]"; 42 } 43 44 45 46 }
1 <%@page import="com.sp.test.Student"%> 2 <%@ page language="java" contentType="text/html; charset=UTF-8" 3 pageEncoding="UTF-8"%> 4 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 5 <html> 6 <head> 7 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 8 <title>Insert title here</title> 9 </head> 10 <body> 11 <% 12 Student st = new Student("張三",2016001,"男","網絡工程"); 13 out.println(st.toString()); 14 15 %> 16 </body> 17 </html>