<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
String info = request.getParameter("info");
if(info == null){
System.out.println("yao hehe");
}else if(info.equals("error")){
out.println("<center><h1>用户名或密码错误</h1></center>");
}
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP '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">
-->
</head>
<body topmargin="150">
<center>
<form name="form1" action="loginCheck.jsp" method="post">
<table width="250" border="1">
<tr>
<td colspan="2" align="center">登陆界面</td>
</tr>
<tr>
<td>用户名</td>
<td><input type="text" name="username" size="17"></td>
</tr>
<tr>
<td>密码</td>
<td><input type="password" name="password" size="18"></td>
</tr>
<tr>
<td colspan="2"><input type="submit" name="submit" value="登录"> <a href="register.jsp">注册新用户</a></td>
</tr>
</table>
</form>
</center>
</body>
</html>
loginCheck.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+"/";
String username = request.getParameter("username");
String password = request.getParameter("password");
//这就是在服务器端完成的,javascript也是可以完成这个功能的,不过就是在客户端完成的了,减轻了服务器的压力
if(username == null||password ==null){
response.sendRedirect("login.jsp");
}
boolean flag = false;
Connection conn = null;
Statement st = null;
ResultSet rs = null;
String sql ="select * from users where username = '"+username+"' and password = '"+password+"' ";
try{
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost/login?user=root&password=admin");
if(conn!=null){
System.out.println("链接数据库成功");
}
st = conn.createStatement();
rs = st.executeQuery(sql);
if(rs.next()){
flag = true;
}
}catch(Exception e){
e.printStackTrace();
}finally{
if(rs!=null){
rs.close();
}
if(st!=null){
st.close();
}
if(conn !=null){
conn.close();
}
}
if(flag){
response.sendRedirect("welcome.jsp?user="+username);
}else{
response.sendRedirect("login.jsp?info=error");
}
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'loginCheck.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>
</body>
</html>
welcome.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
String username = request.getParameter("user");
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'welcome.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>
欢迎登陆本网页!<%=username%><br>
</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 '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">
-->
</head>
<body>
<center>
<form name="form1" action="registerCheck.jsp" method="post">
<table width="300" border="1">
<tr>
<td colspan="2" align="center">注册窗口</td>
<tr>
<td>用户名</td>
<td><input type="text" name="username" size="25"></td>
</tr>
<tr>
<td>密码</td>
<td><input type="password" name="password1" size="26"></td>
</tr>
<tr>
<td>确认密码</td>
<td><input type="password" name="password2" size="26"></td>
</tr>
<tr>
<td>Email</td>
<td><input type="text" name="email" size="25"></td>
</tr>
<tr>
<td colspan="2"><input type="submit" name="submit" value="注册"> <a
href="login.jsp">返回</a></td>
</tr>
</table>
</form>
</center>
</body>
</html>
registerCheck.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+"/";
String email = request.getParameter("email");
String username = request.getParameter("username");
String password1 = request.getParameter("password1");
String password2 = request.getParameter("password2");
if(username == null||password1 == null|| password2 == null||!password1.equals(password2)){
response.sendRedirect("register.jsp");
}
boolean flag = false;
String sql = "select * from users where username ='"+username+"'";
Connection conn = null;
Statement st = null;
ResultSet rs = null;
try{
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost/login?user=root&password=admin");
if(conn!=null){
System.out.println("链接数据库成功");
}
st = conn.createStatement();
rs = st.executeQuery(sql);
if(!rs.next()){
sql = "insert into users(username,password) values('"+username+"','"+password1+"')";
//st.execute(sql);
int res = st.executeUpdate(sql);
if(1 == res){
flag = true;
}
}
}catch(Exception e){
e.printStackTrace();
}finally{
if(rs!=null){
rs.close();
}
if(st!=null){
st.close();
}
if(conn !=null){
conn.close();
}
}
if(flag){
System.out.println("flag=true");
response.sendRedirect("login.jsp");
}else{
System.out.println("flag=false");
response.sendRedirect("register.jsp");
}
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'registerCheck.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>
This is my JSP page. <br>
</body>
</html>