login.html
<!-- DROP TABLE IF EXISTS `xxx`;
CREATE TABLE `xxx` (
`zusername` char(11) NOT NULL,
`zpassword` char(11) NOT NULL,
PRIMARY KEY (`zusername`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8; -->
<html>
<head>
<title>用户登录</title>
<script type="text/javascript">
function check() {
if (form1.Username.value == "") {
alert("请输入用户名!"); form1.Username.focus; return false;
}
if (form1.Password.value == "") {
alert("请输入密码!"); form1.Password.focus; return false;
}
}
</script>
</head>
<body>
<form method="POST" action="ll.jsp" name="form1" οnsubmit="return check()">
<table width="30%" align="center" >
<tr>
<th colspan="2" >
用户登录
</th>
</tr>
<tr bgcolor="#FFFFFF">
<td>
用户名:
</td>
<td>
<input type="text" name="Username" size="18">
</td>
</tr>
<tr bgcolor="#FFFFFF">
<td>
密码:
</td>
<td>
<input type="password" name="Password" size="20">
</td>
</tr>
<tr bgcolor="#FFFFFF">
<td>
</td>
<td>
<input type="submit" value="提交" name="ok">
<a href="sign.html" >注册</a>
</td>
</tr>
</table>
</form>
</body>
</html>
ll.jsp
<%@ page contentType="text/html;charset=GB2312" language="java" import="java.sql.*" %>
<%
Class.forName("com.mysql.jdbc.Driver");
String strConn="jdbc:mysql://localhost:3306/zxl";
String strUser="root";
String strPassword="2009";
Connection conn=DriverManager.getConnection(strConn,strUser,strPassword);
String strName=request.getParameter("Username");
String strPass=request.getParameter("Password");
if(strName!=null && strPass!=null && request.getParameter("ok").equals("提交") ) {
Statement stat=conn.createStatement();
String strSql="SELECT COUNT(*) FROM xxx WHERE zusername='"+strName+"' and zpassword='"+strPass+"'";
ResultSet result=stat.executeQuery(strSql);
result.next();
if(result.getInt(1)==1) {
session.setAttribute("login","OK");
response.sendRedirect("welcome.jsp");
}else{
System.out.println("on login");
out.println("<h2>login error,please enter username & passworld</h2>");
}
}else{
try{
PreparedStatement zp=conn.prepareStatement("insert into xxx values(?,?)");
zp.setString(1, strName);
zp.setString(2, strPass);
zp.execute();
session.setAttribute("sign","OK");
response.sendRedirect("welcome.jsp");
}catch(Exception e){
System.out.println("on sign");
out.println("<h2>login error,please enter username & passworld</h2>");
}
}
%>
sign.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form method="POST" action="ll.jsp" name="form1" οnsubmit="return check()">
<table width="30%" align="center" >
<tr>
<th colspan="2" >
用户注册
</th>
</tr>
<tr bgcolor="#FFFFFF">
<td>
用户名:
</td>
<td>
<input type="text" name="Username" size="18">
</td>
</tr>
<tr bgcolor="#FFFFFF">
<td>
密码:
</td>
<td>
<input type="password" name="Password" size="20">
</td>
</tr>
<tr bgcolor="#FFFFFF">
<td>
</td>
<td>
<input type="submit" value="注册" name="ok">
</td>
</tr>
</table>
</form>
</body>
</html>
welcome.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>
welcome1
</body>
</html>