jsp+servlet+jdbc 实现crud

本文介绍了如何利用JSP、Servlet和JDBC技术进行CRUD(创建、读取、更新、删除)操作。首先创建数据库,然后通过JSP页面设计用户界面,如登录、欢迎、错误提示等页面。接着,编写Servlet来处理用户请求,包括用户登录验证、数据展示、添加、修改和删除学生信息。整个流程涉及到了数据库连接、SQL语句执行、数据传输和页面跳转。最后,配置web.xml文件完成URL映射。
摘要由CSDN通过智能技术生成
史上最简单的jsp+servlet+jdbc实现CRUD项目,已经经历了千锤百炼的测试与修改,健壮性一级棒!没有任何问题。而且简明易懂,内附大量注释,是不可多得的好例子。为了保证简单,甚至没有把每个servlet里的jdbc连接单独拿出来,直观。数据库也一并给出。使用本项目时,首先访问的登录页面,即:http://localhost:8080/demo1/login.jsp,用户名root,密码root,也可以用库里面的其它用户名、密码。输入正确进入welcome页然后跳到查看学生信息页,输入错误进error.jsp,然后自动跳转回登录页。

目录结构:


先创建库。

写前端吧。

******************Jsp**********************

login.jsp

[html]  view plain   copy
  在CODE上查看代码片 派生到我的代码片
  1. <%@ page language="java" contentType="text/html; charset=utf-8"  
  2.     pageEncoding="utf-8"%>  
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
  4. <html>  
  5. <head>  
  6. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">  
  7. <title>Insert title here</title>  
  8. <style type="text/css">  
  9. body {  
  10.     background-image:url('images/yellowbg.png');  
  11.     background-size: cover;  /* 图片平铺拉伸,适应屏幕 */  
  12. }  
  13. h1{  /* 标题居中  */  
  14.     margin:100px auto;  
  15.     text-align: center;  
  16. }  
  17. form{  /* 表单居中  */  
  18.     width:300px;  
  19.     height:220px;  
  20.     margin: 100px auto;  
  21. }  
  22. .item{    /* item与itemV的设定纯粹是为了让出入框左对齐,名字右对齐    */  
  23.     width:80px;  
  24.     display:inline-block;  
  25.     text-align: right;  
  26. }  
  27. .itemV{  
  28.     width:180px;  
  29.     display:inline-block;  
  30.     text-align: right;  
  31. }  
  32. #btn{    /* 按钮居中  */  
  33.     margin-left: 80px;  
  34. }  
  35. </style>  
  36. </head>  
  37. <body>  
  38.     <h1>欢迎登录学生管理系统!</h1>  
  39.       
  40.     <form action="loginServlet" method="post">  
  41.     <span class="item">用户名:</span>   
  42.     <input type="text" name="username" /></span><br><br>  
  43.     <span class="item">密码:</span>   
  44.     <input type="text" name="password" /></span><br><br><br>  
  45.     <div id="btn">   
  46.     <input type="submit" value="登录" />   
  47.     <input type="reset" value="重置" />  
  48.     </div>  
  49.     </form>     
  50. </body>  
  51. </html>  

welcome.jsp

[html]  view plain   copy
  在CODE上查看代码片 派生到我的代码片
  1. <%@ page language="java" contentType="text/html; charset=utf-8"  
  2.     pageEncoding="utf-8"%>  
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
  4. <html>  
  5. <head>  
  6. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">  
  7. <title>Insert title here</title>  
  8. <style>  
  9. body {  
  10.     background-image:url('images/greenbg.png');  
  11.     background-size: cover;  
  12. }  
  13. h1{  
  14.     margin:150px auto;  
  15.     text-align: center;  
  16. }  
  17. span{  
  18.     color:red;  
  19.     font-family:fantasy;  
  20. }  
  21. </style>  
  22. </head>  
  23. <body>  
  24.     <h1>登录成功,欢迎<span><%=request.getAttribute("username") %></span>来到学生管理系统!</h1>  
  25.     <center><p>5秒钟后系统会自动跳转到查看全部学生信息页面...</p></center>  
  26.       
  27.     <%response.setHeader("refresh","5;url=findAllServlet"); %>  
  28. </body>  
  29. </html>  


error.jsp

[html]  view plain   copy
  在CODE上查看代码片 派生到我的代码片
  1. <%@ page language="java" contentType="text/html; charset=utf-8"  
  2.     pageEncoding="utf-8"%>  
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
  4. <html>  
  5. <head>  
  6. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">  
  7. <title>Insert title here</title>  
  8. <style>  
  9. body {  
  10.     background-image:url('images/yellowbg.png');  
  11.     background-size: cover;  
  12. }  
  13. h1{  
  14.     margin:150px auto;  
  15.     text-align: center;  
  16. }  
  17. span{  
  18.     color:red;  
  19.     font-family:fantasy;  
  20. }  
  21. </style>  
  22. </head>  
  23. <body>  
  24.     <h1>登录失败,<span><%=request.getAttribute("username") %></span>的用户名或者密码错误!</h1>  
  25.     <center><p>5秒钟后系统会自动跳转到登录页面...</p></center>  
  26.       
  27.     <%response.setHeader("refresh","5;url=login.jsp"); %>  
  28. </body>  
  29. </html>  

listStudent.jsp

[html]  view plain   copy
  在CODE上查看代码片 派生到我的代码片
  1. <%@ page language="java" contentType="text/html; charset=utf-8"  
  2.     pageEncoding="utf-8"%>  
  3. <%@page import="java.util.List" %>  
  4. <%@page import="com.bean.Student" %>  
  5. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
  6. <html>  
  7. <head>  
  8. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">  
  9. <title>Insert title here</title>  
  10. <style>  
  11. body {  
  12.     background-image:url('images/greenbg.png');  
  13.     background-size: cover;  /* 图片平铺拉伸,适应屏幕 */  
  14. }  
  15. h1{  
  16.     margin:50px auto;  
  17.     text-align: center;  
  18. }  
  19.   
  20. table {  
  21.     width:600px;  
  22.     margin:50px auto;  
  23.     border-collapse: collapse;  
  24.     text-align: center;  
  25. }  
  26.   
  27. table,th,td{border:1px solid black;}  
  28. th{height:50px;}  
  29.   
  30. a:link {color:black;}      /* 未访问链接*/  
  31. a:visited {color:black;}   
  32. a:hover {color:#f00;}  /* 鼠标移动到链接上 */  
  33. a:active {color:#f60;}  /* 已点击 */  
  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值