史上最简单的jsp+servlet+jdbc实现CRUD项目,已经经历了千锤百炼的测试与修改,健壮性一级棒!没有任何问题。而且简明易懂,内附大量注释,是不可多得的好例子。为了保证简单,甚至没有把每个servlet里的jdbc连接单独拿出来,直观。数据库也一并给出。使用本项目时,首先访问的登录页面,即:http://localhost:8080/demo1/login.jsp,用户名root,密码root,也可以用库里面的其它用户名、密码。输入正确进入welcome页然后跳到查看学生信息页,输入错误进error.jsp,然后自动跳转回登录页。
listStudent.jsp
目录结构:
先创建库。
写前端吧。
******************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>
- <style type="text/css">
- body {
- background-image:url('images/yellowbg.png');
- background-size: cover; /* 图片平铺拉伸,适应屏幕 */
- }
- h1{ /* 标题居中 */
- margin:100px auto;
- text-align: center;
- }
- form{ /* 表单居中 */
- width:300px;
- height:220px;
- margin: 100px auto;
- }
- .item{ /* item与itemV的设定纯粹是为了让出入框左对齐,名字右对齐 */
- width:80px;
- display:inline-block;
- text-align: right;
- }
- .itemV{
- width:180px;
- display:inline-block;
- text-align: right;
- }
- #btn{ /* 按钮居中 */
- margin-left: 80px;
- }
- </style>
- </head>
- <body>
- <h1>欢迎登录学生管理系统!</h1>
- <form action="loginServlet" method="post">
- <span class="item">用户名:</span>
- <input type="text" name="username" /></span><br><br>
- <span class="item">密码:</span>
- <input type="text" name="password" /></span><br><br><br>
- <div id="btn">
- <input type="submit" value="登录" />
- <input type="reset" value="重置" />
- </div>
- </form>
- </body>
- </html>
welcome.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>
- <style>
- body {
- background-image:url('images/greenbg.png');
- background-size: cover;
- }
- h1{
- margin:150px auto;
- text-align: center;
- }
- span{
- color:red;
- font-family:fantasy;
- }
- </style>
- </head>
- <body>
- <h1>登录成功,欢迎<span><%=request.getAttribute("username") %></span>来到学生管理系统!</h1>
- <center><p>5秒钟后系统会自动跳转到查看全部学生信息页面...</p></center>
- <%response.setHeader("refresh","5;url=findAllServlet"); %>
- </body>
- </html>
- <%@ 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>
- <style>
- body {
- background-image:url('images/yellowbg.png');
- background-size: cover;
- }
- h1{
- margin:150px auto;
- text-align: center;
- }
- span{
- color:red;
- font-family:fantasy;
- }
- </style>
- </head>
- <body>
- <h1>登录失败,<span><%=request.getAttribute("username") %></span>的用户名或者密码错误!</h1>
- <center><p>5秒钟后系统会自动跳转到登录页面...</p></center>
- <%response.setHeader("refresh","5;url=login.jsp"); %>
- </body>
- </html>
listStudent.jsp
- <%@ page language="java" contentType="text/html; charset=utf-8"
- pageEncoding="utf-8"%>
- <%@page import="java.util.List" %>
- <%@page import="com.bean.Student" %>
- <!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>
- <style>
- body {
- background-image:url('images/greenbg.png');
- background-size: cover; /* 图片平铺拉伸,适应屏幕 */
- }
- h1{
- margin:50px auto;
- text-align: center;
- }
- table {
- width:600px;
- margin:50px auto;
- border-collapse: collapse;
- text-align: center;
- }
- table,th,td{border:1px solid black;}
- th{height:50px;}
- a:link {color:black;} /* 未访问链接*/
- a:visited {color:black;}
- a:hover {color:#f00;} /* 鼠标移动到链接上 */
- a:active {color:#f60;} /* 已点击 */