java web实现登录功能

用到的技术jsp+servlet+mysql

1.index.jsp

<%@ page language="java" pageEncoding="UTF-8" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
    <title>登录</title>
    <link rel="stylesheet" href="css/bootstrap.min.css">
    <script src="js/jquery.min.js"></script>
    <script src="js/bootstrap.min.js"></script>
    <style>
        boody {
            padding-bottom: 70px;
        }

        .container {
            padding-left: 0px;
            padding-right: 0px;
            border-top-left-radius: 3px;
            border-top-right-radius: 3px;
        }

        #gezi {
            background: #6596c8;
            border: 1px solid white;
            border-top-left-radius: 3px;
            border-top-right-radius: 3px;
            padding: 15px;
            color: white;
        }

        .ahh {
            margin-top: 60px;
        }

        body {
            background: black;
        }

        .footer {
            margin: 30px -1px 30px;
            padding: 30px;
            border-radius: 3px;
            background: #ccc;
            color: #fff;
        }

        .add {
            padding-top: 60px;
            padding-bottom: 100px;
        }
    </style>
</head>

<body>
<div class="container ahh" style="background: #ffffff">
    <div class="row">
        <div class="col-sm-12">
            <div id="gezi">欢迎登录官方网站!</div>
        </div>
    </div>
    <br/>
    <form action="${pageContext.request.contextPath}/LoginServlet" method="post" class="form-horizontal add"
          role="form">
        <h2 class="text-center" style="padding-bottom: 30px;">欢迎登录</h2>
        <div class="form-group">
            <label for="firstname" class="col-sm-3 control-label"><span style="color: red">* </span>账号</label>
            <div class="col-sm-6">
                <input type="text" class="form-control" id="firstname" name="username"/>
                <span class="help-inline" style="color: #808080;font-size: 12px">6~18个字符,可使用字母、数字、下划线,需以字母开头</span>
            </div>
            <div class="col-sm-5">
            </div>
        </div>
        <br/>
        <div class="form-group">
            <label for="lastname" class="col-sm-3 control-label"><span style="color: red">* </span>密码</label>
            <div class="col-sm-6">
                <input type="text" class="form-control" id="lastname" name="password"/>
                <span class="help-inline" style="color: #808080;font-size: 12px">6~16个字符,区分大小写</span>
            </div>
            <div class="col-sm-5">
            </div>
        </div>
        <br/>
        <div class="form-group">
            <div class="col-sm-offset-3 col-sm-10">
                <div class="checkbox">
                    <label>
                        <a href=""></a>
                        <input type="checkbox">同意<a href="#">服务条款</a>和<a href="#">隐私权相关政策</a>
                    </label>
                </div>
            </div>
        </div>
        <div class="form-group">
            <div class="col-sm-offset-3 col-sm-10">
                <button type="submit" class="btn btn-success"><strong> 立即登录 </strong></button>
            </div>
        </div>
    </form>
</div>
<%--</div>--%>
</body>
</html>

2.servlet

package servlet;

import bean.Admin;
import service.LoginService;

import javax.security.auth.login.LoginException;
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("/LoginServlet")
public class LoginServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

//        1.获取登录名和密码
        String username = request.getParameter("username");
        String password = request.getParameter("password");

        System.out.println("输出账户名和密码");
        System.out.println(username);
        System.out.println(password);


            LoginService lservice = new LoginService();
            Admin admin = null;
            try {
                admin = lservice.login(username,password);
                System.out.println(admin.getId());
                response.sendRedirect(request.getContextPath() + "/succeed.jsp");
                return;

            } catch (LoginException e) {
                e.printStackTrace();
                request.setAttribute("login_message",e.getMessage());
                response.sendRedirect(request.getContextPath() + "/error.jsp");
            }


    }

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

3.service

package service;

import bean.Admin;
import dao.LoginDao;
import javax.security.auth.login.LoginException;
import java.sql.SQLException;

//登录操作
public class LoginService {
    public Admin login(String username,String password) throws LoginException {

        try {
            LoginDao dao = new LoginDao();
            Admin admin = dao.findAdminByUsernameAndPassword(username, password);
            if(admin!=null){
                return admin;
            }
            throw new LoginException("用户名或密码错误");
        } catch (SQLException e) {
            e.printStackTrace();
            throw new LoginException("登录失败");
        }

    }
}

4.dao

package dao;

import bean.Admin;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import util.DataSourceUtils;

import java.sql.SQLException;

public class LoginDao {
    public Admin findAdminByUsernameAndPassword(String username,String password) throws SQLException {
        String sql = "select * from admin where name=? and password = ?";
        QueryRunner qrunner = new QueryRunner(DataSourceUtils.getDataSource());

        return qrunner.query(sql,new BeanHandler<Admin>(Admin.class),username,password);

    }
}

5.bean

package bean;

public class Admin {
    int id;
    String name;
    String password;

    public int getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPassword() {
        return password;
    }

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

6.工具类连接数据库

package util;
import com.mchange.v2.c3p0.ComboPooledDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
/**
 * 数据源工具
 */
public class DataSourceUtils {
	private static DataSource dataSource = new ComboPooledDataSource();
	private static ThreadLocal<Connection> tl = new ThreadLocal<Connection>();

	public static DataSource getDataSource() {
		return dataSource;
	}
	/**
	 * 当DBUtils需要手动控制事务时,调用该方法获得一个连接
	 * @return
	 * @throws SQLException
	 */
	public static Connection getConnection() throws SQLException {
		Connection con = tl.get();
		if (con == null) {
			con = dataSource.getConnection();
			tl.set(con);
		}
		return con;
	}
	/**
	 * 开启事务
	 * @throws SQLException
	 */
	public static void startTransaction() throws SQLException {
		Connection con = getConnection();
		if (con != null)
			con.setAutoCommit(false);
	}
	/**
	 * 从ThreadLocal中释放并且关闭Connection,并结束事务
	 * @throws SQLException
	 */
	public static void releaseAndCloseConnection() throws SQLException {
		Connection con = getConnection();
		if (con != null) {
			con.commit();
			tl.remove();
			con.close();
		}
	}
	/**
	 * 事务回滚
	 * @throws SQLException 
	 */
	public static void rollback() throws SQLException {
		Connection con = getConnection();
		if (con != null) {
			con.rollback();
		}
	}
}

7.配置文件

<?xml version="1.0" encoding="UTF-8"?>
<c3p0-config>
	<default-config>
		<property name="driverClass">com.mysql.jdbc.Driver</property>
		<property name="jdbcUrl">
     		jdbc:mysql://localhost:3306/test
     	</property>
		<property name="user">root</property>
		<property name="password">123</property>
		<property name="checkoutTimeout">30000</property>
		<property name="initialPoolSize">10</property>
		<property name="maxIdleTime">30</property>
		<property name="maxPoolSize">100</property>
		<property name="minPoolSize">10</property>
		<property name="maxStatements">200</property>
	</default-config>
</c3p0-config>

8.界面

9.数据库

评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值