首先新建一個(gè)典型web應(yīng)用程序
然后建一個(gè)com.storm包,在包下寫一個(gè)UserBean的類,添加private的屬性之后只要點(diǎn)擊菜單重構(gòu)-封裝字段就可以添加set和get方法

package ?com.storm;
public ? class ?UserBean? {
????
private ?String?name;
????
private ? int ?sex;
????
private ?String?education;
????
private ?String?email;
????
public ?String?getName()? {
????????
return ?name;
????}


????
public ? void ?setName(String?name)? {
????????
this .name? = ?name;
????}


????
public ? int ?getSex()? {
????????
return ?sex;
????}


????
public ? void ?setSex( int ?sex)? {
????????
this .sex? = ?sex;
????}


????
public ?String?getEducation()? {
????????
return ?education;
????}


????
public ? void ?setEducation(String?education)? {
????????
this .education? = ?education;
????}


????
public ?String?getEmail()? {
????????
return ?email;
????}


????
public ? void ?setEmail(String?email)? {
????????
this .email? = ?email;
????}

}

然后再寫一個(gè)注冊(cè)頁面index.jsp
<% @page?contentType = " text/html;?charset=GB2312 " ? %>
< html >
????
< head >
????????
< meta?http - equiv = " Content-Type " ?content = " text/html;?charset=UTF-8 " >
????????
< title > JSP?Page </ title >
????
</ head >
????
< body >
????????
< form?name = " form " ?action = " reg.jsp " ?method = " POST " >
????????????
< table?border = " 1 " >
????????????????
< thead >
????????????????????
< tr >
????????????????????????
< td > 用戶名 </ td >
????????????????????????
< td >< input?type = " text " ?name = " name " ?value = "" ?width = " 6 " ? /></ td >
????????????????????
</ tr >
????????????????
</ thead >
????????????????
< tbody >
????????????????????
< tr >
????????????????????????
< td > 性別 </ td >
????????????????????????
< td > ?
????????????????????????????
< input?type = " radio " ?name = " sex " ?value = " 0 " ? />

????????????????????????????
< input?type = " radio " ?name = " sex " ?value = " 1 " ? />

????????????????????????
</ td >

????????????????????
</ tr >
????????????????????
< tr >
????????????????????????
< td > 學(xué)歷 </ td >
????????????????????????
< td >< select?name = " education " >
????????????????????????????
< option?value = " 高中 " ?selected > 高中 </ option >
????????????????????????????
< option?value = " 大學(xué) " > 大學(xué) </ option >
????????????????????????????
< option?value = " 碩士 " > 碩士 </ option >
????????????????????????????
< option?value = " 博士 " > 博士 </ option >
????????????????????????
</ select ></ td >
????????????????????
</ tr >
????????????????????
< tr >
????????????????????????
< td > EMAIL </ td >
????????????????????????
< td >< input?type = " text " ?name = " mail " ?value = "" ?width = " 10 " ? /></ td >
????????????????????
</ tr >
????????????????????
< tr >
????????????????????????
< td ></ td >
????????????????????????
< td ></ td >
????????????????????
</ tr >
????????????????????
< tr >
????????????????????????
< td >< input?type = " submit " ?value = " 提交 " ?name = " submit " ? /></ td >
????????????????????????
< td >< input?type = " reset " ?value = " 重置 " ?name = " reset " ? /></ td >
????????????????????
</ tr >
????????????????
</ tbody >
????????????
</ table > ?????
????????
</ form >

????
</ body >
</ html >

form會(huì)交給reg.jsp處理,reg.jsp負(fù)責(zé)實(shí)例化一個(gè)javabean,并儲(chǔ)存數(shù)據(jù)


<% @page?contentType = " text/html;?charset=GB2312 " ? %>
< html >
????
< head >
????????
< meta?http - equiv = " Content-Type " ?content = " text/html;?charset=UTF-8 " >
????????
< title > JSP?Page </ title >
????
</ head >
????
< body >
????????
<%
????????????request.setCharacterEncoding(
" GB2312 " );
????????
%>
????????
< jsp:useBean?id = " user " ?scope = " session " ? class = " com.storm.UserBean " >
????????????
< jsp:setProperty?name = " user " ?property = " * " ? />
????????????
< jsp:setProperty?name = " user " ?property = " email " ?param = " mail " ? />
????????
</ jsp:useBean >
????
</ body >
</ html >

鏈接到get.jsp用于取出javabean的數(shù)據(jù)并顯示

<%@page?contentType="text/html;?charset=GB2312"?%>
<html>
????
<head>
????????
<meta?http-equiv="Content-Type"?content="text/html;?charset=UTF-8">
????????
<title>JSP?Page</title>
????
</head>
????
<body>
<jsp:useBean?id="user"?scope="session"?class="com.storm.UserBean"?/>
????????
<jsp:getProperty?name="user"?property="name"?/><br>
????????你的性別:
<%?int?sex=user.getSex();
????????????
if(0==sex)
????????????????out.println(
"");
????????????
else?if(1==sex)
????????????????out.println(
"");
????????
%>
????????
<br>
????????你的學(xué)歷:
<jsp:getProperty?name="user"?property="education"?/><br>
????????你的email:
<jsp:getProperty?name="user"?property="email"?/><br>
????
</body>
</html>


需要注意的是同一個(gè)javabean實(shí)例在打開瀏覽器之后創(chuàng)建并儲(chǔ)存數(shù)據(jù)直到關(guān)閉也無法通過后退再到Index.jsp-reg.jsp改變其中的數(shù)據(jù)
而且要支持中文必須在每個(gè)文件頭加上 <%@page?contentType="text/html;?charset=GB2312"?%>