学习日志——2019/09/06

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/weixin_44757417/article/details/100586494

jsp

学生信息管理系统

  • 需求分析在这里插入图片描述
  1. 先写 login.jsp , 并且搭配一个LoginServlet 去获取登录信息。
  • login.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>登入</title>
</head>
<body>
	<h1>欢迎使用学生信息管理系统</h1>
	<form action="Login_servlet" method="post">
		账号:<input type="text" name="username"> <br>
		密码:<input type="password" name="password"><br>
		<input type="submit" value="登入">
	</form>
</body>
</html>
  • servlet
package web.servlet;


import java.io.IOException;
import java.util.List;

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

import dao.StuDaoImpl;
import dao.UserDaoImpl;
import dao.stuDao;
import domain.Student;

/**
 * 这是用于处理登入的servlet
 */
public class Login_servlet extends HttpServlet {
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//提交的数据可能有中文,怎么处理
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=utf-8");
		
		//1.获取客户端提交的信息
		String userName=request.getParameter("username");
		String password=request.getParameter("password");
		//System.out.println("username"+userName+"-->"+"password"+password);
		
		//2.去访问dao,看看是否满足登入。
		UserDaoImpl dao=new UserDaoImpl();
		boolean isSccess = dao.login(userName, password);
		
		//3.针对dao的返回结果,做出响应
		if(isSccess) {
			//response.getWriter().write("登入成功!");
			//1.查询出所有的学生信息
			stuDao stuDao=new StuDaoImpl();
			List<Student> list = stuDao.findAll();
			
			//2.先把这个集合存到作用域中
			
			request.getSession().setAttribute("list", list);
			
			//2.重定向
			response.sendRedirect("stu_list.jsp");
		}else {
			response.getWriter().write("用户名或密码错误!");
		}
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}

  1. 创建用户表, 里面只要有id , username 和 password

  2. 创建UserDao, 定义登录的方法

package dao;
/**
 * *这里定义了对用户表的访问规则
 * @author Administrator
 *
 */
public interface UserDao {
	/**
	 * 
	 ** 这里要返回一个Boolean类型,成功返回或者失败即可
	 * *但是在开发时,登入的方法,一旦成功返回所有信息
	 * @param userName
	 * @param password 
	 * @return true :登入成功 false: 登入失败
	 */
	boolean login(String userName,String password);
}

  1. 创建UserDaoImpl , 实现刚才定义的登录方法。
package dao;

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

import com.sun.org.apache.regexp.internal.recompile;

import util.JDBCUtil;

public class UserDaoImpl implements UserDao {

	@Override
	public boolean login(String userName,String password) {
		// TODO Auto-generated method stub
		Connection conn =null;
		PreparedStatement ps=null;
		ResultSet rs=null;
		try {
			//1.创建连接
			conn = JDBCUtil.getConn();
			//			System.out.println("数据状态:"+conn.isClosed());
			String sql="select * from t_user where username=? and password=?";

			//2.创建ps对象
			ps = conn.prepareStatement(sql);
			ps.setString(1, userName);
			ps.setString(2, password);

			//3.开始执行
			rs = ps.executeQuery();
			//如果能够成功就移到下一条记录。那么表明有这个用户
			return rs.next();

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			JDBCUtil.release(conn, ps, rs);
		}
		return false;
	}

}

  1. 在LoginServlet里面访问UserDao, 判断登录结果。 以区分对待

  2. 创建stu_list.jsp , 让登录成功的时候跳转过去。

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>学生信息管理系统</title>
</head>
<body>
<br>学生列表<br>
	<table border="1" width="700">
		<tr align="center">
			<td>编号</td>
			<td>姓名</td>
			<td>年龄</td>
			<td>性别</td>
			<td>住址</td>
			<td>操作</td>
		</tr>
		<c:forEach items="${list }" var="stu">
			<tr align="center">
				<td>${stu.id }</td>
				<td>${stu.name }</td>
				<td>${stu.age }</td>
				<td>${stu.gender }</td>
				<td>${stu.address }</td>
				<td><a href="#">更新</a> <a href="$">删除</a></td>
			</tr>


		</c:forEach>



	</table>


</body>
</html>
  1. 创建学生表 , 里面字段随意。

  2. 定义学生的Dao . StuDao

  • stuDao
package dao;

import java.util.List;

import domain.Student;

public interface stuDao {
	/**
	 * *查询出来所有的学生信息
	 * @return List集合
	 */
	List<Student> findAll();
}
  • StuDaoimpl
package dao;

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

import com.sun.org.apache.regexp.internal.recompile;

import util.JDBCUtil;

public class UserDaoImpl implements UserDao {

	@Override
	public boolean login(String userName,String password) {
		// TODO Auto-generated method stub
		Connection conn =null;
		PreparedStatement ps=null;
		ResultSet rs=null;
		try {
			//1.创建连接
			conn = JDBCUtil.getConn();
			//			System.out.println("数据状态:"+conn.isClosed());
			String sql="select * from t_user where username=? and password=?";

			//2.创建ps对象
			ps = conn.prepareStatement(sql);
			ps.setString(1, userName);
			ps.setString(2, password);

			//3.开始执行
			rs = ps.executeQuery();
			//如果能够成功就移到下一条记录。那么表明有这个用户
			return rs.next();

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			JDBCUtil.release(conn, ps, rs);
		}
		return false;
	}

}

  1. 对上面定义的StuDao 做出实现 StuDaoImpl
package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import domain.Student;
import util.JDBCUtil;

public class StuDaoImpl implements stuDao {

	@Override
	public List<Student> findAll() {
		// TODO Auto-generated method stub
		List<Student> list=new ArrayList<Student>();
		Connection conn =null;
		PreparedStatement ps=null;
		ResultSet rs=null;
		try {
			
			//1.创建连接
			conn = JDBCUtil.getConn();
			//			System.out.println("数据状态:"+conn.isClosed());
			String sql="select * from t_stu ";
			
			ps = conn.prepareStatement(sql);
			rs = ps.executeQuery();
			
		
			
			//数据多了,用对象装,对象多了用集合装
			while(rs.next()) {//循环10此,10个学生
				Student stu = new Student();
				stu.setId(rs.getInt("id"));
				stu.setName(rs.getString("name"));
				stu.setAge(rs.getInt("age"));
				stu.setGender(rs.getString("gender"));
				stu.setAddress(rs.getString("address"));
				
				list.add(stu);
				
			}
			

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			JDBCUtil.release(conn, ps, rs);
		}
		return list;
	}

}

  1. 在登录成功的时候,完成三件事情。

  2. 查询所有的学生

    1. 把这个所有的学生集合存储到作用域中。

    2. 跳转到stu_list.jsp

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//提交的数据可能有中文,怎么处理
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=utf-8");
		
		//1.获取客户端提交的信息
		String userName=request.getParameter("username");
		String password=request.getParameter("password");
		//System.out.println("username"+userName+"-->"+"password"+password);
		
		//2.去访问dao,看看是否满足登入。
		UserDaoImpl dao=new UserDaoImpl();
		boolean isSccess = dao.login(userName, password);
		
		//3.针对dao的返回结果,做出响应
		if(isSccess) {
			//response.getWriter().write("登入成功!");
			//1.查询出所有的学生信息
			stuDao stuDao=new StuDaoImpl();
			List<Student> list = stuDao.findAll();
			
			//2.先把这个集合存到作用域中
			
			request.getSession().setAttribute("list", list);
			
			//2.重定向
			response.sendRedirect("stu_list.jsp");
		}else {
			response.getWriter().write("用户名或密码错误!");
		}
		
  1. 在stu_list.jsp中,取出域中的集合,然后使用c标签 去遍历集合。
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>学生信息管理系统</title>
</head>
<body>
<br>学生列表<br>
	<table border="1" width="700">
		<tr align="center">
			<td>编号</td>
			<td>姓名</td>
			<td>年龄</td>
			<td>性别</td>
			<td>住址</td>
			<td>操作</td>
		</tr>
		<c:forEach items="${list }" var="stu">
			<tr align="center">
				<td>${stu.id }</td>
				<td>${stu.name }</td>
				<td>${stu.age }</td>
				<td>${stu.gender }</td>
				<td>${stu.address }</td>
				<td><a href="#">更新</a> <a href="$">删除</a></td>
			</tr>
		</c:forEach>
	</table>
</body>
</html>

效果:
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_44757417/article/details/100586494