1.login.html
<html>
<head><title>www.mldn.cn</title></head>
<body>
<center>
<h1>登录操作</h1>
<form action="login_check.jsp" method="post">
<table border="1">
<tr>
<td colspan="2">用户登录</td>
</tr>
<tr>
<td>登录ID:</td>
<td><input type="text" name="id"></td>
</tr>
<tr>
<td>登录密码:</td>
<td><input type="password" name="password"></td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="登录">
<input type="reset" value="重置">
</td>
</form>
</center>
</body>
</html>
-------------------------------------------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------------------------------------------------------------------
2.login_check.jsp
<%@page import ="java.sql.*"%>
<html>
<head><title>www.mldn.cn</title></head>
<body>
<%!
public static final String dBDriver = "com.mysql.jdbc.Driver"; //数据库驱动对象
public static final String url = "jdbc:mysql://localhost:3306/db_userinfo"; //数据库驱动+IP地址+端口号+数据库名子
public static final String user = "root";//用户名
public static final String password = "root";//密码
%>
<%
Connection conn = null;//连接对象
PreparedStatement pst = null;//
ResultSet rs = null;
boolean flag=false;
String name=null;
%>
<%
try{
Class.forName(dBDriver); //加载驱动
String sql="select name from user where userid=? and password=?";
conn = DriverManager.getConnection(url, user, password); //建立连接对象
pst = conn.prepareStatement(sql);
pst.setString(1,request.getParameter("id"));
pst.setString(2,request.getParameter("password"));
rs = pst.executeQuery();
if(rs.next()){
name=rs.getString(1);
flag=true;
}
}catch(Exception e)
{
System.out.println(e);
}
finally
{
try{
rs.close();
pst.close();
conn.close();
}catch(Exception e){}
}
%>
<%
if(flag){
%>
<jsp:forward page="login_success.jsp">
<jsp:param name="uname" value="<%=name%>"/>
</jsp:forward>
<%
}else{
%>
<jsp:forward page="login_failure.html"/>
<%
}
%>
</body>
</html>
<html>
<head><title>www.mldn.cn</title></head>
<body>
<%!
public static final String dBDriver = "com.mysql.jdbc.Driver"; //数据库驱动对象
public static final String url = "jdbc:mysql://localhost:3306/db_userinfo"; //数据库驱动+IP地址+端口号+数据库名子
public static final String user = "root";//用户名
public static final String password = "root";//密码
%>
<%
Connection conn = null;//连接对象
PreparedStatement pst = null;//
ResultSet rs = null;
boolean flag=false;
String name=null;
%>
<%
try{
Class.forName(dBDriver); //加载驱动
String sql="select name from user where userid=? and password=?";
conn = DriverManager.getConnection(url, user, password); //建立连接对象
pst = conn.prepareStatement(sql);
pst.setString(1,request.getParameter("id"));
pst.setString(2,request.getParameter("password"));
rs = pst.executeQuery();
if(rs.next()){
name=rs.getString(1);
flag=true;
}
}catch(Exception e)
{
System.out.println(e);
}
finally
{
try{
rs.close();
pst.close();
conn.close();
}catch(Exception e){}
}
%>
<%
if(flag){
%>
<jsp:forward page="login_success.jsp">
<jsp:param name="uname" value="<%=name%>"/>
</jsp:forward>
<%
}else{
%>
<jsp:forward page="login_failure.html"/>
<%
}
%>
</body>
</html>
-------------------------------------------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------------------------------------------------------------------
3.login_success.jsp
<html>
<head><title>www.mldn.cn</title></head>
<body>
<center>
<h1>登录操作</h1><br>
<h2>登录成功</h2>
<h2>欢迎<font color="red"><%=request.getParameter("uname")%></font>光临!</h2>
</center>
</body>
</html>
-------------------------------------------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------------------------------------------------------------------
4.login_failure.html
<html>
<head><title>www.mldn.cn</title></head>
<body>
<center>
<h1>登录操作</h1><br>
<h2>登录失败,请重新<a href="login.html">登录</a>!</h2>
</center>
</body>
</html>