简单的JSP登录程序
HTML登录界面
<html>
<head>
<title>
登录
</title>
</head>
<body bgcolor="#ffffff">
<form action="chkLogin.jsp" method="POST">
<input type="text" name="user" /><br />
<input type="password" name="pass" /><br />
<input type="Submit" value="提交" />
</form>
</body>
</html>
<head>
<title>
登录
</title>
</head>
<body bgcolor="#ffffff">
<form action="chkLogin.jsp" method="POST">
<input type="text" name="user" /><br />
<input type="password" name="pass" /><br />
<input type="Submit" value="提交" />
</form>
</body>
</html>
检查登录
<%@ page contentType="text/html; charset=GBK" %>
<%@ page import="java.sql.*" %>
<html>
<head>
<title>
chkLogin
</title>
</head>
<body bgcolor="#ffffff">
<%
String user = request.getParameter("user");
String pass = request.getParameter("pass");
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
try
{
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
con = DriverManager.getConnection("jdbc:microsoft:sqlserver://localhost:1433;databasename=Test", "sa", "");
ps = con.prepareStatement("SELECT * FROM [User] WHERE UserName=? AND UserPwd =?");
ps.setString(1, user);
ps.setString(2, pass);
rs = ps.executeQuery();
if ( rs.next())
{
out.print("登录成功!");
}
else
{
out.print("登录失败!");
}
}catch(Exception ex)
{
ex.printStackTrace();
}finally{
if ( null != rs)
{
rs.close();
rs = null;
}
if ( null != ps)
{
ps.close();
ps = null;
}
if ( null != con){
con.close();
con = null;
}
}
%>
</body>
</html>
<%@ page import="java.sql.*" %>
<html>
<head>
<title>
chkLogin
</title>
</head>
<body bgcolor="#ffffff">
<%
String user = request.getParameter("user");
String pass = request.getParameter("pass");
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
try
{
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
con = DriverManager.getConnection("jdbc:microsoft:sqlserver://localhost:1433;databasename=Test", "sa", "");
ps = con.prepareStatement("SELECT * FROM [User] WHERE UserName=? AND UserPwd =?");
ps.setString(1, user);
ps.setString(2, pass);
rs = ps.executeQuery();
if ( rs.next())
{
out.print("登录成功!");
}
else
{
out.print("登录失败!");
}
}catch(Exception ex)
{
ex.printStackTrace();
}finally{
if ( null != rs)
{
rs.close();
rs = null;
}
if ( null != ps)
{
ps.close();
ps = null;
}
if ( null != con){
con.close();
con = null;
}
}
%>
</body>
</html>