目录结构,如图:
index.jsp
<%@ 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
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <p align="left" style="color:red"> 版权所有 盗版必究 </p>Person.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
<%@ 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
<?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>
index.jsp效果如下:
提交后到Bean.jsp