欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

简单账号管理系统的实现(b/s、servlet、html、mysql)

程序员文章站 2022-05-30 11:02:07
...

      漫漫学习路,无处是尽头。“技术是学不完的,只能学会学习能力,并不断学习才是真”这句绝对的真理。总结的虽是,斌哥布置的练习,但完成这个练习的过程中,却真心是温习了很多,学习了很多。

      进入正题,下面总结下简单账号管理系统的实现过程。

      设计如下:

一、表结构

表名:userinfo

字段信息:id-int

               name-varchar

               password-varchar

二、模块结构

   1、数据操作模块

       将数据库操作写在一个包下。

public class DAO {
	//创建arrylist用以存放查询结果
 	 private  ArrayList<Userinfo> al;
/**
 * 插入数据到数据库中的方法
 */
 	public  ArrayList<Userinfo> add(Userinfo user){
		//获取数据库连接对象
		Connection conn=Until.getDBConnect();
		//sql操作数据
		String sql="insert into userinfo(name,pwd) values('"+user.name+"','"+user.password+"')";
		try {
			Statement stmt=conn.createStatement();
			int i=stmt.executeUpdate(sql);
			if(i>0){
				System.out.println("插入数据成功!");
			}
			//关闭数据库连接
			conn.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return searchAll();
	}
	/**
	 * 查询数据库所有数据
	 */
 	public  ArrayList<Userinfo>   searchAll(){
 		al=new ArrayList<Userinfo>();
		//获取数据库连接对象
		Connection conn=Until.getDBConnect();
		//sql操作数据
		String sql="select * from userinfo";
		try {
			Statement stmt=conn.createStatement();
			ResultSet rs=stmt.executeQuery(sql);
			while(rs.next()){
				Userinfo user=new Userinfo(rs.getInt("id"), rs.getString("name"),rs.getString("pwd"));
				this.al.add(user);
			}
			//清空结果集
			rs.close();
			//关闭数据库
			conn.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return al;
		
	}
	/**
	 * 按给定的id查询数据的方法
	 */
 	public  ArrayList<Userinfo> searchById(int id){
		//获得数据库连接对象
		Connection conn=Until.getDBConnect();
		//操作数据
		String sql="select * from userinfo where id="+id;
		try {
			Statement stmt=conn.createStatement();
			ResultSet rs=stmt.executeQuery(sql);
			while(rs.next()){
				System.out.println("id="+rs.getInt("id")+",name="+rs.getString("name")+",password="+rs.getString(3));
			}
			conn.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return searchAll();
	}
	/**
	 * 删除指定id的数据的方法
	 */
 	public  ArrayList<Userinfo> deleteById(int id){
		//获取数据库连接对象
		Connection conn=Until.getDBConnect();
		//操作数据
		String sql="delete from userinfo where id="+id;
		try {
			Statement stmt=conn.createStatement();
			int i=stmt.executeUpdate(sql);
			if(i>0){
				System.out.println("删除成功!!");
			}
			conn.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return searchAll();
	}
	/**
	 * 根据指定id修改数据的方法
	 */
 	public  ArrayList<Userinfo> updateById(int id,Userinfo user){
		//获取数据库连接对象
		Connection conn=Until.getDBConnect();
		//数据操作
		String sql="update userinfo set name=?,pwd=? where id=?";	
		
		try {
			//预编译
			PreparedStatement ps=conn.prepareStatement(sql);
		//设置对象参数
			ps.setString(1, user.name);
			ps.setString(2, user.password);
			ps.setInt(3, id);
			int i=ps.executeUpdate();
			if(i>0){
				System.out.println("数据修改成功!");
			}
			conn.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return searchAll();
	}
}

 
简单账号管理系统的实现(b/s、servlet、html、mysql)
            
    
    博客分类: 足迹 b/sservlethtmlmysql

 2、用5个servlet、2个html页面进行页面

5个servlet:Userinfo-用户列表servlet-用于查找并显示所有用户

                   Deleteservlet-删除操作servlet-用于删除指定用户

                   Updateservlet-更新操作servlet-用于更新指定用户信息

                   Regesiterservlet-添加操作(注册)servlet-用于注册新用户

                   Logincheck-用户身份验证servlet-用于验证登陆用户是否为注册用户

2个html:page.html-登陆页面

               register.html-注册页面

3、系统各页面交互流程


简单账号管理系统的实现(b/s、servlet、html、mysql)
            
    
    博客分类: 足迹 b/sservlethtmlmysql
 4、代码示例

userinfo:

/**显示用户servlet:用于显示当前全部用户信息
 * Servlet implementation class Userinfo
 */
public class Userinfo extends HttpServlet {
       DAO dao=new DAO();
  

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//设置响应文本字符集
		response.setContentType("text/html;charset=gbk");
		PrintWriter out=response.getWriter();
		//创建arraylist来接收查询结果
		ArrayList<test1_db.Userinfo> al_temp=dao.searchAll();
		//拼出显示页面
		out.println("<!DOCTYPE HTML PUBLIC\"-//W3C//DTD HTML 4.01 Transitional//EN\">");
		out.println("<html>");
		out.println("<head><title>Userinfo</title></head>");
		out.println("<body>");
		out.println("userinfo show as follows:");
		out.println("<table border=\"5\"width=\"250px\"bgcolor=\"green\"bordercolor=\"gray\">");
		out.println("<tr><td>ID</td><td>姓名</td><td>密码</td><td>操作</td><td>操作</td></tr>");
		//循环查询用户信息以显示
		for(int i=0;i<al_temp.size();i++){
		test1_db.Userinfo user_temp=al_temp.get(i);
		int id=user_temp.id;
		String name=user_temp.name;
		String password=user_temp.password;
		out.println("<tr><td>"+id+"</td><td>"+name+"</td><td>"+password+"</td><td><a href='Deleteservlet?id="+id+" '>删除用户</a></td><td><a href='Updateservlet?id="+id+"'>修改信息</a></td></tr>");
		}
		out.println("</border>");
		out.println("</table>");
		out.println("</body>");
		out.println("</html>");
	}

 logincheck:

/**
 * Servlet implementation class Logincheck
 */
public class Logincheck extends HttpServlet {
	private static final long serialVersionUID = 1L;
       DAO dao=new DAO();
  
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html;charset=gbk");
		PrintWriter out=response.getWriter();
		//获得参数
		String name=request.getParameter("username");
		String password=request.getParameter("password");
		//创建用户列表以存放查询结果
		ArrayList<test1_db.Userinfo> al_temp=dao.searchAll();
		//循环得到每条记录以验证身份
		for(int i=0;i<al_temp.size();i++){
		test1_db.Userinfo user_temp=al_temp.get(i);
		//判断用户名和密码是否存在
		if(user_temp.name.equals(name)&&user_temp.password.equals(password)){
			//是,则跳转到用户列表servlet并跳出循环
			response.sendRedirect("Userinfo");
			break;
		}
		}
		//否,则拼出提示页面
		out.println("<!DOCTYPE HTML PUBLIC\"-//W3C//DTD HTML 4.01 Transitional//EN\">");
		out.println("<html>");
		out.println("<body>");
		out.println("用户不存在,请注册……");
		out.println("<p><a href='register.html'>注册</a></p>");
		out.println("</body>");
		out.println("</html>");
	}

 updateservlet:

/**
 * Servlet implementation class Updateservlet
 */
public class Updateservlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	DAO dao = new DAO();

	
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html;charset=gbk");
		PrintWriter out = response.getWriter();
		//获得来自于用户列表servlet的参数id
		String id_temp = request.getParameter("id");
		int id = Integer.parseInt(id_temp);
		//创建arraylist以存放查找的对应id的记录
		ArrayList<test1_db.Userinfo> al_temp = dao.searchById(id);
		//拼出查找结果显示页面
		out.println("<!DOCTYPE HTML PUBLIC\"-//W3C//DTD HTML 4.01 Transitional//EN\">");
		out.println("<html>");
		out.println("<head><title>Update</title></head>");
		out.println("<body>");
		out.println("please recorrect the information as follows:");
		out.println("<table border=\"5\"width=\"250px\"bgcolor=\"green\"bordercolor=\"gray\">");
		out.println("<tr><td>ID</td><td>姓名</td><td>密码</td></tr>");
		//获得查找结果集中的参数以用于传入“修改”响应
		test1_db.Userinfo user = al_temp.get(0);
		String name = user.name;
		String password = user.password;
		String username = "username";
		String userpassword = "userpassword";
		// 1、重写url传值
//		out.println("<form action='Updateservlet?id=" + user.id
//				+ "'  method='post'>");
//		out.println("<tr><td >" + id + "</td><td><input type='text' name='"
//				+ username + "' value='" + name
//				+ "'><br></td><td><input type='text' name='" + userpassword
//				+ "' value='" + password + "'><br></td></tr>");
		// 2、表单传值
		out.println("<form action='Updateservlet'  method='post'>");
		out.println("<tr><td >" + id + "</td><td><input type='text' name='"
				+ username + "' value='" + name
				+ "'><br></td><td><input type='text' name='" + userpassword
				+ "' value='" + password + "'><br></td></tr>");
		out.println("<input type='hidden' name='id' value='" + id + "'>");
		out.println("<input type='submit' value='提交'><input type='reset' name='reset'>");
		out.println("</form>");
		out.println("</border>");
		out.println("</table>");
		out.println("</body>");

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		//获得updateservlet中传入的更新数据所需的参数
		String name_update = request.getParameter("username");
		String password_update = request.getParameter("userpassword");
		String id_update = request.getParameter("id");
		int id = Integer.parseInt(id_update);
		//创建更新后的用户对象、执行更新操作并跳转页面到用户列表servlet
		test1_db.Userinfo user_update = new test1_db.Userinfo(id, name_update,
				password_update);
		dao.updateById(id, user_update);
		response.sendRedirect("Userinfo");
	}

}

 registerservlet:

/**
 * Servlet implementation class Register
 */
public class Register extends HttpServlet {
	private static final long serialVersionUID = 1L;
       DAO dao=new DAO();
   

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//获得参数
		String username=request.getParameter("username");
		String password=request.getParameter("password");
		//创建要添加的用户对象
		test1_db.Userinfo user_temp=new test1_db.Userinfo(username, password);
		dao.add(user_temp);
		//跳转回登陆界面
		response.sendRedirect("page.html");
	}

 deleteservlet:

/**
 * Servlet implementation class Deleteservlet
 */
public class Deleteservlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("gbk");
		PrintWriter out=response.getWriter();
		out.println("进入删除servlet!");
		//获得需删除的用户信息的id
		String id_temp=request.getParameter("id");
		int id=Integer.parseInt(id_temp);
		DAO dao=new DAO();
		dao.deleteById(id);
		//跳转回用户显示servlet
		response.sendRedirect("Userinfo");
		
	}
 
  • 简单账号管理系统的实现(b/s、servlet、html、mysql)
            
    
    博客分类: 足迹 b/sservlethtmlmysql
  • 大小: 6.8 KB
  • 简单账号管理系统的实现(b/s、servlet、html、mysql)
            
    
    博客分类: 足迹 b/sservlethtmlmysql
  • 大小: 40.3 KB