实体类entity
User.java
package com.bzxy.entity;
public class User {
private int id;
private String user;
private String pwd;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUser() {
return user;
}
public void setUser(String user) {
this.user = user;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public User() {
super();
// TODO Auto-generated constructor stub
}
public User(int id, String user, String pwd) {
super();
this.id = id;
this.user = user;
this.pwd = pwd;
}
}
UserDao.java
package com.bzxy.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.bzxy.entity.User;
import com.bzxy.jdbc.DBUtil;
public class UserDao {
//获得Conn对象
private Connection conn = DBUtil.getConn();
private PreparedStatement ps;
/**
* 用户注册
*
*/
public boolean reg(User u){
String sql = "insert into user(user,pwd) values(?,md5(?))";
try {
ps = conn.prepareStatement(sql);
ps.setString(1, u.getUser());
ps.setString(2, u.getPwd());
boolean b = ps.execute();
if(!b){
return true;
}
} catch (SQLException e) {
e.printStackTrace();
}
return false;
}
/**
* 用户登录
*/
public User login(String user,String pwd){
String sql = "select * from user where user=? and pwd=md5(?)";
User u = null;
try {
ps = conn.prepareStatement(sql);
ps.setString(1, user);
ps.setString(2, pwd);
ResultSet rs = ps.executeQuery();
while(rs.next()){
int id = rs.getInt("id");
String name = rs.getString("user");
String p = rs.getString("pwd");
u = new User(id,name,p);
}
} catch (SQLException e) {
e.printStackTrace();
}
return u;
}
}
jdbc
Dbutil.java
package com.bzxy.jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
/**
* 连接数据库工具类,使用单例模式
* @author user
*
*/
public class DBUtil {
private static String url = "jdbc:mysql://localhost:3306/bzxy";
private static String user = "root";
private static String pwd = "root";
private static Connection conn ;
private DBUtil(){
}
static{
//1.动态加载驱动程序
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(url, user, pwd);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
/**
* 获得数据库连接的方法
*/
public static Connection getConn(){
return conn;
}
}
Service
UserService
import com.bzxy.dao.UserDao;
import com.bzxy.entity.User;
public class UserService {
private UserDao ud = new UserDao();
/**
* 用户注册
*
*/
public boolean reg(User u){
return ud.reg(u);
}
/**
* 用户登录
*/
public User login(String user,String pwd){
return ud.login(user, pwd);
}
}
servlet
Login
package com.bzxy.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.bzxy.entity.User;
import com.bzxy.service.UserService;
public class Login extends HttpServlet {
private UserService us = new UserService();
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request,response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//处理字符编码
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
//接受页面的参数
String user = request.getParameter("user");
String pwd = request.getParameter("pwd");
User u = us.login(user, pwd);
if(u == null){
//在服务器端设置的请求参数
request.setAttribute("mess", "登录失败");
//请求转发,把客户端的请求再转给客户端
request.getRequestDispatcher("login.jsp").forward(request, response);
}else{
//请求重定向
response.sendRedirect("main.jsp");
}
}
}
Reg
package com.bzxy.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.bzxy.entity.User;
import com.bzxy.service.UserService;
public class Reg extends HttpServlet {
private UserService us = new UserService();
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 处理字符编码
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
// 接受页面的参数
String user = request.getParameter("user");
String pwd = request.getParameter("pwd");
User u = new User(0,user,pwd);
boolean b = us.reg(u);
if(b){
response.sendRedirect("login.jsp");
}else{
//在服务器端设置的请求参数
request.setAttribute("mess", "注册失败");
//请求转发,把客户端的请求再转给客户端
request.getRequestDispatcher("reg.jsp").forward(request, response);
}
}
}
JSP
index
<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>XXXX-首页</title>
<style>
body{
margin: 0px;
padding: 0px;
}
.top{
background-color: #eeeeee;
height: 40px;
line-height: 40px;
text-indent: 20px
}
</style>
</head>
<body>
<p class="top">
<a href="login.jsp">登录</a>
<a href="reg.jsp">注册</a>
</p>
</body>
</html>
login
<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>登录</title>
</head>
<body>
<%
Object obj = request.getAttribute("mess");
%>
<h1 align="center">用户登录</h1>
<form action="Login" method="post">
<table align="center" height="150px">
<tr>
<td>
账号:<input type="text" name="user" />
</td>
</tr>
<tr>
<td>
密码:<input type="password" name="pwd" />
</td>
</tr>
<tr>
<td align="center">
<input type="submit" value="登录" />
<%
if(obj != null){
%>
<font color="red"><%=obj %></font>
<%
}
%>
</td>
</tr>
</table>
</form>
</body>
</html>
main
<%@ 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 'main.jsp' starting page</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>
你好,欢迎登录<br>
</body>
</html>
reg
<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>注册</title>
</head>
<body>
<%
Object obj = request.getAttribute("mess");
%>
<h1 align="center">用户注册</h1>
<form action="Reg" method="post">
<table align="center" height="150px">
<tr>
<td>
账号:<input type="text" name="user" />
</td>
</tr>
<tr>
<td>
密码:<input type="password" name="pwd" />
</td>
</tr>
<tr >
<td align="center">
<input type="submit" value="注册" />
<%
if(obj != null){
%>
<font color="red"><%=obj %></font>
<%
}
%>
</td>
</tr>
</table>
</form>
</body>
</html>