用JSP做一个登入页面

来源:百度知道 编辑:UC知道 时间:2024/09/21 05:49:54
<%@ page language="java" import="java.util.*" pageEncoding="gb2312" %>;
<%@ page import = "java.sql.*" %>;
<html>
<head>
<title>表单输入验证示例</title>
<script type="text/javascript">
function validate()
{
var userName=document.forms[0].userName.value;
var password=document.forms[0].password.value;

if(userName.length<=0)
alert("用户名不能为空!");
else if(password<=0)
alert("密码不能为空!");

else if(userName.length<6)
alert("用户名不能小于6位!");

else
{
alert("验证通过,表单可以提交!");
document.forms[0].submit();
}
}
</script&

<%
String name=request.getParameter("userName");
String pwd=request.getParameter("password");
try{
Connection conn = null;
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();

String url= "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=dengru";
//数据库用户名
String user = "sa";
//数据库密码
String password = "123456";
//根据数据库参数取得一个数据库连接
conn = DriverManager.getConnection(url, user, password);
Statement stmt=conn.createStatement
rs = stmt.executeQuery(sql);
string sql="select* form user where useName="'+name+'" and password="'+pwd+'" ;
ResultSet re=stmt.executeQuery(sql);
if(re.next())
{
response.sendRedirect("要跳转的页面 自己写吧");
}
else {
response.sendRedirect("登陆失败 也自己写吧");
}
con.close();
}cat