A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

本帖最后由 小石姐姐 于 2018-11-1 15:21 编辑

Java tomcat 登陆案例练习
创建user类,封装用户信息
[Java] 纯文本查看 复制代码
package com.itheima.demo01;

public class User {
    private int id;
    private String username;
    private String password;

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                '}';
    }

    public User() {
    }

    public User(int id, String username, String password) {
        this.id = id;
        this.username = username;
        this.password = password;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}

创建UserDao获得数据库信息,并对比用户信息对象user
[Java] 纯文本查看 复制代码
package com.itheima.Dao;

import com.itheima.demo01.User;
import com.itheima.util.JDBCUtils;
import org.junit.Test;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

public class UserDao {

    //声明JDBCTemplate对象共用
    private JdbcTemplate template =new JdbcTemplate(JDBCUtils.getDataSource());

    /*
    * 登陆方法
    * 只有用户名和密码
    * user包含全部用户数据,没有查询到,返回null
    * */

    public User login(User loninUser){

        try {
            String sql = "select * from user where username = ? and password =?";
            User user = template.queryForObject(sql, new BeanPropertyRowMapper<User>(User.class), loninUser.getUsername(), loninUser.getPassword());
            return user;
        } catch (DataAccessException e) {
            e.printStackTrace();//进行日志操作
            return null;
        }

    }
}

创建JDBC工具类型,通过调用起方法获得DataSource 对象
[Java] 纯文本查看 复制代码
package com.itheima.util;

import com.alibaba.druid.pool.DruidDataSourceFactory;

import javax.sql.DataSource;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

//jdbc工具类,使用Durid
public class JDBCUtils {
    //获取连接池对象
    private static DataSource ds;

    static {

        try {

            //1加载配置文件
            Properties pro = new Properties();
            //使用类加载器获取字节输入流
            InputStream is = JDBCUtils.class.getClassLoader().getResourceAsStream("druid.properties");
            pro.load(is);
           ds = DruidDataSourceFactory.createDataSource(pro);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

public static DataSource getDataSource(){
    return ds;
}
    public static Connection getconnection() throws SQLException {
        return ds.getConnection();
    }
}

创建Servlet文件,进行数据处理(核心文件)
[Java] 纯文本查看 复制代码
package com.itheima.domain;

import com.itheima.Dao.UserDao;
import com.itheima.demo01.User;

import javax.servlet.RequestDispatcher;
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 java.io.IOException;

@WebServlet("/ServletTest")
[Java] 纯文本查看 复制代码
public class ServletTest extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        String usern= request.getParameter("username");
        String pass = request.getParameter("password");
//封装客户登陆信息到User对象loginuser中
        User loginuser = new User();
        loginuser.setUsername(usern);
        loginuser.setPassword(pass);

        //获取UserDao对象,调用login方法进行条件判断
        UserDao userDao = new UserDao();
        User user = userDao.login(loginuser);
//如果返回空,则跳转错误页面
if (user == null){
    RequestDispatcher requestDispatcher = request.getRequestDispatcher("/FailServlet");
    requestDispatcher.forward(request,response);
}else {
    //共享数据
    request.setAttribute("usershuju",user);
    //转发
    RequestDispatcher requestDispatcher = request.getRequestDispatcher("/SuccessServlet");
    requestDispatcher.forward(request,response);
}
    }
}

错误页面,数据处理
[Java] 纯文本查看 复制代码
package com.itheima.domain;

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 java.io.IOException;


@WebServlet("/FailServlet")
[Java] 纯文本查看 复制代码
@WebServlet("/FailServlet")
public class FailServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设置编码
        response.setContentType("text/html;charset=utf-8");

        response.getWriter().write("登陆失败,用户名或者密码错误");
    }



    }


正确页面处理
[Java] 纯文本查看 复制代码
package com.itheima.domain;

import com.itheima.Dao.UserDao;
import com.itheima.demo01.User;

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 java.io.IOException;

@WebServlet("/SuccessServlet")
[Java] 纯文本查看 复制代码
public class SuccessServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//获取共享数据
        System.out.println("登陆成功");
        User user =(User) request.getAttribute("usershuju");
        if (user != null){
            //设置编码
            response.setContentType("text/html;charset=utf-8");
            response.getWriter().write("登陆成功"+user.getUsername()+"欢迎您");
        }
    }
}



0 个回复

您需要登录后才可以回帖 登录 | 加入黑马