本地数据库名: test_mysql
用户表名: userinfo
表的结构:
username--varchar(10)
password--varchar(20)
<em>注:编码均为UTF-8 。如果要在你的机子上运行以下代码,请将你对应的数据库名、登陆数据库的用户名、密码及表作出相应的修改</em>
index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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">
<link rel="stylesheet" href="http://apps.bdimg.com/libs/bootstrap/3.3.0/css/bootstrap.min.css">
<script class="lazyload" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB/AAffA0nNPuCLAAAAAElFTkSuQmCC" data-original="http://apps.bdimg.com/libs/jquery/2.1.1/jquery.min.js"></script>
<script class="lazyload" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB/AAffA0nNPuCLAAAAAElFTkSuQmCC" data-original="http://apps.bdimg.com/libs/bootstrap/3.3.0/js/bootstrap.min.js"></script>
<style>
form{
margin-top:20px;
}
input{
margin: 0 10px 0 10px;
}
</style>
<title>请先登录</title>
</head>
<body>
<form class="form-inline text-center" role="form" action="doLogin.jsp" method="post">
<div class="form-group">
<label>用户名:<input type="text" class="form-control" name="username"></label>
</div>
<div class="form-group">
<label>密码<input type="password" class="form-control" name="password"></label>
</div>
<div class="form-group">
<label><input type="submit" class="form-control btn-primary" value="提交"></label>
</div>
</form>
</body>
</html>
</body>
</html>
doLogin.jsp
<%@page import="java.sql.*"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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>
<%!
String DBDriver = "com.mysql.jdbc.Driver";
String DBUrl = "jdbc:mysql://localhost:3306/test_mysql?useUnicode=true&useSSL=false";
String DBUser = "root";
String DBPass = "1589";
boolean getPermit = false;
%>
<%
String username = request.getParameter("username");
String password = request.getParameter("password");
Connection con = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
// 记得反射先
Class.forName(DBDriver);
con = DriverManager.getConnection(DBUrl, DBUser, DBPass);
String sql = "";
pstmt = con.prepareStatement("select * from userinfo where username=?");
pstmt.setString(1, username);
rs = pstmt.executeQuery();
// 说明找到记录
if(rs.next()) {
if (rs.getString(2).equals(password)) {
getPermit = true;
}
}
out.println(rs.getString(2));
} catch (SQLException e) {
System.out.println("连接数据库失败");
e.printStackTrace();
} finally {
rs.close();
pstmt.close();
con.close();
}
%>
<%
if(getPermit) {
request.getRequestDispatcher("success.jsp").forward(request, response);
} else {
request.getRequestDispatcher("failure.jsp").forward(request, response);
}
%>
</body>
</html>
failure.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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>
<center>
<h2>登录失败<a href="index.jsp">返回</a>重新登录</h2>
</center>
</body>
</html>
success.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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>
<center>
<h2>欢迎用户<%=request.getParameter("username") %>,登陆成功</h2>
</center>
</body>
</html>