Java中模拟web页面用户登录

在页面输入用户名和密码,验证码.将用户名和密码和数据库中的用户表中的信息进行比较如果用户存在则比较用户在页面输入的验证码,如果验证码和当前页面的验证码(点击验证码或换一换,验证码就随时更新)内容一致(忽略大小写)怎在页面提示登录成功,否则在页面输出登录失败.

页面代码:




    
    首页
    


    
用户名
密码
验证码(忽略大小写):

看不清,换一张

web层代码:

import cn.itcast.domain.Users;
import cn.itcast.service.MyService;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;

public class MyServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");//防止中文乱码
        Map map = request.getParameterMap();//获取用户从页面传来的数据
        Users user = new Users();
        try {
            BeanUtils.populate(user, map);//将数据封装到对象中
            MyService myService = new MyService();
            int count = myService.login( user);
            System.out.println(count);
            if (count == 0) {
                response.getWriter().write("用户名或密码错误");
            } else {
                request.getRequestDispatcher("/ms2").forward(request,response);//使用请求转发
            }
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }


    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
                doPost(request,response);
    }
}

请求转发的servlet代码:

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 MyServlet1 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset = utf-8");//防止中文乱码

        String code =(String) request.getSession().getAttribute("code");//获取当前session中的验证码信息
        String yzm = request.getParameter("yzm");//获取用户在页面输入的验证码
        if (yzm == null || !yzm.equalsIgnoreCase(code)) {//验证不通过
            response.getWriter().write("验证码错误请重新填写");
            request.getSession().removeAttribute("code");//删除当前的验证码
        } else {
            response.getWriter().write("登录成功");
            request.getSession().removeAttribute("code");//删除当前的验证码
        }

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}

service层代码:

import cn.itcast.dao.MyDao;
import cn.itcast.domain.Users;

public class MyService {

    public int login(Users user) {
        MyDao myDao = new MyDao();
        int count = myDao.login(user);
        return count;
    }
}

dao层代码:

import cn.itcast.domain.Users;
import cn.itcast.utlis.JdbcUtils;
import org.springframework.jdbc.core.JdbcTemplate;

import javax.sql.DataSource;

public class MyDao {
    public int login(Users user) {
        //到数据库中查询数据并将结果返回
        String sql = " select count(*) from users where name=? and password =?";
        DataSource dataSource = JdbcUtils.getDataSource();
        JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
        int count = jdbcTemplate.queryForObject(sql, int.class, user.getName(), user.getPassword());
        return count;
    }
}

jdbc工具类代码:

import com.alibaba.druid.pool.DruidDataSourceFactory;

import javax.sql.DataSource;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
/*
定义工具类用来获取druid连接池*/
public class JdbcUtils {
    public static Properties pro = new Properties();
     static DataSource dataSource =null;
    static {
        InputStream in = JdbcUtils.class.getClassLoader().getResourceAsStream("druid.properties");
        try {
            pro.load(in);
             dataSource = DruidDataSourceFactory.createDataSource(pro);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
    public static DataSource getDataSource(){
        return dataSource;
    }
}

需要的配置文件信息:

Java中模拟web页面用户登录_第1张图片

web.xml也需要进行相应的配置.

需要导入的jar包:

Java中模拟web页面用户登录_第2张图片

 

数据库内容:

Java中模拟web页面用户登录_第3张图片

页面输入内容及页面,控制台输出结果:

Java中模拟web页面用户登录_第4张图片

Java中模拟web页面用户登录_第5张图片

页面输入内容及页面,控制台输出结果:

Java中模拟web页面用户登录_第6张图片

页面输入内容及页面,控制台输出结果:

Java中模拟web页面用户登录_第7张图片

 

你可能感兴趣的:(Java中模拟web页面用户登录)