版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
要求
tomcat8.0、mysql
需要一个mysql的驱动器,放在WEB-INFO的lib。版本号如下:
数据库文件
在mysql数据库中建立一个名字叫db_01的数据库。在数据库的建立一个名字叫tb_user的表,表格的字段为UName,Pwd。
index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP</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">欢迎JSP</font><hr>
<div>
<img alt="" width = "600" height = "400" src="D:\Images.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('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 = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP</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 = request.getParameter("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="返 回"
"window.location.href('/webText')">
</td>
</tr>
</table>
</form>
</center>
</body>
</html>
checkRegister.jsp
<%@ page language="java" import="java.util.*,java.sql.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP</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 String(request.getParameter("username").getBytes("ISO-8859-1"),"UTF-8");
String pwd = request.getParameter("password");
String driverClass = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/db_01";
String username = "root";
String password = "root";
Class.forName(driverClass);//加载驱动
Connection conn = DriverManager.getConnection(url,username,password);//得到连接
PreparedStatement pStmt = conn.prepareStatement("select * from tb_user where UName = '" + user + "'");
ResultSet rs = pStmt.executeQuery();
if(rs.next()){
out.println("<script language='javascript'>alert('该用户已存在,请重新注册!');window.location.href='register.jsp';</script>");
}else{
PreparedStatement tmt = conn.prepareStatement("Insert into tb_user values('" + user + "','" + pwd + "')");
int rst = tmt.executeUpdate();
if (rst != 0){
out.println("<script language='javascript'>alert('用户注册成功!');window.location.href='index.jsp';</script>");
}else{
out.println("<script language='javascript'>alert('用户注册失败!');window.location.href='register.jsp';</script>");
}
}
%>
</body>
</html>
loginCh.jsp
<%@ page language="java" import="java.util.*,java.sql.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP</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 String(request.getParameter("username").getBytes("ISO-8859-1"),"UTF-8");
String pwd = request.getParameter("password");
String driverClass = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/db_01";
String username = "root";
String password = "root";
Class.forName(driverClass);//加载驱动
Connection conn = DriverManager.getConnection(url,username,password);//得到连接
PreparedStatement pStmt = conn.prepareStatement("select * from tb_user where UName = '" + user + "'");
ResultSet rs = pStmt.executeQuery();
if(rs.next()){
out.println("<script language='javascript'>alert('该用户已存在,请重新注册!');window.location.href='register.jsp';</script>");
}else{
PreparedStatement tmt = conn.prepareStatement("Insert into tb_user values('" + user + "','" + pwd + "')");
int rst = tmt.executeUpdate();
if (rst != 0){
out.println("<script language='javascript'>alert('用户注册成功!');window.location.href='index.jsp';</script>");
}else{
out.println("<script language='javascript'>alert('用户注册失败!');window.location.href='register.jsp';</script>");
}
}
%>
</body>
</html>
register.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP</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 username=document.getElementById("username").value;
var password=document.getElementById("password").value;
var newword=document.getElementById("newword").value;
if(username==""){
alert("用户名不能为空!");
document.getElementById("username").focus();
return false;
}
if(password==""){
alert("密码不能为空!");
document.getElementById("password").focus();
return false;
}
if(password != newword){
alert("两次输入密码不相同!");
document.getElementById("newword").focus();
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>
success.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+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 String(request.getParameter("username").getBytes("8859_1"));
out.println("欢迎你:" + name);
%><br>
<a href="login.jsp">重新登陆</a>
</center>
</body>
</html>