1.安装MySQL数据库,建立用户表 uid uname upwd 并插入3条数据
2.制作jsp登录页面 login.jsp 提交到dologin.jsp,使用jdbc连数据库,判断输入的用户名密码是否存在
3.如果存在,把用户名保存在SESSION中,跳转到welcome.jsp,welcome.jsp中读取session中的用户名,显示欢迎你xxx
4.若不存在,跳到登录页面。
login.jsp
1 <%@ page language="java" import="java.util.*" contentType="text/html;charset=utf-8"%> 2 <html> 3 <head> 4 <title> 5 用户登录 6 </title> 7 </head> 8 <body bgcolor="#e3e3e3"> 9 <center> 10 <form action="check.jsp" method="post"> 11 <table> 12 <caption>用户登录</caption> 13 <tr><td>用户名:</td><td><input type="text" name="username" size="20"/></td></tr> 14 <tr><td>密码:</td><td><input type="text" name="pwd" size="20"/></td></tr> 15 <tr><td><input type="submit" value="登录"/><td><input type="reset" value="重置"/> 16 </table> 17 </form> 18 如果您还没有注册,请单击<a href="register.jsp">这里</a>注册! 19 </body> 20 </center> 21 </html>
register.jsp
1 <%@ page language="java" import="java.util.*" contentType="text/html;charset=utf-8"%> 2 <% 3 String path = request.getContextPath(); 4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 5 %> 6 7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 8 <html> 9 <head> 10 <base href="<%=basePath%>"> 11 12 <title>注册页面</title> 13 14 <meta http-equiv="pragma" content="no-cache"> 15 <meta http-equiv="cache-control" content="no-cache"> 16 <meta http-equiv="expires" content="0"> 17 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> 18 <meta http-equiv="description" content="This is my page"> 19 <!-- 20 <link rel="stylesheet" type="text/css" href="styles.css"> 21 --> 22 <script language="javascript"> 23 function isValid(form) 24 { 25 if (form.username.value=="") 26 { 27 alert("用户名不能为空"); 28 return false; 29 } 30 if (form.pwd.value!=form.pwd2.value) 31 { 32 alert("两次输入的密码不同!"); 33 return false; 34 } 35 else if (form.pwd.value=="") 36 { 37 alert("用户密码不能为空!"); 38 return false; 39 } 40 else return true; 41 } 42 </script> 43 </head> 44 45 <body> 46 <center> 47 <body bgcolor="#e3e3e3"> 48 <h2>用户注册</h2> 49 <form action="check2.jsp" method="post" onSubmit="return isValid(this);"> 50 <table> 51 <tr><td>用户名:</td><td><input type="text" name="username" size="20"/></td></tr> 52 <tr><td>输入密码:</td><td><input type="text" name="pwd" size="20"/></td></tr> 53 <tr><td>再次确认密码:</td><td><input type="text"name="pwd2" size="20"/></td><tr> 54 <tr><td><input type="submit" value="注册"/><td><input type="reset" value="重置"/> 55 </table> 56 </form> 57 </center> 58 <br> 59 </body> 60 </html>
check.jsp(判断用户登录)
1 <%@ page language="java" import="java.sql.*" contentType="text/html;charset=utf-8"%> 2 <% 3 String path = request.getContextPath(); 4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 5 %> 6 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 7 <html> 8 <head> 9 <base href="<%=basePath%>"> 10 <title>My JSP 'check.jsp' starting page</title> 11 <meta http-equiv="pragma" content="no-cache"> 12 <meta http-equiv="cache-control" content="no-cache"> 13 <meta http-equiv="expires" content="0"> 14 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> 15 <meta http-equiv="description" content="This is my page"> 16 <!-- 17 <link rel="stylesheet" type="text/css" href="styles.css"> 18 --> 19 </head> 20 <body> 21 <% 22 request.setCharacterEncoding("utf-8"); 23 String users=request.getParameter("username"); 24 String pass=request.getParameter("pwd"); 25 boolean flag=false; 26 PreparedStatement sql=null; 27 ResultSet rs=null; 28 Connection conn=null; 29 %> 30 31 <% 32 String driver = "com.mysql.jdbc.Driver"; 33 String url = "jdbc:mysql://127.0.0.1:3307/login"; 34 String use = "root"; 35 String password = "960404"; 36 Class.forName(driver); 37 conn= DriverManager.getConnection(url,use,password); 38 sql =conn.prepareStatement("select * from student where username=? and password=?"); 39 sql.setString(1,users); 40 sql.setString(2,pass); 41 rs=sql.executeQuery(); 42 if (rs.next()) { 43 flag=true; 44 } 45 rs.close(); 46 sql.close(); 47 conn.close(); 48 %> 49 <!-- 判断是否是正确的登录用户 --> 50 <% if (flag==true){ %> 51 <jsp:forward page="show.jsp"/> 52 <%} 53 else 54 if (flag==false){ 55 %> 56 <jsp:forward page="login_fali.jsp"/> 57 <%} %> 58 </body> 59 </html>
check2.jsp(判断用户注册)
1 <%@ page language="java" import="java.sql.*" contentType="text/html;charset=utf-8"%> 2 <% 3 String path = request.getContextPath(); 4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 5 %> 6 7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 8 <html> 9 <head> 10 <base href="<%=basePath%>"> 11 12 <title>检验注册页面</title> 13 <meta http-equiv="pragma" content="no-cache"> 14 <meta http-equiv="cache-control" content="no-cache"> 15 <meta http-equiv="expires" content="0"> 16 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> 17 <meta http-equiv="description" content="This is my page"> 18 <!-- 19 <link rel="stylesheet" type="text/css" href="styles.css"> 20 --> 21 22 </head> 23 24 <body> 25 <br> 26 <% 27 request.setCharacterEncoding("utf-8"); 28 String users=request.getParameter("username"); 29 String pass=request.getParameter("pwd"); 30 %> 31 <% 32 String driver = "com.mysql.jdbc.Driver"; 33 String url = "jdbc:mysql://127.0.0.1:3307/login"; 34 String use = "root"; 35 String password = "960404"; 36 Class.forName(driver); 37 Connection conn= DriverManager.getConnection(url,use,password); 38 PreparedStatement sql =conn.prepareStatement("insert into student(username,password)values(?,?)"); 39 sql.setString(1,users); 40 sql.setString(2,pass); 41 int rtn=sql.executeUpdate(); 42 sql.close(); 43 conn.close(); 44 %> 45 46 </body> 47 </html>
show.jsp(登录成功)
1 <%@ page language="java" import="java.util.*" contentType="text/html;charset=utf-8"%> 2 <% 3 String path = request.getContextPath(); 4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 5 %> 6 7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 8 <html> 9 <head> 10 <base href="<%=basePath%>"> 11 <title>登录成功</title> 12 <meta http-equiv="pragma" content="no-cache"> 13 <meta http-equiv="cache-control" content="no-cache"> 14 <meta http-equiv="expires" content="0"> 15 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> 16 <meta http-equiv="description" content="This is my page"> 17 <!-- 18 <link rel="stylesheet" type="text/css" href="styles.css"> 19 --> 20 21 </head> 22 23 <body> 24 登录成功. <br> 25 </body> 26 </html>
login_fail.jsp(注意还是要跳转回原来的登录页面的)
1 <%@ page language="java" import="java.sql.*" contentType="text/html;charset=utf-8"%> 2 <% 3 String path = request.getContextPath(); 4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 5 %> 6 7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 8 <html> 9 <head> 10 <base href="<%=basePath%>"> 11 12 <title>登录失败</title> 13 14 <meta http-equiv="pragma" content="no-cache"> 15 <meta http-equiv="cache-control" content="no-cache"> 16 <meta http-equiv="expires" content="0"> 17 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> 18 <meta http-equiv="description" content="This is my page"> 19 <!-- 20 <link rel="stylesheet" type="text/css" href="styles.css"> 21 --> 22 23 </head> 24 25 <body> 26 <% out.println("登录失败");%> 27 <% response.setHeader("refresh","5;url=login.jsp");%> 28 </body> 29 </html>