前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >[javaEE] 三层架构案例-用户模块(二)

[javaEE] 三层架构案例-用户模块(二)

作者头像
唯一Chat
发布2019-09-10 16:31:00
3450
发布2019-09-10 16:31:00
举报
文章被收录于专栏:陶士涵的菜地陶士涵的菜地

使用junit测试框架,测试查找用户和添加用户功能

com.tsh.test.xmlUserDaoTest

代码语言:javascript
复制
package com.tsh.test;


import org.junit.Test;

import com.tsh.dao.XmlUserDao;
import com.tsh.domain.User;
/**
 * 测试用例
 * @author taoshihan
 *
 */
public class xmlUserDaoTest {
    @Test
    public void testFindUserByUsername(){
        XmlUserDao dao=new XmlUserDao();
        User user= dao.findUserByUsername("taoshihan");
        System.out.println(user);
    }
    @Test
    public void testAddUser(){
        XmlUserDao dao=new XmlUserDao();
        User user=new User();
        user.setUsername("taoshihan");
        user.setPassword("123456");
        dao.addUser(user);
    }
}

在逻辑层service层中,抛出自定义异常

com.tsh.service.UserService

代码语言:javascript
复制
package com.tsh.service;

import com.tsh.dao.XmlUserDao;
import com.tsh.domain.User;
import com.tsh.exception.MsgException;
/**
 * 用户逻辑
 * @author taoshihan
 *
 */
public class UserService {
    /**
     * 用户注册
     * @param user
     * @throws MsgException
     */
    public void registerUser(User user) throws MsgException{
        //检查用户名是否存在
        XmlUserDao dao=new XmlUserDao();
        if(dao.findUserByUsername(user.getUsername())!=null){
            throw new MsgException("用户名已经存在");
        }
        dao.addUser(user);
    }
}

在jsp中使用el标签判断登陆状态

代码语言:javascript
复制
<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>用户中心</title>
</head>
<body>
<h1>用户中心</h1>
<hr>
<c:if test="${sessionScope.user==null }">
    欢迎光临,游客!
    <a href="${pageContext.request.contextPath }/login.jsp">登陆</a>
    <a href="${pageContext.request.contextPath }/register.jsp">注册</a>
</c:if>
<c:if test="${sessionScope.user!=null }">
    欢迎光临,${sessionScope.user.username }!
    <a href="${pageContext.request.contextPath }/Servlet/logout">注销</a>
</c:if>
</body>
</html>

在Servlet中对发送过来的数据进行处理

com.tsh.web.LoginServlet

代码语言:javascript
复制
package com.tsh.web;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import com.tsh.dao.XmlUserDao;
import com.tsh.domain.User;

/**
 * 登陆处理
 */
public class LoginServlet extends HttpServlet {
    /**
     */
    public LoginServlet() {
        super();
    }

    /**
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.getWriter().write("sss");
    }

    /**
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html; charset=utf-8");
        String username=request.getParameter("username");
        String password=request.getParameter("password");
        if("".equals(username) || "".equals(password==null)){
            request.setAttribute("msg", "用户名和密码不能为空!");
            request.getRequestDispatcher("/login.jsp").forward(request, response);
            return ;
        }
        XmlUserDao dao=new XmlUserDao();
        User user=dao.findUserByUsername(username);
        
        if(user!=null && user.getPassword().equals(password)){
            request.getSession().setAttribute("user", user);
            response.getWriter().write("登陆成功!");
        }else{
            response.getWriter().write("用户名或密码错误");
        }
    }

}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2016-07-04 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档