mvc模式实现登录注册功能

2015/02/01 14:02
阅读数 2.3K

1、创建项目

展示项目


2、数据库设计


4、首先写view视图jsp界面:

创建了个文件分别是index.jsp主界面,login.jsp登录界面,insert.jsp注册界面

<%@ 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 'index.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>
		<a href="Login.jsp">${yonghu}登录</a>
		<a href="insert.jsp">注册</a>
		<br>
		${msg}

	</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>登录界面</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>
		<center>
			<h1>
				登录界面
			</h1>
			<hr color="blue">
			<form action="./login.do">
				<input type="hidden" name="methodName" value="0" />
				<table border="1px">
					<tr>
						<td>
							用户名:
						</td>
						<td>
							<input type="text" name="name" />
						</td>
					</tr>
					<tr>
						<td>
							密码:
						</td>
						<td>
							<input type="password" name="pass" />
						</td>
					</tr>
				</table>
				<input type="submit" value="登录" />
			</form>
		</center>
	</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>注册界面</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>
		<center>
			<h1>
				注册界面
			</h1>
			<hr color="blue">
			<form action="./login.do">
				<input type="hidden" name="methodName" value="1" />
				<table border="1px">
					<tr>
						<td>
							用户名:
						</td>
						<td>
							<input type="text" name="name" />
						</td>
					</tr>
					<tr>
						<td>
							密码:
						</td>
						<td>
							<input type="password" name="pass" />
						</td>
					</tr>
				</table>
				<input type="submit" value="注册" />
			</form>
		</center>
	</body>
</html>
4、接下来是model层

他那javabean类

package cn.csdn.web.domain;

import java.io.Serializable;

public class Admin implements Serializable {
	private Integer id;
	private String name;
	private String pass;
	private String sex;
	private Integer age;

	public Admin() {
		super();
		// TODO Auto-generated constructor stub
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPass() {
		return pass;
	}

	public void setPass(String pass) {
		this.pass = pass;
	}

	public String getSex() {
		return sex;
	}

	public void setSex(String sex) {
		this.sex = sex;
	}

	public Integer getAge() {
		return age;
	}

	public void setAge(Integer age) {
		this.age = age;
	}

}

dao接口

package cn.csdn.web.dao;

import cn.csdn.web.domain.Admin;

public interface AdminDao {
	boolean select(Admin entity);
	boolean insert(Admin entity);
}

实现类
package cn.csdn.web.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import cn.csdn.web.domain.Admin;

public class AdminDaoImpl implements AdminDao {
	private static Connection conn = null;
	private PreparedStatement pstmt = null;
	private ResultSet rs = null;
	private static final String URL = "jdbc:mysql://localhost:3306/3g?user=root&password=123&useUnicode=true&characterEncoding=UTF-8";
	static {
		try {
			Class.forName("com.mysql.jdbc.Driver");
			try {
				conn = DriverManager.getConnection(URL);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	private void release(ResultSet rs, PreparedStatement pstmt) {
		if (rs != null) {
			try {
				rs.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if (pstmt != null) {
			try {
				pstmt.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	public boolean select(Admin entity) {
		// 声明返回值变量
		boolean flag = false;
		// 声明sql语句
		String sql = "select * from admin where name=? and pass=?";
		try {
			// 创建
			pstmt = conn.prepareStatement(sql);
			// 为占位符赋值
			int index = 1;
			pstmt.setObject(index++, entity.getName());
			pstmt.setObject(index++, entity.getPass());
			// 执行更新
			rs = pstmt.executeQuery();
			if (rs.next()) {
				flag = true;
				System.out.println("e");
			}
			release(rs, pstmt);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		release(rs, pstmt);
		// 修改返回值变量
		return flag;
	}

	public boolean insert(Admin entity) {
		boolean flag = false;
		String sql = "insert into admin(name,pass) values(?,?)";
		try {
			pstmt = conn.prepareStatement(sql);
			int index = 1;
			pstmt.setObject(index++, entity.getName());
			pstmt.setObject(index++, entity.getPass());
			int i = pstmt.executeUpdate();
			if (i > 0) {
				flag = true;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		release(rs, pstmt);
		return flag;
	}

}

5、最后是control层
package cn.csdn.web.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.csdn.web.dao.AdminDao;
import cn.csdn.web.dao.AdminDaoImpl;
import cn.csdn.web.domain.Admin;

public class LoginServlet extends HttpServlet {
	private AdminDao aDao = new AdminDaoImpl();

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setCharacterEncoding("utf-8");
		request.setCharacterEncoding("utf-8");
		String method = request.getParameter("methodName");
		if (method.equals("0")) {
			select(request, response);
			request.getRequestDispatcher("./index.jsp").forward(request,
					response);
		} else if (method.equals("1")) {
			insert(request, response);
			request.getRequestDispatcher("./index.jsp").forward(request,
					response);
		} else {
			System.out.println("大大大失败");
		}

	}

	public void select(HttpServletRequest request, HttpServletResponse response) {
		String name = request.getParameter("name");
		String pass = request.getParameter("pass");
		Admin entity = new Admin();
		entity.setName(name);
		entity.setPass(pass);
		boolean flag = aDao.select(entity);
		if (flag) {
			System.out.println("登录成功");
			request.setAttribute("yonghu", name);
			request.setAttribute("msg", "登录成功");
		} else {
			System.out.println("登录失败");
		}
	}

	public void insert(HttpServletRequest request, HttpServletResponse response) {
		String name = request.getParameter("name");
		String pass = request.getParameter("pass");
		Admin entity = new Admin();
		entity.setName(name);
		entity.setPass(pass);
		boolean flag = aDao.insert(entity);
		if (flag) {
			System.out.println("注册成功");
		} else {
			System.out.println("注册失败");
		}
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doGet(request, response);
	}

}



展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部