微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

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

com.tsh.test.xmlUserDaoTest

 

package com.tsh.test;


import org.junit.Test;

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

 

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

com.tsh.service.UserService

 com.tsh.service;

 com.tsh.exception.MsgException;

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

 

 

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

 

<%@ 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

 com.tsh.web;

 java.io.IOException;

 javax.servlet.servletexception;
 javax.servlet.http.HttpServlet;
 javax.servlet.http.HttpServletRequest;
 javax.servlet.http.HttpServletResponse;


 com.tsh.domain.User;


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

    protected void doGet(HttpServletRequest request,HttpServletResponse response)  servletexception,IOException {
        response.getWriter().write("sss");
    }

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

}

 

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐