JSP:JAVA Bean在JSP中的运用
index.
[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>
上一篇: 定时任务crond服务
下一篇: LeetCode 203删除链表元素