jsp学生管理系统

  • login.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h3>欢迎使用学生系统</h3>
<form action="LoginServlet" method="post">
账号:<input type="text" name="username"/><br/>
密码:<input type="password" name="password"/><br/>
<input type="submit" value="登录">
</form>
</body>
</html>
  • LoginServlet.java
package com.test.servlet;

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

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

import com.test.domin.Student;
import com.test.impl.StuDaoImpl;
import com.test.impl.UserDaoImpl;

/**
 * Servlet implementation class LoginServlet
 */
@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.setContentType("text/html;charset=utf-8");
		request.setCharacterEncoding("utf-8");//post方法防乱码
		
		//1.获取客户端提交的信息
		String username=request.getParameter("username");
		String password=request.getParameter("password");
		
		//访问Dao 看是否能登陆
		UserDaoImpl userdaoimpl=new UserDaoImpl();
		if(userdaoimpl.Login(username, password))
		{
			//response.getWriter().write("欢迎登录");
			//查询学生信息
			StuDaoImpl studaoimpl=new StuDaoImpl();
			List<Student> list=studaoimpl.findXinxi();
			
			//2. 先把这个集合存到作用域中。
			request.getSession().setAttribute("list", list);
			
			response.sendRedirect("student_list.jsp");
		}else{
			response.getWriter().write("用户名或密码错误");
		}
	}

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

}
  • 用户表t_user
CREATE TABLE `t_user` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `username` varchar(20) DEFAULT NULL,
  `password` varchar(20) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8
  • UserDao接口
package com.test.Dao;

public interface UserDao {
	
	Boolean Login(String username,String password);
}
  • UserDaoImpl.java
package com.test.impl;

import com.test.Dao.UserDao;
import java.sql.*;

public class UserDaoImpl implements UserDao{

	@Override
	public Boolean Login(String username, String password) {
		// TODO Auto-generated method stub
		
		Connection ct=null;
		PreparedStatement ps=null;
		ResultSet rs=null;

		try{
			//1加载驱动
			Class.forName("com.mysql.jdbc.Driver");
			//2.得到连接  jdbc协议  mysql子协议 localhost:3306主机端口  student数据库
			ct=DriverManager.getConnection
			("jdbc:mysql://localhost:3306/stumanager?useUnicode=true&amp;characterEncoding=UTF-8","root","yang1314");
			//3.创建ps(预编译的sql对象)
			//statement/PreparedStatement/CallableStatement
			ps=ct.prepareStatement("select * from t_user where username=? and password=?");
			ps.setString(1, username);//这里设置了第一个问号的值
			ps.setString(2, password);//这里设置了第二个问号的值
			
			//4.执行
			rs=ps.executeQuery();
			//如果在数据库中能查到有这个用户名并且密码符合的数据,那么就存在,你可以在if(rs.next())里面执行你想要的操作,如果不存在这样的用户,那么就不走if
			//如果能够成功移到下一条记录,那么表明有这个用户。
			 return rs.next();
			
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			//关闭
			if(rs!=null){
				try {
					rs.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				rs=null;
			}
			if(ps!=null){
				try {
					ps.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				ps=null;
			}
			if(ct!=null){
				try {
					ct.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				ct=null;
			}
			
		}
	



		return null;
	}

}
  • student_list.jsp
<%@page import="java.util.List"%>
<%@ 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 PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; 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>
  • student.java
package com.test.domin;

public class Student {
	private int id;
	private String name;
	private int age;
	private String gender;
	private String address;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getGender() {
		return gender;
	}
	public void setGender(String gender) {
		this.gender = gender;
	}
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	
}
  • t_stu表
 CREATE TABLE `t_stu` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(20) DEFAULT NULL,
  `age` int(11) DEFAULT NULL,
  `gender` varchar(4) DEFAULT NULL,
  `address` varchar(30) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8
  • StuDao接口
package com.test.Dao;

import java.util.List;

import com.test.domin.Student;

public interface StuDao {
	List<Student> findXinxi();
}
  • StuDaoImpl.java
package com.test.impl;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;

import com.test.Dao.StuDao;
import com.test.domin.Student;

public class StuDaoImpl implements StuDao{

	@Override
	public List<Student> findXinxi() {
		// TODO Auto-generated method stub
		List<Student> list=new LinkedList<Student>();
		Connection ct=null;
		PreparedStatement ps=null;
		ResultSet rs=null;

		try{
			//1加载驱动
			Class.forName("com.mysql.jdbc.Driver");
			//2.得到连接  jdbc协议  mysql子协议 localhost:3306主机端口  student数据库
			ct=DriverManager.getConnection
			("jdbc:mysql://localhost:3306/stumanager?useUnicode=true&amp;characterEncoding=UTF-8","root","yang1314");
			//3.创建ps(预编译的sql对象)
			ps=ct.prepareStatement("select * from t_stu");
			//4.执行
			rs=ps.executeQuery();
			//如果在数据库中能查到有这个用户名并且密码符合的数据,那么就存在,你可以在if(rs.next())里面执行你想要的操作,如果不存在这样的用户,那么就不走if
			//如果能够成功移到下一条记录,那么表明有这个用户。
			while(rs.next()){
				Student student=new Student();
				student.setId(rs.getInt("id"));
				student.setName(rs.getString("name"));
				student.setAge(rs.getInt("age"));
				student.setGender(rs.getString("gender"));
				student.setAddress(rs.getString("address"));
				list.add(student);
			}
			
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			//关闭
			if(rs!=null){
				try {
					rs.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				rs=null;
			}
			if(ps!=null){
				try {
					ps.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				ps=null;
			}
			if(ct!=null){
				try {
					ct.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				ct=null;
			}
			
		}
	



		return list;
	}

}

	
	

 

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值