你的位置:首页 > Java教程

[Java教程]实战1


(1)编写index.jsp页面,用来收集用户的注册信息

<%@ page language="java" pageEncoding="GBK"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head>  <base href="<%=basePath%>">    <title>应用EL表达式访问JavaBean的属性</title>  <link rel="stylesheet" type="text/css" href="CSS/style.css"> </head>  <body><form name="form1" method="post" action="deal.jsp"> <table width="403" height="230" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#666666">  <tr>   <td height="30" colspan="2" bgcolor="#EFEFEF">·用户注册</td>  </tr>  <tr>   <td width="88" align="center" bgcolor="#FFFFFF">用 户 名:</td>   <td width="359" bgcolor="#FFFFFF"><input name="username" type="text" id="username"></td>  </tr>  <tr>   <td align="center" bgcolor="#FFFFFF">密&nbsp;&nbsp;&nbsp;&nbsp;码:</td>   <td bgcolor="#FFFFFF"><input name="pwd" type="password" id="pwd"></td>  </tr>  <tr>   <td align="center" bgcolor="#FFFFFF">确认密码:</td>   <td bgcolor="#FFFFFF"><input name="repwd" type="password" id="repwd"></td>  </tr>  <tr>   <td align="center" bgcolor="#FFFFFF">性&nbsp;&nbsp;&nbsp;&nbsp;别:</td>   <td bgcolor="#FFFFFF"><input name="sex" type="radio" class="noborder" value="男">    男     <input name="sex" type="radio" class="noborder" value="女">    女</td>  </tr>  <tr>   <td align="center" bgcolor="#FFFFFF">爱&nbsp;&nbsp;&nbsp;&nbsp;好:</td>   <td bgcolor="#FFFFFF"><input name="affect" type="checkbox" class="noborder" id="affect" value="体育">体育 <input name="affect" type="checkbox" class="noborder" id="affect" value="美术">美术 <input name="affect" type="checkbox" class="noborder" id="affect" value="音乐">音乐 <input name="affect" type="checkbox" class="noborder" id="affect" value="旅游">旅游 </td>  </tr>  <tr>   <td colspan="2" align="center" bgcolor="#FFFFFF">    <input name="Submit" type="submit" class="btn_grey" value="提交">&nbsp;<input name="Submit2" type="reset" class="btn_grey" value="重置"></td>  </tr> </table> </form> </body></html>

(2)编写JavaBean

package com.wuyudong;public class UserForm {  private String username = "";  private String pwd = "";  private String sex = "";  private String[] affect = null;  public String getUsername() {    return username;  }  public void setUsername(String username) {    this.username = username;  }  public String getPwd() {    return pwd;  }  public void setPwd(String pwd) {    this.pwd = pwd;  }  public String getSex() {    return sex;  }  public void setSex(String sex) {    this.sex = sex;  }  public String[] getAffect() {    return affect;  }  public void setAffect(String[] affect) {    this.affect = affect;  }}

(3)编写deal.jsp页面

<%@ page language="java" pageEncoding="GBK"%><%request.setCharacterEncoding("GBK");%><jsp:useBean id="userForm" class="com.wuyudong.UserForm" scope="page"/><jsp:setProperty name="userForm" property="*"/><!-- jsp:setProperty name="userForm" property="affect" value='<%=request.getParameterValues("affect")%>'/> --><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head>  <title>应用EL表达式访问JavaBean的属性</title>  <link rel="stylesheet" type="text/css" href="CSS/style.css"> </head>  <body>  <table width="403" height="218" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#666666">  <tr>   <td height="30" colspan="2" bgcolor="#EFEFEF">·显示用户填写的注册信息</td>  </tr>  <tr>   <td width="88" align="center" bgcolor="#FFFFFF">用 户 名:</td>   <td width="359" bgcolor="#FFFFFF">&nbsp;${userForm.username}</td>  </tr>  <tr>   <td align="center" bgcolor="#FFFFFF">密&nbsp;&nbsp;&nbsp;&nbsp;码:</td>   <td bgcolor="#FFFFFF">&nbsp;${userForm.pwd}</td>  </tr>  <tr>   <td align="center" bgcolor="#FFFFFF">性&nbsp;&nbsp;&nbsp;&nbsp;别:</td>   <td bgcolor="#FFFFFF">&nbsp;${userForm.sex}</td>  </tr>  <tr>   <td align="center" bgcolor="#FFFFFF">爱&nbsp;&nbsp;&nbsp;&nbsp;好:</td>   <td bgcolor="#FFFFFF">&nbsp;${userForm.affect[0]} ${userForm.affect[1]} ${userForm.affect[2]} ${userForm.affect[3]}</td>  </tr>  <tr>   <td colspan="2" align="center" bgcolor="#FFFFFF">    <input name="Button" type="button" class="btn_grey" value="返回" onClick="window.location.href='index.jsp'">&nbsp;</td>  </tr> </table> </body></html>

运行如下图所示:

提交后显示: