旅游门户网站01

选用技术:

1.1 Web层

a) Servlet:前端控制器

b) html:视图

c) Filter:过滤器

d) BeanUtils:数据封装

e) Jackson:json序列化工具

1.2 Service层

a) Javamail:java发送邮件工具

b) Redis:nosql内存数据库

c) Jedis:java的redis客户端

1.3 Dao层

a) Mysql:数据库

b) Druid:数据库连接池

c) JdbcTemplate:jdbc的工具

功能实现

注册:

功能分析:
在这里插入图片描述

表单校验:

function checkUsername() {
             var username = $("#username").val();
             //2.定义正则
             var reg_username = /^\w{8,20}$/;
             //3.判断,给出提示信息
             var flag = reg_username.test(username);
             if(flag){
                 //用户名合法
                 $("#username").css("border","");
             }else{
                 //用户名非法,加一个红色边框
                 $("#username").css("border","1px solid red");
             }
             return flag;
         }
function checkPassword() {
             //1.获取密码值
             var password = $("#password").val();
             //2.定义正则
             var reg_password = /^\w{8,20}$/;
             var flag = reg_password.test(password);
             if(flag){
                 //密码合法
                 $("#password").css("border","");
             }else{
                 //密码非法,加一个红色边框
                 $("#password").css("border","1px solid red");
             }

             return flag;
         }
function checkEmail() {
             //1.获取邮箱
             var email = $("#email").val();
             //2.定义正则
             var reg_email = /^\w+@\w+\.\w+$/;
             //3.判断
             var flag = reg_email.test(email);
             if(flag){
                 $("#email").css("border","");
             }else{
                 $("#email").css("border","1px solid red");
             }

             return flag;
         }
function checkTel() {
             //1.获取邮箱
             var tel = $("#telephone").val();
             //2.定义正则
             var reg_tel = /^(13[0-9]|14[5|7]|15[0|1|2|3|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\d{8}$/;
             //3.判断
             var flag = reg_tel.test(tel);
             if(flag){
                 $("#telephone").css("border","");
             }else{
                 $("#telephone").css("border","1px solid red");
             }

             return flag;
         }

异步ajax提交表单:

$(function() {
             $("#registerForm").submit(function() {
      if (checkEmail()&&checkPassword()&&checkUsername()&&checkTel()){
         $.post("user/register",$(this).serialize(),function(data) {
            if (data.flag){
                location.href="http://localhost:80/sumang/register_ok.html";
            }else {
               $("#msg").html(data.errorMsg);
            }
                     },"json");
      }
      return false;

             });
             $("#username").blur(checkUsername);
             $("#password").blur(checkPassword);
             $("#email").blur(checkEmail);
             $("#telephone").blur(checkTel);
         })

RegistUsserServlet:

@WebServlet("/user/*")
public class RegistUserServlet extends HttpServlet {
public void register(HttpServletRequest request,HttpServletResponse response) throws IOException {

    Map<String, String[]> map = request.getParameterMap();
    User user = new User();

    String check = request.getParameter("check");
    String checkcode_server = (String) request.getSession().getAttribute("CHECKCODE_SERVER");
    ResponseInfo responseInfo = new ResponseInfo();
    request.getSession().removeAttribute("CHECKCODE_SERVER");

    if(check.equalsIgnoreCase(checkcode_server)){
        try {
            BeanUtils.populate(user,map);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        //servlet:接受传递的参数,做出响应

        boolean b = userService.addUser(user);//业务逻辑层
        if(b){
            responseInfo.setFlag(true);
        }else {
            responseInfo.setFlag(false);
            responseInfo.setErrorMsg("注册失败");
        }

    }else {
        responseInfo.setFlag(false);
        responseInfo.setErrorMsg("验证码输入错误");
    }

    writeResponse(responseInfo,response);
}
}

UserService:

@Override
public boolean addUser(User user) {
    User user1= userDao.getUserByUserName(user.getUsername());
    if(user1 != null){
        //用户名存在,注册失败
        return false;
    }
    user.setStatus("N");
    user.setCode(UuidUtil.getUuid());
    boolean b = userDao.addUser(user);
    MailUtils.sendMail(user.getEmail(),"<a href='http://10.137.87.36:80/sumang/user/active?code="+user.getCode()+"'>去激活</a>","苏芒旅游网");
    return b;
}

UserDao:

@Override
public boolean addUser(User user) {

    try{
        String sql="insert into tab_user(username,password,name,birthday,sex,telephone,email,code,status) values(?,?,?,?,?,?,?,?,?)";
        jdbcTemplate.update(sql, user.getUsername(),
                user.getPassword(),
                user.getName(),
                user.getBirthday(),
                user.getSex(),
                user.getTelephone(),
                user.getEmail(),user.getCode(),user.getStatus());
        return true;
    }catch (Exception e){
        return false;
    }

}

邮件激活:

功能分析:

在这里插入图片描述

MailUtil.java:(发送邮件)

public class MailUtils {
    // 发件人 账号和密码
    private static final String USER = ""; // 发件人称号,同邮箱地址
    private static final String PASSWORD = ""; // 如果是qq邮箱可以使户端授权码,或者登录密码


    // SMTP服务器(这里用的163 SMTP服务器)
    public static final String MEAIL_163_SMTP_HOST = "smtp.163.com";
    public static final String SMTP_163_PORT = "25";// 端口号,这个是163使用到的;QQ的应该是465或者875


    /**
     *
     * @param to 收件人邮箱
     * @param text 邮件正文
     * @param title 标题
     */
    /* 发送验证信息的邮件 */
    public static boolean sendMail(String to, String text, String title){
        try {
            final Properties props = new Properties();
            props.setProperty("mail.user",USER);
            props.setProperty("mail.password",PASSWORD);
            props.setProperty("mail.smtp.host", MEAIL_163_SMTP_HOST);
            props.setProperty("mail.smtp.port", SMTP_163_PORT);
            props.setProperty("mail.smtp.socketFactory.port", SMTP_163_PORT);
            props.setProperty("mail.smtp.auth", "true");
            props.setProperty("mail.smtp.socketFactory.class", "SSL_FACTORY");

           /* props.put("mail.smtp.auth", "true");
            props.put("mail.smtp.host", "smtp.qq.com");

            // 发件人的账号
            props.put("mail.user", USER);
            //发件人的密码
            props.put("mail.password", PASSWORD);*/

            // 构建授权信息,用于进行SMTP进行身份验证
            Authenticator authenticator = new Authenticator() {
                @Override
                protected PasswordAuthentication getPasswordAuthentication() {
                    // 用户名、密码
                    String userName = props.getProperty("mail.user");
                    String password = props.getProperty("mail.password");
                    return new PasswordAuthentication(userName, password);
                }
            };
            // 使用环境属性和授权信息,创建邮件会话
            Session mailSession = Session.getInstance(props, authenticator);
            // 创建邮件消息
            MimeMessage message = new MimeMessage(mailSession);
            // 设置发件人
            String username = props.getProperty("mail.user");
            InternetAddress form = new InternetAddress(username);
            message.setFrom(form);

            // 设置收件人
            InternetAddress toAddress = new InternetAddress(to);
            message.setRecipient(Message.RecipientType.TO, toAddress);

            // 设置邮件标题
            message.setSubject(title);

            // 设置邮件的内容体
            message.setContent(text, "text/html;charset=UTF-8");
            // 发送邮件
            Transport.send(message);
            return true;
        }catch (Exception e){
            e.printStackTrace();
        }
        return false;
    }

    public static void main(String[] args) throws Exception { // 做测试用
        MailUtils.sendMail("xxxxxxx@qq.com","你好,这是一封测试邮件,无需回复。","测试邮件");
        System.out.println("发送成功");
    }


}

UserServlet:

public void active(HttpServletRequest request,HttpServletResponse response) throws IOException {
    String code = request.getParameter("code");
    Boolean b =userService.active(code);

    if(b){
        response.sendRedirect("/sumang/login.html");
    }else {
        response.getWriter().write("激活失败,请联系管理员");
    }
}

UserService:

@Override
public Boolean active(String code) {
    User user =userDao.getUserByCode(code);
    if(user == null){
        return false;
    }else {
        userDao.active(user);
        return true;
    }
}

UserDao:

@Override
public void active(User user) {
    jdbcTemplate.update("update tab_user set status = 'Y' where uid=? ",user.getUid());
}

登陆:

UserServlet:

public void login(HttpServletRequest request,HttpServletResponse response) throws IOException {

    Map<String, String[]> map = request.getParameterMap();
    User user = new User();
    String check = request.getParameter("check");
    String checkcode_server = (String) request.getSession().getAttribute("CHECKCODE_SERVER");
    ResponseInfo responseInfo = new ResponseInfo();
    request.getSession().removeAttribute("CHECKCODE_SERVER");

    if(check.equalsIgnoreCase(checkcode_server)){
        try {
            BeanUtils.populate(user,map);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }

        User user1= userService.getUserByUserNameAndPassWord(user.getUsername(),user.getPassword());
        if(user1 == null){
            responseInfo.setFlag(false);
            responseInfo.setErrorMsg("用户名或密码错误!");
        }else{
            if(user1.getStatus().equalsIgnoreCase("y")){
                request.getSession().setAttribute("user",user1);
                responseInfo.setFlag(true);
            }else{
                responseInfo.setFlag(false);
                responseInfo.setErrorMsg("请激活!");
            }

        }

    }
    writeResponse(responseInfo,response);
}

UserService:

@Override
public User getUserByUserNameAndPassWord(String username, String password) {
    return userDao.getUserByUserNameAndPassWord(username,password);
}

UserDao:

@Override
public User getUserByUserNameAndPassWord(String username, String password) {
    User user=null;
    try {
        String sql = "SELECT * FROM tab_user WHERE username = ? AND PASSWORD = ? ";
        user = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<User>(User.class), username,password);
    }catch (Exception e){
        user=null;
    }
    return user;
}

前端显示用户信息

$.get("user/findUser",{},function(data) {
    if (data.flag){
        $("#user").html("欢迎回来,"+data.data.name);
    }else {
        if (data.data==null||data.data==""){
            $("#user").html("欢迎回来,<a href='http://localhost:80/sumang/login.html'>请登录</a>");
        }else {
            $("#user").html("欢迎回来,"+data.data);
        }
    }
});

退出:

public void exit(HttpServletRequest request,HttpServletResponse response) throws IOException {
    request.getSession().removeAttribute("user");
    writeResponse("ok",response);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值