例1.通過非可視化的JavaBean,封裝郵箱地址對象,通過JSP頁面調用該對象來驗證郵箱地址是否合法。
(1)創建名稱為Email的JavaBean對象,用於封裝郵箱地址,關鍵代碼如下:
package com.cn.gao; import java.io.Serializable; public class Email implements Serializable { //serialVersionUID值 private static final long serialVersionUID=1L; //Email地址 private String mailAdd; //是否是一個標准的Email地址 private boolean email; /** * 默認無參數的構造方法 */ public Email(){ } public Email(String mailAdd){ this.mailAdd=mailAdd; } /** * 是否是一個標准的Email地址 * @return 布爾值 */ public boolean isEmail(){ //正則表達式,定義郵箱格式 String regex = "\\w+([-+.']\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*"; //matches()方法可判斷字符串是否與正則表達式匹配 if(mailAdd.matches(regex)){ //email為真 email=true; } return email; } public String getMailAdd(){ return mailAdd; } public void setMailAdd(String mailAdd){ this.mailAdd=mailAdd; } }
說明:雖然在JavaBean的規范中,要求JavaBean對象提供默認無參的構造方法,但除默認無參構造方法外,JavaBean對象也可以根據相關屬性提供構造方法,所以Email類為了實例化方便,還提供了使用mailAdd實現的一個構造方法。
(2)創建名為index.jsp的頁面,它是程序中的首頁,用於放置驗證郵箱的表單,該表單的提交地址為result.jsp頁面。關鍵代碼如下:
<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%> <% 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>My JSP 'index.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <form action="result.jsp" method="post"> <table align="center" width="300" border="1" height="150"> <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 colspan="2" align="center"> <input type="submit" name="Submit" value="提交查詢內容"/> </td> </tr> </table> </form> </body> </html>
(3)創建名稱為result.jsp的頁面,對index.jsp頁面中的表單進行處理在此頁面中實例化Email對象,對郵箱地址進行驗證,並將驗證結果輸出到頁面中。關鍵代碼如下:
<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%> <%@page import="com.cn.gao.Email"%> <% 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>My JSP 'result.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <div align="center"> <% //獲取郵箱地址 String mailAdd=request.getParameter("mailAdd"); //實例化Email,並對mailAdd賦值 Email email=new Email(mailAdd); //判斷是否是標准的郵箱地址 if(email.isEmail()){ out.print(mailAdd + "<br>是一個標准的郵箱地址!<br>"); }else{ out.print(mailAdd + "<br>不是一個標准的郵箱地址!<br>"); } %> <a href="index.jsp">返回</a> </div> </body> </html>
該頁面通過JSP的內置對象request,接收表單傳遞的mailAdd值,然后通過該值來實例化Email對象,通過Email的isEmail()方法判斷郵箱地址是否合法,並在頁面中輸出判斷結果。
