資料庫表代碼: /*Navicat MySQL Data Transfer Source Server : localhost_3306Source Server Version : 50528Source Host : localhost:3306Source Database : bbs Tar ...
資料庫表代碼:
/*
Navicat MySQL Data Transfer
Source Server : localhost_3306
Source Server Version : 50528
Source Host : localhost:3306
Source Database : bbs
Target Server Type : MYSQL
Target Server Version : 50528
File Encoding : 65001
Date: 2016-06-04 09:25:01
*/
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for tb_user
-- ----------------------------
DROP TABLE IF EXISTS `tb_user`;
CREATE TABLE `tb_user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(255) NOT NULL,
`password` varchar(255) NOT NULL,
`linkman` varchar(255) DEFAULT NULL,
`company` varchar(255) DEFAULT NULL,
`companytel` varchar(255) DEFAULT NULL,
`qq` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of tb_user
-- ----------------------------
INSERT INTO `tb_user` VALUES ('1', 'admin', 'admin', null, null, null, null);
INSERT INTO `tb_user` VALUES ('2', 'user', 'user', null, null, null, null);
註冊頁面:
<%@ page language="java" import="java.util.*,java.sql.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@ page import="java.sql.*"%>
<%!
public static final String DBDRIVER = "com.mysql.jdbc.Driver" ;
public static final String userName = "root"; //登錄的用戶名
public static final String userPasswd = "taizhen"; //登錄mysql密碼
public static final String dbName = "test"; //資料庫名
public static final String tableName="user"; //表名
public static final String DBURL = "jdbc:mysql://localhost:3306/"+dbName+"?user="+userName+"&password="+userPasswd;
%>
<%
Connection conn = null ;
PreparedStatement pstmt = null ;
ResultSet rs = null ;
boolean flag = false ; // 表示登陸成功或失敗的標記
%>
<%
String action = request.getParameter("action");
if(null==action){
}else{
%>
<%out.println(action); %>
<%
String name = request.getParameter("username") ; // 接收表單參數
String password = request.getParameter("password") ; // 接收表單參數
try{
Class.forName(DBDRIVER) ;
conn = DriverManager.getConnection(DBURL) ;
String sql = "SELECT name,password FROM user WHERE name=? AND password=?" ;
pstmt = conn.prepareStatement(sql) ;
pstmt.setString(1,name) ;
pstmt.setString(2,password) ;
rs = pstmt.executeQuery() ;
while(rs.next()){
// 如果有內容,則此處執行,表示查詢出來,合法用戶
flag = true ;
}
}catch(Exception e){
}finally{
try{
conn.close() ; // 連接一關閉,所有的操作都將關閉
}catch(Exception e){}
}
%>
<%out.println(flag); %>
<%out.println(action); %>
<%
if(flag){
request.getSession().setAttribute("success", "登錄");
%>
<jsp:forward page="success.jsp"/>
<%
}else{ // 登陸失敗,跳轉到failure.jsp
request.getSession().setAttribute("error", "登錄");
%>
<jsp:forward page="error.jsp"/>
<%
}
}
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'login.jsp' starting page</title>
</head>
<body>
<form action="login.jsp" method="post">
<input type="hidden" name="action" />
用戶名:
<input type="text" name="username" value="username"/>
<br>
密碼:
<input type="password" name="password" value="password"/>
<br>
<input type="submit" value="login" />
</form>
</body>
</html>
jsp鏈接資料庫代碼:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@ page import="java.sql.*"%>
<%!
public static final String DBDRIVER = "com.mysql.jdbc.Driver" ;
public static final String userName = "root"; //登錄的用戶名
public static final String userPasswd = "taizhen"; //登錄mysql密碼
public static final String dbName = "test"; //資料庫名
public static final String tableName="user"; //表名
public static final String DBURL = "jdbc:mysql://localhost:3306/"+dbName+"?user="+userName+"&password="+userPasswd;
%>
<%
Connection conn = null ;
PreparedStatement pstmt = null ;
ResultSet rs = null ;
boolean flag = false ; // 表示登陸成功或失敗的標記
%>
<%
String action = request.getParameter("username");
if(action!=null){
%>
<%out.println(request.getParameter("password")); %>
<%
String name = request.getParameter("username") ; // 接收表單參數
String password = request.getParameter("password") ; // 接收表單參數
String linkman=request.getParameter("linkman");
String qq=request.getParameter("qq");
try{
Class.forName(DBDRIVER) ;
conn = DriverManager.getConnection(DBURL) ;
String sql = "INSERT INTO user (name, password) VALUES (?,?)";
pstmt = conn.prepareStatement(sql) ;
pstmt.setString(1,name) ;
pstmt.setString(2,password) ;
int sql_update = pstmt.executeUpdate();
if(sql_update>0){
flag=true;
}
}catch(Exception e){
}finally{
try{
conn.close() ; // 連接一關閉,所有的操作都將關閉
}catch(Exception e){}
}
%>
<%
if(flag){
request.getSession().setAttribute("success", "註冊");
%>
<jsp:forward page="success.jsp"/>
<%
}else{ // 登陸失敗,跳轉到failure.jsp
request.getSession().setAttribute("error", "註冊");
%>
<jsp:forward page="error.jsp"/>
<%
}
}else{}
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'register.jsp' starting page</title>
</head>
<body>
<div class="loginbox registbox">
<div class="login-content reg-content">
<div class="loginbox-title">
<h3>註冊</h3>
</div>
<form id="signupForm" action="register.jsp" method="post">
<div class="row">
<label class="field" for="email">用戶名</label>
<input type="text" class="input-text-user noPic input-click" name="username" id="email">
</div>
<div class="row">
<label class="field" for="password">密碼</label>
<input type="password" value="" class="input-text-password noPic input-click" name="password" id="password">
</div>
<div class="row">
<label class="field" for="contact">聯繫人</label>
<input type="text" value="" class="input-text-user noPic input-click" name="linkman" id="contact">
</div>
<div class="row">
<label class="field" for="company">公司名</label>
<input type="text" value="" class="input-text-user noPic input-click" name="company" id="company">
</div>
<div class="row">
<label class="field" for="tel">公司電話</label>
<input type="text" value="" class="input-text-user noPic input-click" name="companytel" id="tel">
</div>
<div class="row">
<label class="field" for="qq">QQ</label>
<input type="text" value="" class="input-text-user noPic input-click" name="qq" id="qq">
</div>
<div class="row btnArea">
<button class="login-btn" id="submit">註冊</button>
</div>
</form>
</div>
<div class="go-regist">
已有帳號,請<a href="/Demo/login.jsp" class="link">登錄</a>
</div>
</div>
</body>
</html>
登錄成功:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'success.jsp' starting page</title>
</head>
<body>
${sessionScope.success }成功! <br>
</body>
</html>
登錄失敗:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'error.jsp' starting page</title>
</head>
<body>
${sessionScope.error }失敗 <br>
</body>
</html>