Jsp登陆页面和前后台验证并连接数据库
http://www.veryhuo.com 2009-01-05 ZDWZZU专栏 投递稿件 我有话说
烈火建站学院(LieHuo.Net)JSP文档 前台login.html和后台verifylogin.jsp两个页面组成:
login.html内容:
以下为引用的内容:
<html>
<head>
<title>登录</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<meta http-equiv="Content-Language" content="ch-cn">
</head>
<body>
<!-- Form 用来提取用户填入并提交的信息-->
<form method="post" name="frmLogin" action="verifylogin.jsp">
<h1 align="center">用户登录</h1><br>
<div align="center">用户名:
<input type="text" name="txtUserName" value="Your name"
οnfοcus="if(this.value=='Your name')this.value='';"><br>密码:
<input type="password" name="txtPassword" value="Your password"
οnfοcus="if(this.value=='Your password')this.value='';"><br>
<input type="submit" name="Submit" value="提交">
<input type="reset" name="Reset" value="重置"><br>
</div></form></body>
</html>
verifylogin.jsp内容:
<%@ page language="java" contentType="text/html;charset=gb2312"
pageEncoding="UTF-8"%>
<%@ page import="java.sql.*"%>
<%@ page import="java.util.*"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>登录</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<div align=center>
<%
//获取用户名
String sUserName = request.getParameter ( "txtUserName" );
//获取密码
String sPasswd = request.getParameter ( "txtPassword" );
//登记JDBC驱动程序
Class.forName ( "org.gjt.mm.mysql.Driver" ).newInstance ( );
//连接参数与Access不同
String url = "jdbc:mysql://localhost/LearnJSP";
//建立连接
Connection connection = DriverManager.getConnection ( url, "root",
"011124" );
//SQL语句
String sql = "select * from userinfo where username='" + sUserName
+ "' and userpwd = '" + sPasswd + "'";
Statement stmt = connection.createStatement ( );
ResultSet rs = stmt.executeQuery ( sql ); //返回查询结果
//如果记录集非空,表明有匹配的用户名和密码,登陆成功
if ( rs.next ( ) )
{
out.println ( "登录成功!" );
} else
//否则登录失败
{
out.println ( "用户名不存在或密码错误!" );
}
rs.close ( );
stmt.close ( );
connection.close ( );
%>
</body>
</html>
文章源自:烈火网,原文:http://www.veryhuo.com/a/view/4173.html