(1)页面显示登录内容:用户名、密码及其输入内容的文区域,以及提交按钮;
(2)设置输入用户名和密码,在数据库中查找, 如找到:”欢迎你, “,若输入错误则页面不跳转;
(3)设置一个注册按钮,点击后进入你设计的注册页面实现注册功能。
login.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>用户登录页面</title>
</head>
<body >
<h2 align="center"><font color=red>用户登录页面</font></h2>
<form action="success.jsp" method="post">
<table align="center" border="1">
<tr>
<td>用户名:</td>
<td><input type="text" name="name"></td>
</tr>
<tr>
<td>密 码:</td>
<td><input type="text" name="password"></td>
</tr>
<tr>
<td><input type="submit" value="登录" name="login"></td>
<td><input type="reset" value="重置" name="reset"></td>
</tr>
</table>
<p align="center"><a href="registered.jsp" color=blue>注册用户</a></p>
</form>
</body>
</html>
registered.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body align="center">
<h2>新用户注册</h2>
<form action="registeredSucceed.jsp" method="post">
<table align="center">
<tr align="right">
<td>请输入用户名:</td>
<td><input type="text" name=name autofocus="autofocus"></td>
</tr>
<tr align="right">
<td>请输入密码:</td>
<td><input type="text" name=password></td>
</tr>
<tr align="right">
<td>请输入确认密码:</td>
<td><input type="text" name=refill></td>
</tr>
</table>
<input type="submit" name=register value="注册" >
<input type="reset" name=refill value="重填" >
</form>
</body>
</html>
registeredSucceed.jsp
<%@page import="java.sql.*"%>
<%@page import="javax.sql.*"%>
<%@page import="javax.naming.*"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%request.setCharacterEncoding("utf-8"); %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>注册成功页面</title>
</head>
<body>
<%
Context ctx = null;
DataSource ds = null;
Statement stmt =null;
ResultSet rs = null;
Connection con = null;
String name=request.getParameter("name").trim();//去除首尾空格
String password=request.getParameter("password").trim();
String refill=request.getParameter("refill").trim();
try{
ctx = new InitialContext();
ds = (DataSource)ctx.lookup("java:comp/env/jdbc/mysql");//mysql是在创建连接池时自己创建的名字
con = ds.getConnection();
stmt = con.createStatement();
if(name!=null ){
rs=stmt.executeQuery("select * from user where name='"+name+"'");
if(rs.next()){
out.print("用户已经存在 "+"请<a href=\"registered.jsp\">注册</a>");
}else{
if(password.equals(refill)){
stmt.executeUpdate("insert into user values('"+name+"','"+ password + "');");
%>
注册成功!!!<br>
三秒钟后自动转到登录页面!!!<br>
如果没有跳转,请点击<a href="login.jsp">这里</a>!!!
<span style="font-size:24px;"><meta http-equiv="refresh" content="3;URL=login.jsp"> </span>
<%
}else{
out.print("密码输入不一致!!!<br>"+"重新<a href=\"registered.jsp\">注册</a>");
}
}
}else {
out.print("姓名不能为空");
}
}catch(Exception e){
out.print(e);
}finally{
if(rs!=null)
rs.close();
if(stmt!=null)
stmt.close();
if(con!=null)
con.close();
}
%>
</body>
</html>
success.jsp
<%@page import="java.sql.*"%>
<%@page import="javax.sql.*"%>
<%@page import="javax.naming.*"%>
<%request.setCharacterEncoding("utf-8"); %>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>判断登录</title>
</head>
<body>
<%
Context ctx = null;
DataSource ds = null;
Statement stmt =null;
ResultSet rs = null;
Connection con = null;
String name = request.getParameter("name").trim();
String password = request.getParameter("password").trim();
try{
ctx = new InitialContext();
ds = (DataSource)ctx.lookup("java:comp/env/jdbc/mysql");//mysql是在创建连接池时自己创建的名字 语句功能找到配置的数据库
con = ds.getConnection();//创建数据库连接
stmt = con.createStatement();
rs=stmt.executeQuery("select * from user where name='"+name+"'");
if(rs.next()){
rs=stmt.executeQuery("select * from user where name='"+name+"' and password='"+password+"'");
if(rs.next()){
out.print(name+"登录成功");
}else{
out.print("密码输入错误!!!<br>"+"重新<a href=\"login.jsp\">登录</a>");
}
}else{
out.print("<font color=red>"+name+"</font>用户不存在!!!<br>"+"请点击<a href=\"registered.jsp\">注册</a>");
}
}catch(Exception e){
out.print(e);
}finally{
if(rs!=null)
rs.close();
if(stmt!=null)
stmt.close();
if(con!=null)
con.close();
}
%>
</body>
</html>
另外还有登录时密码不正确的提示,以及注册时密码输入不一致的判断的功能,也可以去除注册时的空格。可以自己尝试一下这里就不做介绍了