一个简单的用户注册、登录和利用邮箱找回密码的S2SH程序
一个简单的用户注册、登录和利用邮箱找回密码的S2SH程序
1、在model(po,vo等)层建立一个用户(User.java)信息
a)用户信息
View Code
1 package com.ibm.forgotpassword.model; 2 3 public class User { 4 5 private int id; 6 private String email; 7 private String username; 8 private String password; 9 10 public int getId() { 11 return id; 12 } 13 public void setId(int id) { 14 this.id = id; 15 } 16 public String getEmail() { 17 return email; 18 } 19 public void setEmail(String email) { 20 this.email = email; 21 } 22 public String getUsername() { 23 return username; 24 } 25 public void setUsername(String username) { 26 this.username = username; 27 } 28 public String getPassword() { 29 return password; 30 } 31 public void setPassword(String password) { 32 this.password = password; 33 } 34 35 }
b)建立对应的User.hbm.xml
View Code
1 <?xml version="1.0"?> 2 <!DOCTYPE hibernate-mapping PUBLIC 3 "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 4 "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> 5 <hibernate-mapping> 6 <class name="com.ibm.forgotpassword.model.User" table="tb_user"> 7 <id name="id" type="int"> 8 <column name="id" /> 9 <generator class="native" /> 10 </id> 11 <property name="email" type="java.lang.String"> 12 <column name="email" /> 13 </property> 14 <property name="username" type="java.lang.String"> 15 <column name="username" /> 16 </property> 17 <property name="password" type="java.lang.String"> 18 <column name="password" /> 19 </property> 20 </class> 21 </hibernate-mapping>
2 DAO层(IUserDAO接口和UserDAO实现类)
a)IUserDAO接口
View Code
1 package com.ibm.forgotpassword.dao; 2 3 import com.ibm.forgotpassword.model.User; 4 5 public interface IUserDAO { 6 7 /** 8 * 增加 9 * @param user 10 * @return 11 */ 12 public boolean save(User user); 13 14 /** 15 * 根据邮箱查找用户信息 16 * @param email 17 * @return 18 */ 19 public User findUserByEmail(String email); 20 21 /** 22 * 根据邮箱和密码查找用户信息 23 * @param email 24 * @param password 25 * @return 26 */ 27 public User findUserByEmailAndPssword(String email,String password); 28 29 }
b)UserDAO实现类
View Code
1 package com.ibm.forgotpassword.dao.impl; 2 3 import org.hibernate.HibernateException; 4 import org.hibernate.Query; 5 import org.springframework.orm.hibernate3.support.HibernateDaoSupport; 6 7 import com.ibm.forgotpassword.dao.IUserDAO; 8 import com.ibm.forgotpassword.model.User; 9 10 public class UserDAO extends HibernateDaoSupport implements IUserDAO { 11 12 @Override 13 public User findUserByEmail(String email) { 14 try{ 15 String hql = "from User u where u.email = ?"; 16 Query query = this.getSession().createQuery(hql); 17 query.setString(0, email); 18 User user = (User) query.uniqueResult(); 19 return user; 20 }catch(HibernateException e){ 21 e.printStackTrace(); 22 return null; 23 } 24 } 25 26 @Override 27 public boolean save(User user) { 28 try{ 29 this.getSession().save(user); 30 return true; 31 }catch(HibernateException e){ 32 e.printStackTrace(); 33 return false; 34 } 35 } 36 37 @Override 38 public User findUserByEmailAndPssword(String email, String password) { 39 try{ 40 String hql = "from User u where u.email = ? and u.password = ?"; 41 Query query = this.getSession().createQuery(hql); 42 query.setString(0, email); 43 query.setString(1, password); 44 User user = (User) query.uniqueResult(); 45 return user; 46 }catch(HibernateException e){ 47 e.printStackTrace(); 48 return null; 49 } 50 } 51 52 }
3 service层(IUserService接口和UserService实现类)
a)IUserService接口
View Code
1 package com.ibm.forgotpassword.service; 2 3 import com.ibm.forgotpassword.model.User; 4 5 public interface IUserService { 6 7 public boolean save(User user); 8 9 public User findUserByEmail(String email); 10 11 public User findUserByEmailAndPassword(String email,String password); 12 13 }
b)UserService实现类
View Code
1 package com.ibm.forgotpassword.service.impl; 2 3 import com.ibm.forgotpassword.dao.IUserDAO; 4 import com.ibm.forgotpassword.model.User; 5 import com.ibm.forgotpassword.service.IUserService; 6 7 public class UserService implements IUserService { 8 9 private IUserDAO userDao; 10 11 public IUserDAO getUserDao() { 12 return userDao; 13 } 14 public void setUserDao(IUserDAO userDao) { 15 this.userDao = userDao; 16 } 17 18 @Override 19 public User findUserByEmail(String email) { 20 return userDao.findUserByEmail(email); 21 } 22 @Override 23 public boolean save(User user) { 24 return userDao.save(user); 25 } 26 @Override 27 public User findUserByEmailAndPassword(String email, String password) { 28 return userDao.findUserByEmailAndPssword(email, password); 29 } 30 31 }
4 action(我把注册、登录和找回密码的action分开来写)
a)注册的action
View Code
1 package com.ibm.forgotpassword.action; 2 3 import java.io.IOException; 4 import java.io.PrintWriter; 5 6 import javax.servlet.http.HttpServletResponse; 7 8 import org.apache.struts2.ServletActionContext; 9 10 import com.ibm.forgotpassword.model.User; 11 import com.ibm.forgotpassword.service.IUserService; 12 import com.ibm.forgotpassword.util.Md5s; 13 import com.opensymphony.xwork2.ActionSupport; 14 15 @SuppressWarnings("serial") 16 public class RegisterAction extends ActionSupport { 17 18 private IUserService userService; 19 private User user; 20 public IUserService getUserService() { 21 return userService; 22 } 23 public void setUserService(IUserService userService) { 24 this.userService = userService; 25 } 26 public User getUser() { 27 return user; 28 } 29 public void setUser(User user) { 30 this.user = user; 31 } 32 33 /** 34 * 注册 35 * 页面输入的邮箱和数据库已有的邮箱进行比较 36 * @return 37 */ 38 public String select(){ 39 User u = userService.findUserByEmail(user.getEmail()); 40 HttpServletResponse response = ServletActionContext.getResponse(); 41 response.setContentType("text/html;charset=utf-8"); 42 response.setCharacterEncoding("utf-8"); 43 String responseText = null; 44 try { 45 PrintWriter out = response.getWriter(); 46 if(u == null) 47 responseText = "你所输入的邮箱不存在,可以用来注册"; 48 else 49 responseText = "你所输入的邮箱已经被注册 "; 50 out.write(responseText); 51 out.flush(); 52 out.close(); 53 } catch (IOException e) { 54 e.printStackTrace(); 55 } 56 return null; 57 } 58 59 /** 60 * 注册的action 61 * @return 62 */ 63 public String register(){ 64 user.setPassword(Md5s.md5s(user.getPassword())); 65 if(userService.save(user)) 66 return "success"; 67 else 68 return "register"; 69 } 70 }
b)登录的action
View Code
1 package com.ibm.forgotpassword.action; 2 3 import java.io.IOException; 4 import java.io.PrintWriter; 5 6 import javax.servlet.http.HttpServletResponse; 7 8 import org.apache.struts2.ServletActionContext; 9 10 import com.ibm.forgotpassword.model.User; 11 import com.ibm.forgotpassword.service.IUserService; 12 import com.ibm.forgotpassword.util.Md5s; 13 import com.opensymphony.xwork2.ActionSupport; 14 15 @SuppressWarnings("serial") 16 public class LoginAction extends ActionSupport { 17 18 private IUserService userService; 19 private User user; 20 public IUserService getUserService() { 21 return userService; 22 } 23 public void setUserService(IUserService userService) { 24 this.userService = userService; 25 } 26 public User getUser() { 27 return user; 28 } 29 public void setUser(User user) { 30 this.user = user; 31 } 32 33 /** 34 * 登录 35 * 页面输入的邮箱和数据库的邮箱进行比较 36 * @return 37 */ 38 public String select(){ 39 User u = userService.findUserByEmailAndPassword(user.getEmail(), Md5s.md5s(user.getPassword())); 40 HttpServletResponse response = ServletActionContext.getResponse(); 41 response.setContentType("text/html;charser=utf-8"); 42 response.setCharacterEncoding("utf-8"); 43 String responseText = null; 44 try { 45 PrintWriter out = response.getWriter(); 46 if(u == null) 47 responseText = "你输入的邮箱有误"; 48 else 49 responseText = "你所输入的邮箱正确可以登录"; 50 out.write(responseText); 51 out.flush(); 52 out.close(); 53 } catch (IOException e) { 54 e.printStackTrace(); 55 } 56 return null; 57 } 58 59 /** 60 * 登录的action 61 * @return 62 */ 63 public String login(){ 64 User u = userService.findUserByEmailAndPassword(user.getEmail(), Md5s.md5s(user.getPassword())); 65 if(u != null){ 66 return "success"; 67 }else 68 return "login"; 69 } 70 }
c)找回密码的action
View Code
1 package com.ibm.forgotpassword.action; 2 3 import java.util.Date; 4 import java.util.Properties; 5 6 import javax.mail.Address; 7 import javax.mail.Message; 8 import javax.mail.MessagingException; 9 import javax.mail.NoSuchProviderException; 10 import javax.mail.Session; 11 import javax.mail.Transport; 12 import javax.mail.internet.InternetAddress; 13 import javax.mail.internet.MimeMessage; 14 15 import com.ibm.forgotpassword.model.User; 16 import com.ibm.forgotpassword.service.IUserService; 17 import com.ibm.forgotpassword.util.Md5s; 18 import com.opensymphony.xwork2.ActionContext; 19 import com.opensymphony.xwork2.ActionSupport; 20 21 @SuppressWarnings("serial") 22 public class GetPasswordAction extends ActionSupport { 23 24 private User user; 25 private IUserService userService; 26 public User getUser() { 27 return user; 28 } 29 public void setUser(User user) { 30 this.user = user; 31 } 32 public IUserService getUserService() { 33 return userService; 34 } 35 public void setUserService(IUserService userService) { 36 this.userService = userService; 37 } 38 39 /** 40 * 找回密码的action,并在其中判断输入的邮箱是否在数据库中已经存在 41 * @return 42 */ 43 public String getpassword(){ 44 User u = userService.findUserByEmail(user.getEmail()); 45 if(u == null){ 46 ActionContext.getContext().put("email", "你输入的邮箱不存在"); 47 return "sendEmail"; 48 }else{ 49 int rand = (int) (Math.random()*100000000); 50 String pass = String.valueOf(rand); 51 String content = "亲爱的用户您好!\n"+" 您的注册邮箱:"+user.getEmail()+"密码:"+pass+"\n 请您记住。"; 52 find_password(u.getEmail(), content); 53 user.setPassword(Md5s.md5s(pass));//将修改后的密码加密放进数据库中 54 return "successs"; 55 } 56 } 57 58 /** 59 * 60 * @param email 61 * @param content 62 */ 63 public static void find_password(String email,String content){ 64 Properties props = new Properties(); 65 props.setProperty("mail.transport.protocol", "smtp"); 66 props.setProperty("mail.smtp.auth", "true"); 67 Session session = Session.getInstance(props); 68 session.setDebug(true); 69 70 Transport transport; 71 try { 72 transport = session.getTransport(); 73 Message msg = new MimeMessage(session); 74 msg.setSubject("密码修改"); 75 msg.setFrom(InternetAddress.parse("<zhaohuimima@163.com>")[0]);//新建163邮箱没有发送邮件的功能 76 msg.setSentDate(new Date()); 77 msg.setText(content); 78 79 80 //主机是smtp.163.com(端口25可以不写) 81 //transport.connect("smtp.163.com","25","zhaohuimima", "mima"); 82 //163邮箱貌似不能像qq.com发邮件 83 transport.connect("smtp.163.com", "zhaohuimima", "mima"); 84 transport.sendMessage(msg, new Address[]{ 85 new InternetAddress(email), 86 }); 87 transport.close(); 88 } catch (NoSuchProviderException e) { 89 e.printStackTrace(); 90 } catch (MessagingException e) { 91 e.printStackTrace(); 92 } 93 94 } 95 }
5 Md5密码加密程序
View Code
1 package com.ibm.forgotpassword.util; 2 3 import java.security.MessageDigest; 4 import java.security.NoSuchAlgorithmException; 5 6 public class Md5s { 7 public static String md5s(String plainText){ 8 try { 9 MessageDigest md = MessageDigest.getInstance("MD5"); 10 md.update(plainText.getBytes()); 11 byte[] b = md.digest(); 12 int i; 13 StringBuffer buf = new StringBuffer(""); 14 for(int offset=0;offset<b.length;offset++){ 15 i = b[offset]; 16 if(i<0) 17 i+=256; 18 if(i<16) 19 buf.append("0"); 20 buf.append(Integer.toHexString(i)); 21 } 22 return buf.toString(); 23 } catch (NoSuchAlgorithmException e) { 24 e.printStackTrace(); 25 return null; 26 } 27 } 28 }
6 几个配置文件
a)applicationContext.xml
View Code
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:aop="http://www.springframework.org/schema/aop" 5 xmlns:tx="http://www.springframework.org/schema/tx" 6 xmlns:context="http://www.springframework.org/schema/context" 7 xsi:schemaLocation=" 8 http://www.springframework.org/schema/beans 9 http://www.springframework.org/schema/beans/spring-beans-2.5.xsd 10 http://www.springframework.org/schema/aop 11 http://www.springframework.org/schema/aop/spring-aop-2.5.xsd 12 http://www.springframework.org/schema/tx 13 http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> 14 15 <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> 16 <property name="configLocation"> 17 <value>classpath:hibernate.cfg.xml</value> 18 </property> 19 </bean> 20 21 <bean id="UserDAO" class="com.ibm.forgotpassword.dao.impl.UserDAO"> 22 <property name="sessionFactory" ref="sessionFactory"></property> 23 </bean> 24 <bean id="UserService" class="com.ibm.forgotpassword.service.impl.UserService"> 25 <property name="userDao" ref="UserDAO"></property> 26 </bean> 27 <bean id="RegisterAction" class="com.ibm.forgotpassword.action.RegisterAction"> 28 <property name="userService" ref="UserService"></property> 29 </bean> 30 <bean id="LoginAction" class="com.ibm.forgotpassword.action.LoginAction"> 31 <property name="userService" ref="UserService"></property> 32 </bean> 33 <bean id="GetPasswordAction" class="com.ibm.forgotpassword.action.GetPasswordAction"> 34 <property name="userService" ref="UserService"></property> 35 </bean> 36 37 <!-- 配置持久化对象进行操作时的事务处理 --> 38 <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> 39 <property name="sessionFactory"> 40 <ref bean="sessionFactory"/> 41 </property> 42 </bean> 43 44 <!-- 声明式事物 --> 45 <aop:config> 46 <aop:pointcut expression="execution(* com.ibm.forgotpassword.service..*.* (..) )" id="serviceMethod"/> 47 <aop:advisor advice-ref="txAdvice" pointcut-ref="serviceMethod"/> 48 </aop:config> 49 50 <!-- 事务传播性配置 --> 51 <tx:advice id="txAdvice" transaction-manager="txManager"> 52 <tx:attributes> 53 <tx:method name="find*" read-only="true"/> 54 <tx:method name="get*" read-only="true"/> 55 <tx:method name="save" propagation="REQUIRED"/> 56 <tx:method name="update" propagation="REQUIRED"/> 57 <tx:method name="delete" propagation="REQUIRED"/> 58 </tx:attributes> 59 </tx:advice> 60 61 </beans>
b)hibernate.cfg.xml
View Code
1 <!DOCTYPE hibernate-configuration PUBLIC 2 "-//Hibernate/Hibernate Configuration DTD 3.0//EN" 3 "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> 4 5 <hibernate-configuration> 6 <session-factory > 7 <property name="connection.driver_class">com.mysql.jdbc.Driver</property> 8 <property name="connection.url">jdbc:mysql://localhost:3306/forgotpassword</property> 9 <property name="connection.username">root</property> 10 <property name="connection.password">root</property> 11 12 <property name="dialect">org.hibernate.dialect.MySQLDialect</property> 13 14 <property name="hibernate.current_session_context_class">thread</property> 15 16 <property name="hibernate_format_sql">true</property> 17 <property name="show_sql">true</property> 18 19 <property name="hibernate.hbm2ddl.auto">update</property> 20 21 <mapping resource="com/ibm/forgotpassword/model/User.hbm.xml"/> 22 </session-factory> 23 </hibernate-configuration>
c)log4j.properties
View Code
1 ### direct log messages to stdout ### 2 log4j.appender.stdout=org.apache.log4j.ConsoleAppender 3 log4j.appender.stdout.Target=System.out 4 log4j.appender.stdout.layout=org.apache.log4j.PatternLayout 5 log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n 6 7 ### direct messages to file hibernate.log ### 8 #log4j.appender.file=org.apache.log4j.FileAppender 9 #log4j.appender.file.File=hibernate.log 10 #log4j.appender.file.layout=org.apache.log4j.PatternLayout 11 #log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n 12 13 ### set log levels - for more verbose logging change 'info' to 'debug' ### 14 15 log4j.rootLogger=warn, stdout 16 17 #log4j.logger.org.hibernate=info 18 #log4j.logger.org.hibernate=debug 19 20 ### log HQL query parser activity 21 #log4j.logger.org.hibernate.hql.ast.AST=debug 22 23 ### log just the SQL 24 #log4j.logger.org.hibernate.SQL=debug 25 26 ### log JDBC bind parameters ### 27 #log4j.logger.org.hibernate.type=info 28 #log4j.logger.org.hibernate.type=debug 29 30 ### log schema export/update ### 31 log4j.logger.org.hibernate.tool.hbm2ddl=debug 32 33 ### log HQL parse trees 34 #log4j.logger.org.hibernate.hql=debug 35 36 ### log cache activity ### 37 #log4j.logger.org.hibernate.cache=debug 38 39 ### log transaction activity 40 #log4j.logger.org.hibernate.transaction=debug 41 42 ### log JDBC resource acquisition 43 #log4j.logger.org.hibernate.jdbc=debug 44 45 ### enable the following line if you want to track down connection ### 46 ### leakages when using DriverManagerConnectionProvider ### 47 #log4j.logger.org.hibernate.connection.DriverManagerConnectionProvider=trace
d)struts.xml
View Code
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE struts PUBLIC 3 "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" 4 "http://struts.apache.org/dtds/struts-2.1.dtd"> 5 <struts> 6 <!-- 开发者模式 --> 7 <constant name="struts.devMode" value="true" /> 8 <constant name="struts.i18n.encoding" value="UTF-8"/> 9 10 <package name="" namespace="/" extends="struts-default"> 11 <action name="registerAction-*" class="RegisterAction" method="{1}"> 12 <result name="success">login.jsp</result> 13 <result name="register">register.jsp</result> 14 </action> 15 <action name="loginAction-*" class="LoginAction" method="{1}"> 16 <result name="success">index.jsp</result> 17 <result name="login">login.jsp</result> 18 </action> 19 <action name="getPasswordAction-*" class="GetPasswordAction" method="{1}"> 20 <result name="success">index.jsp</result> 21 <result name="sendEmail">forgotpassword.jsp</result> 22 </action> 23 </package> 24 </struts>
7 几个jsp页面(WebContent\web\)
a)register.jsp
View Code
1 <%@ page language="java" contentType="text/html; charset=utf-8" 2 pageEncoding="utf-8"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 7 <title>注册页面</title> 8 <script type="text/javascript" src="../js/checkregister.js"></script> 9 </head> 10 <body> 11 <form action="registerAction-register" method="post" name="from" onsubmit="return checkPassword()"> 12 <table> 13 <tr> 14 <td>Email:</td> 15 <td><input type="text" class="text" name="user.email" id="email" onblur="checkUserEmail2()"/></td> 16 <td><span id="msgemail"></span></td> 17 </tr> 18 <tr> 19 <td>username:</td> 20 <td><input type="text" class="text" name="user.username"/></td> 21 </tr> 22 <tr> 23 <td>password:</td> 24 <td><input type="password" class="text" name="user.password" id="password" onblur="checkUsePassword()"/></td> 25 <td><span id="msgpassword"></span></td> 26 </tr> 27 <tr> 28 <td>passwordconfirm:</td> 29 <td><input type="password" class="text" id="passwordConfirm" onblur="checkUsePassword()"/></td> 30 <td><span id="msgpassword"></span></td> 31 </tr> 32 <tr> 33 <td><input type="submit" name="submit" value="提交"></td> 34 <td><input type="reset" name="reset" value="重置"></td> 35 </tr> 36 </table> 37 </form> 38 39 </body> 40 </html>
b)login.jsp
View Code
1 <%@ page language="java" contentType="text/html; charset=utf-8" 2 pageEncoding="utf-8"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 7 <title>登录页面</title> 8 <script type="text/javascript" src="../js/checklogin.js"></script> 9 </head> 10 <body> 11 <form action="loginAction-login" method="post" name="from"> 12 <table> 13 <tr> 14 <td>Email:</td> 15 <td><input type="text" class="text" name="user.email" id="email"/></td> 16 </tr> 17 <tr> 18 <td>password:</td> 19 <td><input type="password" class="text" name="user.password" id="password" onblur="checkUserEmailAndPassword() "/></td> 20 <td><span id="msgemailandpassword"></span></td> 21 </tr> 22 <tr> 23 <td><a href="forgotpassword.jsp">忘记密码?</a></td> 24 </tr> 25 <tr> 26 <td><input type="submit" name="submit" value="提交"></td> 27 <td><input type="reset" name="reset" value="重置"></td> 28 </tr> 29 </table> 30 </form> 31 </body> 32 </html>
c)forgotpassword.jsp
View Code
1 <%@ page language="java" contentType="text/html; charset=utf-8" 2 pageEncoding="utf-8"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 7 <title>找回密码</title> 8 <script type="text/javascript" src="../js/checkgetpassword.js"></script> 9 </head> 10 <body> 11 <form action="getPasswordAction-getpassword" method="post" name="from"> 12 <table> 13 <tr> 14 <td>Email:</td> 15 <td><input type="text" class="text" name="user.email" id="email" onblur="check()"/></td> 16 <td><span id="msgemail"></span></td> 17 </tr> 18 <tr> 19 <td><input type="submit" name="submit" value="提交"></td> 20 <td><input type="reset" name="reset" value="重置"></td> 21 </tr> 22 </table> 23 </form> 24 25 </body> 26 </html>
d)index.jsp
View Code
1 <%@ page language="java" contentType="text/html; charset=utf-8" 2 pageEncoding="utf-8"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 7 <title>Insert title here</title> 8 </head> 9 <body> 10 11 </body> 12 </html>
8 几个js文件、含ajax验证(WebContent\js\)
a)checkregister.js
View Code
1 var xmlHttp; 2 3 function createXMLHttp(){ 4 if(window.XMLHttpRequest) 5 xmlHttp = new XMLHttpRequest(); 6 else 7 xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); 8 } 9 10 function checkUserEmail(){ 11 var email = document.getElementById("email").value; 12 createXMLHttp(); 13 xmlHttp.onreadystatechange = checkUserEmailCallback; 14 xmlHttp.open("GET", "registerAction-select?user.email="+email, true); 15 xmlHttp.send(null); 16 } 17 18 function checkUserEmailCallback(){ 19 if(xmlHttp.readyState == 4 && xmlHttp.status == 200){ 20 var responseText = xmlHttp.responseText; 21 document.getElementById("msgemail").innerHTML = responseText; 22 } 23 } 24 25 function checkUserEmail2(){ 26 var email = document.getElementById("email").value; 27 if(email == null) 28 document.getElementById("msgemail").innerHTML = "请输入有效的邮箱"; 29 else if(!checkEmail(email)) 30 document.getElementById("msgemail").innerHTML = "你所输入的邮箱的格式不正确"; 31 else 32 checkUserEmail(); 33 } 34 35 function checkEmail(email){ 36 var pos1 = email.indexOf("@",0); 37 var pos2 = email.indexOf(".",0); 38 if((pos1 <= 0)||(pos1 == email.length)||(pos2 <= 0)||(pos2 == email.length)) 39 return false; 40 else 41 return true; 42 } 43 44 function checkUsePassword(){ 45 var password = document.getElementById("password").value; 46 var passwordConfirm = document.getElementById("passwordConfirm").value; 47 if(password == null) 48 document.getElementById("msgpassword").innerHTML = "请输入密码"; 49 if(passwordConfirm == null) 50 document.getElementById("msgpassword").innerHTML = "请再次输入密码"; 51 if(password.length < 6||passwordConfirm.length < 6){ 52 document.getElementById("msgpassword").innerHTML = "密码的长度不能小于6位"; 53 password = ""; 54 passwordConfirm = ""; 55 } 56 57 58 } 59 60 function checkPassword(){ 61 var password = document.getElementById("password").value; 62 var passwordConfirm = document.getElementById("passwordConfirm").value; 63 if(passwordConfirm != password){ 64 alert("两次的密码不一致"); 65 password = ""; 66 passwordConfirm = ""; 67 return false; 68 } 69 return true; 70 }
b)checklogin.js
View Code
1 var xmlHttp; 2 3 function createXMLHttp(){ 4 if(window.XMLHttpRequest) 5 xmlHttp = new XMLHttpRequest(); 6 else 7 xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); 8 } 9 10 function checkUserEmailAndPassword(){ 11 var email = document.getElementById("email").value; 12 createXMLHttp(); 13 xmlHttp.onreadystatechange = checkUserEmailCallback; 14 xmlHttp.open("GET", "loginAction-select?user.email="+email+"&user.password="+password, true); 15 xmlHttp.send(null); 16 } 17 18 function checkUserEmailCallback(){ 19 if(xmlHttp.readyState == 4 && xmlHttp.status == 200){ 20 var responseText = xmlHttp.responseText; 21 document.getElementById("msgemailandpassword").innerHTML = responseText; 22 } 23 }
c)checkgetpassword.js
View Code
1 function check(){ 2 var email = document.getElementById("email").value; 3 if(email == null||"".equals(email)){ 4 document.getElementById("msgemail").innerHTML = "请输入有效的邮箱"; 5 } 6 }
9 WebContent\WEB-INF\web.xml的配置
View Code
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> 3 4 <display-name>ForgotPassword</display-name> 5 <context-param> 6 <param-name>contextConfigLocation</param-name> 7 <param-value>classpath:applicationContext.xml</param-value> 8 </context-param> 9 10 <listener> 11 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 12 </listener> 13 14 <filter> 15 <filter-name>struts2</filter-name> 16 <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> 17 </filter> 18 <filter-mapping> 19 <filter-name>struts2</filter-name> 20 <url-pattern>/*</url-pattern> 21 </filter-mapping> 22 23 <welcome-file-list> 24 <welcome-file>web/index.jsp</welcome-file> 25 </welcome-file-list> 26 </web-app>
10 用到的包
邮件:mail.jar(1.4.5版本)
Struts2、Hibernate、Spring、mysql等略。