No. | 页面名称 | 描述 |
1 | login.jsp | 提供用户的登陆表单,可以输入用户id和密码。 |
2 | check.jsp | 登陆检查页,根据表单提交过来的id和密码进行数据库验证,成功跳转到登陆成功页,否则跳转到登陆失败页。 |
3 | success.jsp | 登陆成功页,显示欢迎信息。 |
4 | failure.jsp | 登陆失败页,提示用户输入错误,并提供重新登陆的超链接。 |
1、login.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="check.jsp" method="post">
<input type="text" name="uName">
<input type="text" name="uPwd">
<input type="submit" value="提交">
</form>
</body>
</html>
2、check.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.sql.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
String driverName="org.gjt.mm.mysql.Driver";
String userName="root";
String userPasswd="123456";
String dbName="mldn";
String url="jdbc:mysql://localhost/"+dbName+"?user="+userName+"&password="+userPasswd;
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection conn=DriverManager.getConnection(url);
PreparedStatement ps = null;
ResultSet re = null;
%>
<%
String uName=request.getParameter("uName");
String uPwd=request.getParameter("uPwd");
String sql = "select * from vUser where uName=? AND uPwd=?";
ps = conn.prepareStatement(sql);
ps.setString(1, uName);
ps.setString(2, uPwd);
re = ps.executeQuery();
if(re.next()){
%>
<jsp:forward page="success.jsp">
<jsp:param value="<%=uName %>" name="uName"/>
</jsp:forward>
<%} else{%>
<jsp:forward page="fail.jsp"></jsp:forward>
<%} %>
</body>
</html>
3、success.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>登陆成功</h1>
<%
String uName=request.getParameter("uName");
%>
<h1>欢迎<%=uName %>登录</h1>
</body>
</html>
4、fail.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>登录失败</h1>
<a href="index.jsp">请重新登录</a>
</body>
</html>