要求
tomcat、oracle数据库服务启动
需要一个Oracle的驱动器,放在WEB-INFO的lib。版本号如下:
Eclipse的创建目录
数据库文件
在Oracle数据库中的scott的连接,建立一个名字叫t_user的表,表格的字段为username,password。
UserDao接口,定义查找用户(findUser)方法和注册用户(addUser)方法。
package jsp2.dao;
import jsp2.po.User;
public interface UserDao {
/**
* 查找用户
* */
public User findUser(User user);
/**
* 注册用户
* */
public void addUser(User user);
}
抽象类,实现 数据库连接,关闭。
package jsp2.dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public abstract class BaseDao {
private final static String url="jdbc:oracle:thin:@localhost:1521:orcl";
private final static String name="scott";
private final static String pass="tiger";
public Connection conn = null;
public PreparedStatement pstm = null;
public ResultSet rs = null;
/**
* 数据库连接
* */
public Connection getConn() {
try {
//加载数据库驱动
Class.forName("oracle.jdbc.driver.OracleDriver");
//创建数据库连接对象
conn = DriverManager.getConnection(url, name, pass);
}catch(ClassNotFoundException e){
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return conn;
}
/**
* 关闭方法
* */
public void close() {
try {
if(rs!=null) {
rs.close();
rs=null;
}
if(pstm!=null) {
pstm.close();
pstm=null;
}
if(conn!=null) {
conn.close();
conn=null;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
UserDao实现类
package jsp2.dao.impl;
import java.sql.SQLException;
import jsp2.dao.BaseDao;
import jsp2.dao.UserDao;
import jsp2.po.User;
public class UserDaoImpl extends BaseDao implements UserDao {
@Override
public User findUser(User user) {
// TODO Auto-generated method stub
User u = null;
try {
String sql="select username,password from t_user where username=? "
+ "and password=?";
conn = getConn();
pstm =conn.prepareStatement(sql);
pstm.setString(1, user.getUsername());
pstm.setString(2, user.getPassword());
rs= pstm.executeQuery();
while (rs.next()) {
u = new User(rs.getString("username"),rs.getString("password"));
}
} catch (SQLException e) {
// TODO: handle exception
}finally {
close();//调用关闭方法
}
return u;
}
@Override
public void addUser(User user) {
// TODO Auto-generated method stub
try {
String sql="insert into t_user(username,password) values(?,?)";
conn = getConn();
pstm =conn.prepareStatement(sql);
pstm.setString(1, user.getUsername());
pstm.setString(2, user.getPassword());
pstm.executeQuery();
} catch (SQLException e) {
// TODO: handle exception
}finally {
close();
}
}
}
封装一个user对象。
package jsp2.po;
public class User {
private String username;
private String password;
public User(String username, String password) {
this.username = username;
this.password = password;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
UserService接口,继承UserDao中的方法,定义用户是否重复方法。
package jsp2.service;
import jsp2.dao.UserDao;
import jsp2.po.User;
public interface UserService extends UserDao {
/**
*用户是否重复
*/
public boolean isReg(User user);
}
package jsp2.service.impl;
import jsp2.dao.UserDao;
import jsp2.dao.impl.UserDaoImpl;
import jsp2.po.User;
import jsp2.service.UserService;
public class UserServiceImpl implements UserService {
public UserDao uDao = new UserDaoImpl();
@Override
public User findUser(User user) {
// TODO Auto-generated method stub
return uDao.findUser(user);
}
@Override
public void addUser(User user) {
// TODO Auto-generated method stub
uDao.addUser(user);
}
@Override
public boolean isReg(User user) {
// TODO Auto-generated method stub
User u =uDao.findUser(user);
if(u==null) {
return true;
}
return false;
}
}
登录页面,用于登录用户,登录成功后跳转到success页面,失败返回login页面。
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>登录</title>
<style>
input[type="submit"] {
width: 100px;
margin: 0 5px;
}
input[type="button"] {
width: 100px;
margin: 0 5px;
}
</style>
</head>
<body>
<center>
<font face="楷体" size="6" color="#000">登录界面</font>
<form action="index.jsp" method="post">
<table width="300" height="180" border="5" bordercolor="#A0A0A0">
<tbody>
<tr>
<td><label>用户名:</label></td>
<td><input type="text" name="name" maxlength = "12" placeholder="请输入用户名!" /></td>
</tr>
<tr>
<td><label>密 码:</label></td>
<td><input type="password" name="pwd" maxlength = "16" placeholder="请输入密码!" /></td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" value="登陆" />
<input type="button" value="注册" onclick = "window.open('reg.jsp')" /></td>
</tr>
</tbody>
</table>
</form>
</center>
</body>
</html>
登录检测,调用UserService中的java代码(方法),实现用户登录
<%@page import="jsp2.service.impl.UserServiceImpl"%>
<%@page import="jsp2.service.UserService"%>
<%@page import="jsp2.po.User" %>
<%@ page import="java.sql.*"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>login</title>
</head>
<body>
<%
request.setCharacterEncoding("UTF-8");
String uName = request.getParameter("name");//获取name值
String uPwd = request.getParameter("pwd");//获取pwd值
UserService userService = new UserServiceImpl();
User user=userService.findUser(new User(uName,uPwd));//调用findUser方法,用户登录
if(user!=null){
//登录成功
session.setAttribute("info", user);
//设置session值,自己要的数据什么的放在session里面传来传去
response.sendRedirect("success.jsp");
}else{
//登录失败
response.sendRedirect("login.jsp");
}
%>
</body>
</html>
注册页面,用于注册用户,注册成功后跳转到login页面,失败返回reg页面。
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>注册界面</title>
<style>
input[type="submit"] {
width: 100px;
margin: 0 5px;
}
input[type="reset"] {
width: 100px;
margin: 0 5px;
}
</style>
<script>
function addCheck() {
var username = document.getElementById("username").value;
var password = document.getElementById("password").value;
var newword = document.getElementById("newword").value;
if (username == "") {
alert("用户名不能为空!");
document.getElementById("username").focus();
return false;
}
if (password == "") {
alert("密码不能为空!");
document.getElementById("password").focus();
return false;
}
if (password != newword) {
alert("两次输入密码不相同!");
document.getElementById("newword").focus();
return false;
}
}
function validate() {
var flag = addCheck();
if (flag == false)
return false;
return true;
}
</script>
</head>
<body>
<center>
<font face="楷体" size="6" color="#000">注册界面</font>
<form action="index2.jsp" method="post"
onsubmit="return validate()">
<table width="300" height="180" border="5" bordercolor="#A0A0A0">
<tr>
<th>用户名:</th>
<td><input type="text" name="name" id="username"
placeholder="输入4个字符以内" maxlength="4"></td>
</tr>
<tr>
<th>输入密码:</th>
<td><input type="password" name="pwd" id="password"
placeholder="输入12个字符以内" maxlength="12"></td>
</tr>
<tr>
<th>确认密码:</th>
<td><input type="password" name="nwd" id="newword"
placeholder="重新输入密码" maxlength="12"></td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit"value="注 册">
<input type="reset" value="重 置"></td>
</tr>
</table>
</form>
</center>
</body>
</html>
注册检测,调用UserService中的java代码(方法),实现用户注册
<%@page import="jsp2.service.impl.UserServiceImpl"%>
<%@page import="jsp2.service.UserService"%>
<%@page import="jsp2.po.User" %>
<%@ page import="java.sql.*" %>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>login</title>
</head>
<body>
<%
request.setCharacterEncoding("UTF-8");
String uName = request.getParameter("name");//获取输出的name值
String uPwd = request.getParameter("pwd");//获取输出的pwd值
UserService userService = new UserServiceImpl();
boolean fg = userService.isReg(new User(uName,uPwd));//调用isReg方法
if(fg){
//注册成功
userService.addUser(new User(uName,uPwd));//注册用户
response.sendRedirect("login.jsp");
}else{
//注册失败
response.sendRedirect("reg.jsp");
}
%>
</body>
</html>
登录成功后跳转页面
<%@page import="jsp2.po.User"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>登录成功</title>
</head>
<body>
<center>
<h3>
<%
if(session.getAttribute("info")==null){
response.sendRedirect("login.jsp");
}else{
User user=(User) session.getAttribute("info");
%>
欢迎你:<%=user.getUsername() %>
<%
}
%>
<br/>
<a href="out.jsp">注销</a>
</h3>
</center>
</body>
</html>
将当前登录的用户注销,退出登录成功页面,需重新再次登录才能进入页面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>注销</title>
</head>
<body>
<%
session.removeAttribute("info"); //清空当前session中指定对象的属性
response.sendRedirect("login.jsp");//重定向(页面跳转)
%>
</body>
</html>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>jsp2display-name>
<welcome-file-list>
<welcome-file>login.jspwelcome-file>
welcome-file-list>
<session-config>
<session-timeout>2session-timeout>
session-config>
web-app>