影院管理系统的后台(一): 注册

人机界面课团队完成影院管理系统界面的设计,在此基础上添加后台。

首先先完善注册和登录的功能。

注册:使用JavaMail的方法,向一个邮箱发送邮件,邮件里包含一个链接,点击链接后激活。

影院管理系统的后台(一): 注册_第1张图片

界面登录代码:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>


	
	  
	  剧院管理系统
		
		
		
		
	

	

		
(红色文本)点击注册按钮,跳转到注册界面:

影院管理系统的后台(一): 注册_第2张图片

注册界面代码:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>




用户注册界面



     

注册页面


跳转逻辑:

影院管理系统的后台(一): 注册_第3张图片

编写两个类:UserDao,UserManager

UserManager类包含的方法:

注册用户(生成激活码,并调用UserDao的AddUser添加用户,调用发送邮件方法):

public Boolean Register(String username,String password,String email){
		Boolean result=false;
		//生成用户code
		String code=UUID.randomUUID().toString().replace("-", "");
		
		//添加用户
		UserDao userDao=new UserDao();
		result=userDao.AddUser(username, password, email, code);
		
		//向用户发送激活邮件
		sendMail(email,code);
		
		return result;
	}
	

发送激活邮件(使用JavaMail发送,邮件内容为一个链接(跳转到ActiveServlet-激活激活码)):

public static boolean sendMail(String to, String code) {  
        try {  
            Properties props = new Properties();  
            props.put("username", "发送邮箱名");   
            props.put("password", "邮箱密码");   
            props.put("mail.transport.protocol", "smtp" );  
            props.put("mail.smtp.host", "smtp.163.com");  
            props.put("mail.smtp.port", "25" ); 
            Session mailSession = Session.getDefaultInstance(props);  
            
            Message msg = new MimeMessage(mailSession);     
            msg.setFrom(new InternetAddress("[email protected]"));  
            msg.addRecipients(Message.RecipientType.TO, InternetAddress.parse(to));   
            msg.setSubject("激活邮件");   
            msg.setContent("

此邮件为官方激活邮件!请点击下面链接完成激活操作!

http://localhost:8080/ONETTMS/ActiveServlet

","text/html;charset=UTF-8"); msg.saveChanges(); Transport transport = mailSession.getTransport("smtp"); transport.connect(props.getProperty("mail.smtp.host"), props .getProperty("username"), props.getProperty("password")); transport.sendMessage(msg, msg.getAllRecipients()); transport.close(); } catch (Exception e) { e.printStackTrace(); System.out.println(e); return false; } return true; }

激活用户(通过激活码找到对应的用户名,调用UserDao的setState方法将用户设为可用):

public Boolean Active(String code){
		UserDao userDao=new UserDao();
		String username=userDao.findUserByCode(code);
		if(username!=null && username!=""){
			//如果存在用户,将此用户状态设为可用
			userDao.setState(username);
			return true;
		}else{
			return false;
		}
	}

UserDao包含的方法:

向数据库中添加用户(用户信息:用户名,密码,邮箱,激活码,状态):

	public Boolean AddUser(String username,String password,String email,String code){
		Boolean result=false;
		Connection conn=null;
		String sql="insert into t_user(username,password,email,code,state) values(?,?,?,?,?)";
		try {
			//注册驱动
			Class.forName("com.mysql.jdbc.Driver");
			//建立连接
			conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/SendMail","root","密码");
			PreparedStatement pstmt=conn.prepareStatement(sql);
			pstmt.setString(1,username);
			pstmt.setString(2,password);
			pstmt.setString(3,email);
			pstmt.setString(4,code);
			pstmt.setInt(5,0);
			result=pstmt.execute();
		}catch(ClassNotFoundException e){
			e.printStackTrace();
		}catch (SQLException e) {
			e.printStackTrace();
		}
		return result;
	}

获得激活码对应的用户名:

public String findUserByCode(String code) {
		String username="";
		Connection conn=null;
		ResultSet rs=null;
		String sql="select username from t_user where code=?";
		try {
			//注册驱动
			Class.forName("com.mysql.jdbc.Driver");
			//建立连接
			conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/SendMail","root","密码");
			PreparedStatement pstmt=conn.prepareStatement(sql);
			pstmt.setString(1,code);			
			rs=pstmt.executeQuery();
			if(rs.next()){
				username=rs.getString("username");
			}			
		}catch(ClassNotFoundException e){
			e.printStackTrace();
		}catch (SQLException e) {
			e.printStackTrace();
		}
		return username;
	}

将用户状态设为可用(通过 已知的用户名):

public Boolean setState(String username) {
		Boolean result=false;
		Connection conn=null;
		String sql="update t_user set state=1 where username=?";
		try {
			//注册驱动
			Class.forName("com.mysql.jdbc.Driver");
			//建立连接
			conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/SendMail","root","密码");
			PreparedStatement pstmt=conn.prepareStatement(sql);
			pstmt.setString(1,username);
			int i=pstmt.executeUpdate();
			if(i>0){
				result=true;
			}
		}catch(ClassNotFoundException e){
			e.printStackTrace();
		}catch (SQLException e) {
			e.printStackTrace();
		}
		return result;
	}

(注册界面的红色文本)跳转到了RegisterServlet

package servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import service.UserManager;


@WebServlet("/RegisterServlet")
public class RegisterServlet extends HttpServlet {

	private static final long serialVersionUID = -4330309819191817539L;

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		String username=req.getParameter("username");
		String password=req.getParameter("password");
		String email=req.getParameter("email");
		UserManager userManager=new UserManager();
		if(userManager.Register(username, password, email)){
			//注册成功后,转发到成功界面	
			req.setAttribute("message", "注册成功!");
			req.getRequestDispatcher("/succes.jsp").forward(req,resp);
		}
	}


}

这样的话,就已经向数据库里添加了用户,但是这个时候用户的状态还是初始状态。去用户填写的邮箱,已经收到了激活邮件,点击邮件中的链接,链接包含了激活码。所以在ActiveServlet中可以通过request获取传递过来的code,然后调用方法激活。

package servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import service.UserManager;


@WebServlet("/ActiveServlet")
public class ActiveServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		String code=req.getParameter("code");
		UserManager userManager=new UserManager();
		if(userManager.Active(code)){
			//激活成功后,转发到成功界面		
			req.setAttribute("message", "激活成功!");
			req.getRequestDispatcher("/succes.jsp").forward(req,resp);
		}
	}
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doPost(req,resp);
	}
}

你可能感兴趣的:(Java,Web)