用JSP页面和JDBC实现用户的登陆功能
工具:IDEA或者eclipse都可以,然后是MySQL数据库,图形化界面是Navicat for MySQL
用到的知识:JSP,JDBC
本来是要用JSP+JDBC+SERVLET来写的,然后发现servlet学的不是很好,就先用这个来试一试
一:首先来再数据库里建立一个用户表,我这里取名为p_user
主键什么的就先暂时没有设置,先以实现功能为主
CREATE TABLE `p_user` (
`id` int(11) DEFAULT NULL,
`username` varchar(10) DEFAULT NULL,
`password` varchar(10) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
然后再插入数据,插入数据的语句就不写了,这里加入了一个用户名为asd,密码为123的用户
二、先写一个简单的登陆界面login.jsp
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>User Login Page</title>
</head>
<body>
<hr><br>Welcome to this <font color="green">Login Page</font>!<br>
<form action="do_login.jsp" method="get">
<br>
<h1>Please input your message:</h1><br>
用户名:<input type="text" name="name"><br>
密码: <input type="password" name="password"><br>
<br><br><br>
<input type="submit" value="登陆"> <input type="reset"><br>
</form>
</body>
</html>
这个是登陆界面的效果
三、登陆成功后的界面login_success.jsp
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>成功登陆</title>
</head>
<body>
<hr><br>
<h1>欢迎登陆</h1><br>
<font color="green">Welcome <%=session.getAttribute("UserName") %>!</font>
<h3 align="center">你的个人信息:</h3>
<%
out.println("Name:"+session.getAttribute("UserName"));
%>
<font color="red"><a href="login.jsp">点击我</a> 重新登陆</font>
</body>
</html>
然后这个界面的效果是:
四、登陆失败后的界面login_failed
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>登陆失败!</title>
</head>
<body>
<hr>
<br>
<h1><font color="red">用户名或密码不正确</font></h1><br>
<font color="red"><a href="login.jsp">点击我</a>重新登陆</font>
</body>
</html>
然后这个是效果图:
五、最重要的是连接数据库和判断以及从表单中取值do_login.jsp
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.sql.*" %>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Server Page Depend !</title>
</head>
<body>
<h3>Which Pae will be depend by the user's message!</h3>
<%
String name=request.getParameter("name");//获取name的参数值
String password=request.getParameter("password");//获取password的参数值
%>
<%
Class.forName("com.mysql.jdbc.Driver");//加载mysql驱动
Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "123456");//localhost是本机地址,3306是端口号,最后是用户名和密码
Statement stmt=conn.createStatement();//实例化Statement对象
String queryNumberSQL="SELECT * from p_user where username='"+name+"' and password='"+password+"'";
ResultSet rs=stmt.executeQuery(queryNumberSQL);//执行数据库查询操作并获取结果集
boolean flag=false;//初始化flag
if(rs.next()){//判断结果
flag=true;
session.setAttribute("UserName", name);//将name的内容赋值给UserName
}else{
flag=false;
}
%>
<%
if(flag){
%>
<jsp:forward page="login_success.jsp"></jsp:forward>//跳转页面
<%
}else{
%>
<jsp:forward page="login_failed.jsp"></jsp:forward>//跳转页面
<%
}
//关闭上面的几个对象,注意关闭顺序,最后使用的先关闭
rs.close();
stmt.close();
conn.close();
%>
</body>
</html>
然后就结束了,借鉴了几篇博客,但是后来找不到链接了,侵删!!!