JavaWeb 简单项目实现(一) 登录功能

1.超市管理系统

做一个简单的项目,来理解数据库,Servlet,JSP页面以及其他工具的具体使用。

2.登录功能实现

最底层是访问数据的基础Dao类,这个类可以被用来访问数据库中的所有类。

package com.code.dao;

import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

public class BaseDao {

    private static String url;
    private static String username;
    private static String password;

    static {
        InputStream is = BaseDao.class.getClassLoader().getResourceAsStream("db.properties");
        Properties properties = new Properties();

        try {
            properties.load(is);

        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        url = properties.getProperty("url");
        username = properties.getProperty("username");
        password = properties.getProperty("password");


    }

    public static Connection getConnection(){
        Connection connection = null;
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            connection = DriverManager.getConnection(url, username, password);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return connection;
    }

    public static ResultSet execute(Connection connection, PreparedStatement statement, String sql, Object[] params, ResultSet resultSet) throws Exception {
        statement = connection.prepareStatement(sql);
        for(int i=0;i

因为登录要用到的是User类,所以再实现一个UserDaoIml类,专门用来获取User对象。这样就可以根据用户在登录页面输入的用户名取数据库中查找用户名和密码了。

package com.code.dao.user;

import com.code.dao.BaseDao;
import com.code.pojo.User;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class UserDaoImpl implements UserDao{
    public User getLoginUser(Connection connection, String userCode) throws Exception {

        User user = null;


        if(connection!=null){
            String sql = "select* from smbms_user where userCode = ?";
            Object[] params = {userCode};

            PreparedStatement preparedStatement = null;
            ResultSet resultSet = null;

            resultSet = BaseDao.execute(connection, preparedStatement, sql, params,resultSet);

            while(resultSet.next()){
                user = new User();
                user.setAddress(resultSet.getString("address"));
                user.setUserCode(resultSet.getString("userCode"));
                user.setUserName(resultSet.getString("userName"));
                user.setUserPassword(resultSet.getString("userPassword"));
                user.setGender(resultSet.getString("gender"));
                user.setPhone(resultSet.getString("phone"));

                user.setUserRole(resultSet.getInt("userRole"));

                user.setCreatedBy(resultSet.getInt("createdBy"));
                user.setId(resultSet.getInt("id"));
                user.setModifyBy(resultSet.getInt("modifyBy"));

                user.setBirthday(resultSet.getDate("birthday"));
                user.setCreationDate(resultSet.getDate("creationDate"));
                user.setModifyDate(resultSet.getDate("modifyDate"));
            }



            BaseDao.close(connection,preparedStatement,resultSet);
        }


        return user;
    }
}

再在业务层使用UserServiceImpl类调用UserDaoIml类中的获取登录用户的方法:这里其实就是在UserDaoIml类上又封装了一层,因为这一层也会用到BaseDao类中的方法,servlet中不应该处理这类代码(不是servlet做不了,而是出于功能划分的目的)

package com.code.service.user;

import com.code.dao.BaseDao;
import com.code.dao.user.UserDao;
import com.code.dao.user.UserDaoImpl;
import com.code.pojo.User;
import org.junit.Test;

import java.sql.Connection;

public class UserServiceImpl implements UserService{


    private UserDao userDao;

    public UserServiceImpl() {
        this.userDao = new UserDaoImpl();
    }


    public User login(String userCode, String password) {
        User user = null;
        Connection connection = null;

        try {
            connection = BaseDao.getConnection();
            user = userDao.getLoginUser(connection, userCode);

        } catch (Exception e) {
            throw new RuntimeException(e);
        }finally {
            BaseDao.close(connection, null, null);
        }

        return user;
    }

    @Test
    public void test(){
        UserServiceImpl userService = new UserServiceImpl();
        User user = userService.login("admin", "kkkk");
        System.out.println(user.getUserName());
        System.out.println(user.getUserPassword());
    }

}

最高层就是servlet了,先从jsp页面得到用户名和密码,再用UserServiceImpl来查询数据库中的对应对象。检查用户是否存在和密码是否正确,再跳转至对应的页面:

package com.code.servlet.user;

import com.code.pojo.User;
import com.code.service.user.UserService;
import com.code.service.user.UserServiceImpl;
import com.code.util.Constants;

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

public class LoginServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("---enter in LoginServlet---");

        String userCode = req.getParameter("userCode");
        String userPassword = req.getParameter("userPassword");

        UserService userService = new UserServiceImpl();

        User user = userService.login(userCode, userPassword);
        if(user!=null && userPassword.equals(user.getUserPassword())){
            req.getSession().setAttribute(Constants.USER_SESSION, user);
            resp.sendRedirect("jsp/frame.jsp");
        }else{
            req.setAttribute("error", "用户名不存在或者密码错误");
            req.getRequestDispatcher("login.jsp").forward(req,resp);
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}

最后再在web.xml中配置好servlet就可以了:

JavaWeb 简单项目实现(一) 登录功能_第1张图片

 

你可能感兴趣的:(web开发,java,web)