JSP实现登录注册并链接数据库页⾯
在学习了页⾯跳转及部分知识后做了登录注册界⾯,并经过本博主调试bug后完善的更进⼀步,⼤家有什么问题也可以留⾔,本博主以更⼴泛学习讨论为⽬的。
内容介绍:实现页⾯的跳转;注册登录时实现读取数据库,并对数据库实现插⼊(insert)和查询(select)功能。
⼏点注意:sqljdbc.jar包的导⼊和环境变量;数据库的登录读取,可以参考数据库的测试。
登录注册界⾯的代码实现
index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = ContextPath();
String basePath = Scheme()+"://"+ServerName()+":"+ServerPort()+p
ath+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'Feilong_index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<center>
<font face = "宋体" size = "6" color = "#000">欢迎使⽤飞龙科技</font><hr>
<div>
<img alt="" width = "600" height = "400" src="D:\我的图⽚\QImages\⼩⼈团队.jpg">
</div>
<table width = "200" border ="1" bordercolor = "#00F">
<tr>
<td><input type = "button" value = "登陆" onclick = "window.location.href('login.jsp')"></td>
<td><input type = "button" value = "注册" onclick = "window.open('register.jsp')"></td>
</tr>
</table>
</center>
</body>
</html>
login.jsp  //登录界⾯
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = ContextPath();
String basePath = Scheme()+"://"+ServerName()+":"+ServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'Feilong_login.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<body>
<center>
<font face="楷体" size="6" color="#000" >登录界⾯</font>
<%
String flag = Parameter("errNo");
try{
if(flag!=null)
out.println("⽤户名不存在或密码错误");
}catch(Exception e){
e.printStackTrace();
}
%>
<form action = "loginCh.jsp" method="post">
<table width="300" height = "180" border="5" bordercolor="#A0A0A0">
<tr>
<th>账户:</th>
<td><input type="text" name="name"  value = "请输⼊⽤户名" maxlength = "16" onfocus = "if(this.value == '请输⼊⽤户名') this.value =''"></td>      </tr>
<tr>
<th>密码:</th>
<td><input type="password" name="pwd" maxlength = "20"></td>
</tr>
<tr>
<td colspan = "2" align = "center">
<input type="submit" name="submit" value="登录">
<input type="button" value="返回"
οnclick="window.location.href('/webText')">
</td>
</tr>
</table>
</form>
</center>
</body>
</html>
loginCh.jsp  //登录检验
<%@ page language="java" import="java.util.*,java.sql.*,java.*" pageEncoding="utf-8"%>
<%
String path = ContextPath();
String basePath = Scheme()+"://"+ServerName()+":"+ServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'Feilong_loginCh.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<%      //接收⽤户名和密码
String user = new Parameter("name").getBytes("ISO-8859-1"),"UTF-8");
String pwd = Parameter("pwd");
String driverClass = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
String url = "jdbc:sqlserver://localhost:1433; DatabaseName = db_01";
String username = "sa";
String password = "123";
Class.forName(driverClass);//加载驱动
Connection conn = Connection(url,username,password);//得到连接
PreparedStatement pStmt = conn.prepareStatement("select * from tb_user where UName = '" + user + "' and Pwd = '" + pwd + "'");              ResultSet rs = uteQuery();
()){
response.sendRedirect("success.jsp?username="+de(user)); //解决乱码
}else{
response.sendRedirect("login.jsp?errNo");//密码不对返回到登陆
}
rs.close();
pStmt.close();
conn.close();
%>
</body>
</html>
success.jsp  //登录成功界⾯
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = ContextPath();
String basePath = Scheme()+"://"+ServerName()+":"+ServerPort()+path+"/"; %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>Feilong_登录成功</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<center>
<%
String name = new Parameter("username").getBytes("8859_1"));
out.println("欢迎你:" + name);
%><br>
<a href="login.jsp">重新登陆</a>
</center>
</body>
</html>
jsp创建
register.jsp  //注册界⾯
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = ContextPath();
String basePath = Scheme()+"://"+ServerName()+":"+ServerPort()+path+"/"; %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'Feilong_register.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<script>
function addCheck(){
var ElementById("username").value;
var ElementById("password").value;
var ElementById("newword").value;
if(username==""){
alert("⽤户名不能为空!");
return false;
}
if(password==""){
alert("密码不能为空!");
alert("密码不能为空!");
return false;
}
if(password != newword){
alert("两次输⼊密码不相同!");
return false;
}
}
function validate(){
var flag = addCheck();
if(flag == false)
return false;
return true;
}
</script>
<body>
<center>
<font face="楷体" size="6" color="#000">注册界⾯</font>
<form action = "checkRegister.jsp" method = "post" onsubmit = "return validate()">
<table width="300" height = "180" border="5" bordercolor="#A0A0A0">
<tr>
<th>⽤户名:</th>
<td><input type="text" name="username" value="输⼊16个字符以内" maxlength = "16" onfocus = "if(this.value == '输⼊16个字符以内') this.value =''"></td>    </tr>
<tr>
<th>输⼊密码:</th>
<td><input type="text" name="password" value="输⼊20个字符以内" maxlength = "20" onfocus = "if(this.value == '输⼊20个字符以内') this.value =''"></td>    </tr>
<tr>
<th>确认密码:</th>
<td><input type="text" name="newword" value="重新输⼊密码" maxlength = "20" onfocus = "if(this.value == '重新输⼊密码') this.value =''"></td>
</tr>
<tr>
<td colspan = "2" align = "center">
<input type="submit" value="注册">
<input type="reset" value="重置">
</td>
</tr>
</table>
</form>
</center>
</body>
</html>
checkRegister.jsp //验证注册⽤户并导⼊数据库

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。