public class Email implements Serializable { private static final long serialVersionUID=1L; private String mailAdd; private boolean email;//是否是个标准的Email地址 public Email(){ } public Email(String mailAdd) { this.mailAdd = mailAdd; } public String getMailAdd() { return mailAdd; } public void setMailAdd(String mailAdd) { this.mailAdd = mailAdd; } public boolean isEmail() { String regex="\\w+([-+.']\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*";//正 则表达式,定义邮箱格式 if(mailAdd.matches(regex)){//判断字符串是否和正则表达式匹配 email=true;//email为真 } return email; } }
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Titletitle> head> <body> <form name="form1" action="result.jsp" method="post"> <table width="450" height="250" align="center" border="1"> <tr> <td colspan="2" align="center"> <b>邮箱认证系统b> td> tr> <tr> <td align="right">邮箱地址td> <td> <input type="text" name="mailAdd"> td> tr> <tr> <td align="center" colspan="2"> <input type="submit" value="提 交"> td> tr> table> form> body> html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Titletitle> head> <body> <jsp:useBean id="email" class="lyt.email.Email">jsp:useBean> <jsp:setProperty name="email" property="*">jsp:setProperty> <jsp:getProperty name="email" property="mailAdd">jsp:getProperty> <%=email.isEmail()%> body> html>虽然在JavaBean的规范中,要求JavaBean对象提供默认无参构造方法,但除了默认无参构造方法外,JavaBean也可以根据相关属性提供构造方法。
public class Produce { private String name="机械键盘"; private double price=1880.5; public String getName() { return name; } public void setName(String name) { this.name = name; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } }
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Titletitle> head> <body> <jsp:useBean id="produce" class="lyt.produce.Produce">jsp:useBean> <div> <ul> <li>商品名称:<jsp:getProperty name="produce" property="name"> jsp:getProperty>li> <li>价格:<jsp:getProperty name="produce" property="price"> jsp:getProperty>li> ul> div> body> html>本示例通过
public class Person { private String name;//名字 private int age;//年龄 private String sex;//性别 private String add;//地址 public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public String getAdd() { return add; } public void setAdd(String add) { this.add = add; } }
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Titletitle> head> <body> <form name="form1" action="reg.jsp"> <table align="center" width="400" height="200" border="1"> <tr> <td align="center" colspan="2" height="40"> <b>添加用户信息b> td> tr> <tr> <td align="right">姓名:td> <td> <input type="text" name="name"> td> tr> <tr> <td align="right">年龄td> <td> <input type="text" name="age"> td> tr> <tr> <td align="right">性别:td> <td> <input type="text" name="sex"> td> tr> <tr> <td align="right">地址:td> <td> <input type="text" name="add"> td> tr> <tr> <td colspan="2" align="center"> <input type="submit" value="添加"> td> tr> table> form> body> html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Titletitle> head> <body> <%request.setCharacterEncoding("UTF-8");%> <jsp:useBean id="person" class="lyt.com.Person" scope="page"> <jsp:setProperty name="person" property="*">jsp:setProperty> jsp:useBean> <table align="center" width="400"> <tr> <td align="right">姓名:td> <td> <jsp:getProperty name="person" property="name">jsp:getProperty> td> tr> <tr> <td align="right">年龄:td> <td> <jsp:getProperty name="person" property="age">jsp:getProperty> td> tr> <tr> <td align="right">性别:td> <td> <jsp:getProperty name="person" property="sex">jsp:getProperty> td> tr> <tr> <td align="right">地址:td> <td> <jsp:getProperty name="person" property="add"> jsp:getProperty> td> tr> table> body> html>表单中信息中的属性名称最好设置为JavaBean中的属性名称,这样就可以通过“
JavaBean在JSP页面中应用十分广泛,几乎所有的JSP页面中,所有的实体对象及业务逻辑的处理都有JavaBean封装,因此,JavaBean与JSP之间的关系十分密切。
1、解决中文乱码的JavaBean
在JSP页面中,处理中文字符经常会出现字符乱码的现象,特别是通过表单传递中文数据时容易产生。它的解决方法很多,如将request的字符集指定为中文字符集,编写JavaBean对中文乱码字符进行转码。
例子:
public class News { private String title; private String content; public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } }
public class CharactorEncoding { /** * 构造方法 */ public CharactorEncoding(){} /** * 对字符进行转码处理 * @param str 要转码的字符串 * @return 编码后的字符串 */ public String toString(String str) { String text="";//转换字符 if(str!=null&&!"".equals(str)){ try { text=new String(str.getBytes("ISO-8859-1"),"UTF-8");//对字符进 行编码处理 } catch (UnsupportedEncodingException e) { e.printStackTrace(); } } return text;//返回后的字符串 } }
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Titletitle> head> <body> <form name="form1" action="release.jsp" method="post"> <table align="center" width="450" height="260" border="1"> <tr> <td align="center" colspan="2"> <b>新闻发布b> td> tr> <tr> <td align="right">标题td> <td> <input type="text" name="title"> td> tr> <tr> <td align="right">内容td> <td> <textarea name="content" cols="40" rows="8">textarea> td> tr> <tr> <td colspan="2" align="center"> <input type="submit" value="发布"> td> tr> table> form> body> html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>新闻发布结果title> head> <body> <jsp:useBean id="news" class="lyt.bean.News">jsp:useBean> <jsp:useBean id="encoding" class="lyt.bean.CharactorEncoding">jsp:useBean> <jsp:setProperty name="news" property="*">jsp:setProperty> <%=encoding.toString(news.getTitle())%> <%=encoding.toString(news.getContent())%> body> html>通过
public class DateTime { private String dateTime;//日期与时间 private String week;//星期 private Calendar calendar=Calendar.getInstance() ;//Calendar对象 /** * 获得当前日期及时间 * @return 日期及时间的字符串 */ public String getDateTime(){ Date currentDate=Calendar.getInstance().getTime();//获得当前时间 SimpleDateFormat sdf=new SimpleDateFormat("yyyy年MM月dd日 HH点mm分ss秒");//实例化 SimpleDateFormat dateTime=sdf.format(currentDate);//格式化日期时间 return dateTime;//返回日期及时间的字符串 } public String getWeek(){ String[] weeks={"星期日","星期一","星期二","星期三","星期四","星期五","星 期六"};//定义数组 int index=calendar.get(Calendar.DAY_OF_WEEK);//获取一星期的某天索引 week=weeks[index-1];//获取星期几 return week; } }
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>时钟走动title> <body> <style type="text/css"> #clock{ width: 420px; height: 80px; background: #EOEOEO; font-size: 25px; font-weight: bold; border: solid 5px orange; padding: 20px; } #week{ padding-top: 15px; color: #0080FF; } style> <meta http-equiv="Refresh" content="1"> head> <body> <jsp:useBean id="dateTime" class="lyt.date.DateTime" scope="application">jsp:useBean> <div align="center"> <div id="clock"> <div id="time"> <jsp:getProperty name="dateTime" property="dateTime">jsp:getProperty> div> <div id="week"> <jsp:getProperty name="dateTime" property="week">jsp:getProperty> div> div> div> body> html>因为获取当前日期的JavaBean对象DateTime并不涉及更多的业务逻辑,所以实例中将它的作用域设置为application。这样做的好处是,在JSP页面中第一次调用该对象时实例化一个DateTime对象,以后再次调用时不需要再次实例化DateTime,因为它在application范围内已经存在。
public class Paper implements Serializable { private static final long serialVersionUID=1L; private String[] languages;//定义保存编程语言的字符串数组 private String[] technics;//定义保存掌握技术的字符串数组 private String[] parts;//定义保存困难部分的字符数组 public Paper(){ } public String[] getLanguages() { return languages; } public void setLanguages(String[] languages) { this.languages = languages; } public String[] getTechnics() { return technics; } public void setTechnics(String[] technics) { this.technics = technics; } public String[] getParts() { return parts; } public void setParts(String[] parts) { this.parts = parts; } }
public class Convert { /** * 将数组转换为字符串 * @param arr 数组 * @return 字符串 */ public String arr2Str(String[] arr){ StringBuffer sb=new StringBuffer(); if(arr!=null&&arr.length>0){ for(String s:arr){ sb.append(s); sb.append(","); } if(sb.length()>0){ sb.deleteCharAt(sb.length()-1); } } return sb.toString(); } }
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Titletitle> head> <body> <form action="reg.jsp" method="post"> <div> <h1>调查问卷h1> <hr/> <ul> <li>你经常用哪些编程语言开发程序li> <li> <input type="checkbox" name="languages" value="JAVA">JAVA <input type="checkbox" name="languages" value="PHP">PHP <input type="checkbox" name="languages" value=".NET">.NET <input type="checkbox" name="languages" value="Python">Python li> ul> <ul> <li>你目前掌握的技术:li> <li> <input type="checkbox" name="technics" value="HTML">HTML <input type="checkbox" name="technics" value="JAVA BEAN">JAVA BEAN <input type="checkbox" name="technics" value="JSP">JSP <input type="checkbox" name="technics" value="Sping MVC">Spring MVC li> ul> <ul> <li>在学习中有哪一部分感觉有困难:li> <li> <input type="checkbox" name="parts" value="JSP">JSP <input type="checkbox" name="parts" value="STRUTS">STRUTS li> ul> <input type="submit" value="提 交"> div> form> body> html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Titletitle> head> <body> <jsp:useBean id="paper" class="lyt.string.Paper">jsp:useBean> <jsp:useBean id="convert" class="lyt.string.Convert">jsp:useBean> <jsp:setProperty name="paper" property="*">jsp:setProperty> <div> <h1>调查结果h1> <hr/> <ul> <li> 你经常使用的编程语言: <%=convert.arr2Str(paper.getLanguages())%> li> <li> 你目前掌握的技术: <%=convert.arr2Str(paper.getTechnics())%> li> <li> 在学习中感觉有困难的部分: <%=convert.arr2Str(paper.getParts())%> div> li> ul> body> html>至此,JavaBean技术讲完了。