目錄結構,如圖: index.jsp [java] <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title>JSP測試</title> <script language="javascript"> function check() { if(form1.name.value=="") { alert("姓名不能為空!"); return false; } else if(form1.age.value=="") { alert("年齡不能為空!"); return false; } return true; } </script> </head> <body> <form action="Bean.jsp" method="post" id="form1" onsubmit="return check();"> 姓名<input type="text" name="name" /><br/> 性別<select name="sex" > <option value="男">男</option> <option value="女">女</option> </select> <br/> 年齡<input type="text" name="age" /><br/><br/> <input type="submit" value=" 提 交 " /> <jsp:include page="Foot.jsp" /> </form> </body> </html> Foot.jsp [java] <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <p align="left" style="color:red"> 版權所有 盜版必究 </p> Person.java [java] package com.xieyuan; public class Person { private String name; private String sex; private String age; 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; } public String getAge() { return age; } public void setAge(String age) { this.age = age; } } Bean.jsp [java] <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>My JSP 'Bean.jsp' starting page</title> </head> <body> <!-- 聲明JAVA BEAN --> <jsp:useBean id="bean" class="com.xieyuan.Person" scope="page"/> <% request.setCharacterEncoding("UTF-8"); %> <!-- 設置Persion的所有屬性 --> <jsp:setProperty name="bean" property="*" /> 您的信息如下:<BR/> <jsp:getProperty name="bean" property="name"/><br/> <jsp:getProperty name="bean" property="sex"/><br/> <jsp:getProperty name="bean" property="age"/><br/> </body> </html> web.xml [java] <?xml version="1.0" encoding="UTF-8"?> <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>