验证邮箱的正则表达式:\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*
javabean类的代码:
1 package com.lyq.bean; 2 3 import java.io.Serializable; 4 5 public class Email implements Serializable{ 6 private static final long serialVersionUID = 1L; 7 private String mailAdd;//email地址 8 private boolean email;//是否是一个标准的email地址 9 public Email() 10 { 11 12 } 13 public Email(String mailAdd) 14 { 15 this.mailAdd = mailAdd; 16 } 17 public boolean isEmail() 18 { 19 String regex = "\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"; 20 21 /*matches()尝试将整个区域与模式匹配,如果匹配成功,则可以通过 start、end 和 group 方法获取更多信息。 22 *返回:当且仅当整个区域序列匹配此匹配器的模式时才返回 true。 23 */ 24 25 if(mailAdd.matches(regex)) 26 { 27 email = true; 28 } 29 return email; 30 } 31 //返回email地址 32 public String getMailAdd() 33 { 34 return mailAdd; 35 } 36 public void setMailAdd(String mailAdd) 37 { 38 this.mailAdd = mailAdd; 39 } 40 }
index.jsp是初始页面
1 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> 2 <% 3 String path = request.getContextPath(); 4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 5 %> 6 7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 8 <html> 9 <head> 10 <base href="<%=basePath%>"> 11 12 <title>My JSP 'index.jsp' starting page</title> 13 <meta http-equiv="pragma" content="no-cache"> 14 <meta http-equiv="cache-control" content="no-cache"> 15 <meta http-equiv="expires" content="0"> 16 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> 17 <meta http-equiv="description" content="This is my page"> 18 <!-- 19 <link rel="stylesheet" type="text/css" href="styles.css"> 20 --> 21 </head> 22 23 <body> 24 <form action="result.jsp" method="post"> 25 <table align="center" width="300" border="2" height="150"> 26 <tr> 27 <td colspan="2" align="center">邮箱认证系统</td> 28 </tr> 29 <tr> 30 <td align="right">邮箱地址:</td> 31 <td><input type="text" name="mailAdd"/></td> 32 </tr> 33 <tr> 34 <td colspan="2" align="center"><input type="submit"/></td> 35 </tr> 36 </table> 37 </form> 38 </body> 39 </html>
result.jsp是结果页面
1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 2 <% 3 String path = request.getContextPath(); 4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 5 %> 6 <%@page import="com.lyq.bean.Email" %> 7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 8 <html> 9 <head> 10 <base href="<%=basePath%>"> 11 12 <title>My JSP 'result.jsp' starting page</title> 13 14 <meta http-equiv="pragma" content="no-cache"> 15 <meta http-equiv="cache-control" content="no-cache"> 16 <meta http-equiv="expires" content="0"> 17 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> 18 <meta http-equiv="description" content="This is my page"> 19 <!-- 20 <link rel="stylesheet" type="text/css" href="styles.css"> 21 --> 22 23 </head> 24 25 <body> 26 <div align="center"> 27 <% 28 //获取邮箱地址 29 String mailAdd = request.getParameter("mailAdd"); 30 Email email = new Email(mailAdd); 31 if(email.isEmail()){ 32 out.print(mailAdd+"<br>是一个标准的邮箱地址!<br>"); 33 }else 34 { 35 out.print(mailAdd+"<br>不是一个标准的邮箱地址!<br>"); 36 } 37 %> 38 <a href="index.jsp">返回</a> 39 </div> 40 </body> 41 </html>