1.设置工作空间编码,在Window -> preferences 然后搜索encoding 把所有编码都改成UTF-8。
2.选择导入的外部jar要到你的tomcat安装目录下的bin目录下选择servlet-api.jar和jsp-api.jar
3.从客户端的浏览器中的网页里向服务端发起请求有几种方式?
1_通过表单发起请求
2_通过链接发起请求
3_通过Ajax发起请求
4_通过JavaScript发起请求 location.href=””;
<html>
<html> <head> <meta charset="UTF-8"> <title>向服务端发起请求</title> <script type="text/javascript" src="${pageContext.request.contextPath}/js/jquery.min.js"></script> </head> <body> <h1>版本</h1> <hr/> <form method="post" action="${pageContext.request.contextPath}/StuServlet?method=addStu"> 输入姓名:<input type="text" name="stuName"/><button>提交</button> </form> <hr/> <a href="${pageContext.request.contextPath}/StuServlet?method=delStu&stuId=s001">刪除學生</a> <hr/> <button id="btn01">利用AJAX向服務器端發起請求</button> <hr/> <button id="btn02">利用JAVAScript向服務器端發起請求</button> </body> <script> $(function(){ $("#btn01").click(function() { $.post("/TestBaseServlet/StuServlet",{"method":"updateStu","stuId":"s001"},function(data){ console.log(data); }); }); $("#btn02").click(function(){ location.href="/TestBaseServlet/StuServlet?method=findStu"; }); }); </script> </html>
抽取工具类BaseServlet
版本(一)
1 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 2 String md = request.getParameter("method"); 3 //定义一个path变量,存储当前功能执行完毕之后一个转发的路径 4 String path=null; 5 if(null!=md) { 6 if("addStu".equals(md)) { 7 path=addStu(request, response); 8 }else if("delStu".equals(md)) { 9 path=delStu(request, response); 10 }else if("updateStu".equals(md)) { 11 path=updateStu(request, response); 12 }else if("findStu".equals(md)) { 13 path=findStu(request, response); 14 }if(null!=path) { 15 request.getRequestDispatcher(path).forward(request, response); 16 } 17 } 18 } 19 protected String addStu(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 20 System.out.println("執行服務端添加學生功能"); 21 return "/success.jsp"; 22 } 23 protected String delStu(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 24 System.out.println("執行服務端删除學生功能"); 25 return "/success.jsp"; 26 } 27 protected String updateStu(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 28 System.out.println("執行服務端更新學生功能"); 29 response.getWriter().println("updateStuOK"); 30 return null; 31 } 32 protected String findStu(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 33 System.out.println("執行服務端查找學生功能"); 34 return "/success.jsp"; 35 }
版本(二)
1 protected void doGet(HttpServletRequest request, HttpServletResponse response) 2 throws ServletException, IOException { 3 String md = request.getParameter("method"); 4 // 定义一个path变量,存储当前功能执行完毕之后一个转发的路径 5 String path = null; 6 if (null != md) { 7 //查看当前的模块中是否有对应的方法,方法的名称和md中的内容保持一致,方法的参数HttpSetvletRequest,HttpSetvletResponse 8 Class clazz = this.getClass(); 9 try { 10 Method method = clazz.getMethod(md, HttpServletRequest.class,HttpServletResponse.class); 11 if(null!=method) { 12 //找到了,执行方法 13 path = (String) method.invoke(this, request,response); 14 } 15 //进行统一的转发 16 if (null != path) { 17 request.getRequestDispatcher(path).forward(request, response); 18 } 19 } catch (Exception e) { 20 e.printStackTrace(); 21 } 22 } 23 } 24 25 public String addStu(HttpServletRequest request, HttpServletResponse response) 26 throws ServletException, IOException { 27 System.out.println("執行服務端添加學生功能"); 28 return "/success.jsp"; 29 } 30 31 public String delStu(HttpServletRequest request, HttpServletResponse response) 32 throws ServletException, IOException { 33 System.out.println("執行服務端删除學生功能"); 34 return "/success.jsp"; 35 } 36 37 public String updateStu(HttpServletRequest request, HttpServletResponse response) 38 throws ServletException, IOException { 39 System.out.println("執行服務端更新學生功能"); 40 response.getWriter().println("updateStuOK"); 41 return null; 42 } 43 44 public String findStu(HttpServletRequest request, HttpServletResponse response) 45 throws ServletException, IOException { 46 System.out.println("執行服務端查找學生功能"); 47 return "/success.jsp"; 48 }
版本(三)BaseServlet
1 package org.wl.web.servlet; 2 3 import java.io.IOException; 4 import java.lang.reflect.Method; 5 import javax.servlet.ServletException; 6 import javax.servlet.http.HttpServlet; 7 import javax.servlet.http.HttpServletRequest; 8 import javax.servlet.http.HttpServletResponse; 9 10 public class BaseServlet extends HttpServlet { 11 12 @Override 13 public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 14 this.doPost(req, resp); 15 } 16 17 @Override 18 public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 19 20 // localhost:8080/store/productServlet?method=addProduct 21 String method = req.getParameter("method"); 22 23 if (null == method || "".equals(method) || method.trim().equals("")) { 24 method = "execute"; 25 } 26 27 // 注意:此处的this代表的是子类的对象 28 // System.out.println(this); 29 // 子类对象字节码对象 30 Class clazz = this.getClass(); 31 32 try { 33 // 查找子类对象对应的字节码中的名称为method的方法.这个方法的参数类型是:HttpServletRequest.class,HttpServletResponse.class 34 Method md = clazz.getMethod(method, HttpServletRequest.class, HttpServletResponse.class); 35 if (null != md) { 36 String jspPath = (String) md.invoke(this, req, resp); 37 if (null != jspPath) { 38 req.getRequestDispatcher(jspPath).forward(req, resp); 39 } 40 } 41 } catch (Exception e) { 42 e.printStackTrace(); 43 } 44 } 45 46 // 默认方法 47 public String execute(HttpServletRequest req, HttpServletResponse resp) throws Exception { 48 return null; 49 } 50 }
public class StuServlet03 extends BaseServlet{ private static final long serialVersionUID = 1L; public String addStu(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("執行服務端添加學生功能"); return "/success.jsp"; } public String delStu(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("執行服務端删除學生功能"); return "/success.jsp"; } public String updateStu(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("執行服務端更新學生功能"); response.getWriter().println("updateStuOK"); return null; } public String findStu(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("執行服務端查找學生功能"); return "/success.jsp"; } }
数据库
教学资料 t_doc(docId,docName,原始上传文件的名称,真实的文件名称,上传时间)
教学视频t_vedio
(vedioId,视频名称,视频简介,视频真实的名称,视频上传时的文件名称,上传时间)
试题t_exam ( examId,试题名称,试题真实名称,试题上传时的名称,上传时间)
师表t_tea(teaId,teaName,teaSex,teaAge,teaLoginName,loginPwd)
管理员表t_admin(adminId,loginName,loginPwd)
学生t_stu(stuId , 学号,密码)
师生交流 t_message(msgId,提问的内容,提问时间,回复内容,回复时间,stuId)
注意1:t_message表下的列stuId是外键列,参照了t_stu表中的主键列stuId
注意2:详细的建表语句参见资料下的db.sql
CREATE TABLE `t_admin` ( `userId` INT(11) NOT NULL AUTO_INCREMENT, #管理员的编号 `userName` VARCHAR(66) DEFAULT NULL, #管理员的登录名 `userPw` VARCHAR(55) DEFAULT NULL, #管理员的密码 PRIMARY KEY (`userId`) ) ENGINE=INNODB DEFAULT CHARSET=utf8; INSERT INTO `t_admin` VALUES ('1', '001', '123'); INSERT INTO `t_admin` VALUES ('2', '002', '123'); INSERT INTO `t_admin` VALUES ('3', '003', '123'); DROP TABLE IF EXISTS `t_doc`; CREATE TABLE `t_doc` ( `id` INT(11) NOT NULL AUTO_INCREMENT, #资料编号 `name` VARCHAR(66) DEFAULT NULL, #资料描述 `attachment` VARCHAR(50) DEFAULT NULL, #资料在目录中的真实名称 `attachmentOldName` VARCHAR(55) DEFAULT NULL,#资料的原始名称 `uploadTime` VARCHAR(50) DEFAULT NULL, #是否删除 `del` VARCHAR(50) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=INNODB DEFAULT CHARSET=utf8; INSERT INTO `t_doc` VALUES (NULL, 'Servlet-技术1', '111111111111.doc', 'day01_课堂笔记1.docx', '2016-05-19', 'no'); INSERT INTO `t_doc` VALUES (NULL, 'Servlet-技术2', '111111111112.doc', 'day01_课堂笔记2.docx', '2016-05-20', 'no'); INSERT INTO `t_doc` VALUES (NULL, 'Servlet-技术3', '111111111113.doc', 'day01_课堂笔记3.docx', '2016-05-21', 'no'); INSERT INTO `t_doc` VALUES (NULL, 'Servlet-技术4', '111111111114.doc', 'day01_课堂笔记4.docx', '2016-05-22', 'no'); INSERT INTO `t_doc` VALUES (NULL, 'Servlet-技术5', '111111111115.doc', 'day01_课堂笔记5.docx', '2016-05-23', 'no'); INSERT INTO `t_doc` VALUES (NULL, 'Servlet-技术6', '111111111116.doc', 'day01_课堂笔记6.docx', '2016-05-24', 'no'); INSERT INTO `t_doc` VALUES (NULL, 'Servlet-技术7', '111111111117.doc', 'day01_课堂笔记7.docx', '2016-05-25', 'no'); INSERT INTO `t_doc` VALUES (NULL, 'Servlet-技术8', '111111111118.doc', 'day01_课堂笔记8.docx', '2016-05-26', 'no'); DROP TABLE IF EXISTS `t_vedio`; CREATE TABLE `t_vedio` ( `vedioId` INT(11) NOT NULL AUTO_INCREMENT, #视频编号 `vedioName` VARCHAR(66) DEFAULT NULL, #视频简介 `vedioPro` VARCHAR(2000) DEFAULT NULL, #视频描述 `vedioAttachment` VARCHAR(55) DEFAULT NULL, #视频在目录下的真实名称 `attachmentOldName` VARCHAR(2000) DEFAULT NULL, #视频原始名称 `uploadTime` VARCHAR(50) DEFAULT NULL, #视频的上传时间 `del` VARCHAR(50) DEFAULT NULL, #视频是否删除 PRIMARY KEY (`vedioId`) ) ENGINE=INNODB DEFAULT CHARSET=utf8; insert into `t_vedio`(`vedioId`,`vedioName`,`vedioPro`,`vedioAttachment`,`attachmentOldName`,`uploadTime`,`del`) values (1,'jdk8新特性01','黑马培训XX老师JDK8新特性1','11111111111111111111.mp4','黑马程序员_XXJDK8新特性1.avi','2016-05-19','no'),(2,'jdk8新特性02','黑马培训XX老师JDK8新特性2','11111111111111111111.mp4','黑马程序员_XXJDK8新特性2.avi','2016-05-20','no'),(3,'jdk8新特性03','黑马培训XX老师JDK8新特性3','11111111111111111111.mp4','黑马程序员_XXJDK8新特性3.avi','2016-05-21','no'),(4,'jdk8新特性04','黑马培训XX老师JDK8新特性4','11111111111111111111.mp4','黑马程序员_XXJDK8新特性4.avi','2016-05-22','no'),(5,'jdk8新特性05','黑马培训XX老师JDK8新特性5','11111111111111111111.mp4','黑马程序员_XXJDK8新特性5.avi','2016-05-23','no'),(6,'jdk8新特性06','黑马培训XX老师JDK8新特性6','11111111111111111111.mp4','黑马程序员_XXJDK8新特性6.avi','2016-05-24','no'),(7,'jdk8新特性07','黑马培训XX老师JDK8新特性7','11111111111111111111.mp4','黑马程序员_XXJDK8新特性1.avi','2016-05-25','no'),(8,'jdk8新特性07','黑马培训XX老师JDK8新特性8','11111111111111111111.mp4','黑马程序员_XXJDK8新特性8.avi','2016-05-25','no'),(9,'jdk8新特性07','黑马培训XX老师JDK8新特性9','11111111111111111111.mp4','黑马程序员_XXJDK8新特性9.avi','2016-05-25','no'),(10,'jdk8新特性07','黑马培训XX老师JDK8新特性10','11111111111111111111.mp4','黑马程序员_XXJDK8新特性10.avi','2016-05-25','no'),(11,'jdk8新特性07','黑马培训XX老师JDK8新特性11','11111111111111111111.mp4','黑马程序员_XXJDK8新特性11.avi','2016-05-25','no'),(12,'jdk8新特性07','黑马培训XX老师JDK8新特性12','11111111111111111111.mp4','黑马程序员_XXJDK8新特性12.avi','2016-05-25','no'),(13,'jdk8新特性07','黑马培训XX老师JDK8新特性13','11111111111111111111.mp4','黑马程序员_XXJDK8新特性13.avi','2016-05-25','no'),(14,'jdk8新特性07','黑马培训XX老师JDK8新特性14','11111111111111111111.mp4','黑马程序员_XXJDK8新特性14.avi','2016-05-25','no'),(15,'jdk8新特性07','黑马培训XX老师JDK8新特性15','11111111111111111111.mp4','黑马程序员_XXJDK8新特性15.avi','2016-05-25','no'),(16,'jdk8新特性07','黑马培训XX老师JDK8新特性16','11111111111111111111.mp4','黑马程序员_XXJDK8新特性16.avi','2016-05-25','no'),(17,'jdk8新特性07','黑马培训XX老师JDK8新特性17','11111111111111111111.mp4','黑马程序员_XXJDK8新特性16.avi','2016-05-25','no'),(18,'jdk8新特性07','黑马培训XX老师JDK8新特性18','11111111111111111111.mp4','黑马程序员_XXJDK8新特性16.avi','2016-05-25','no'),(19,'jdk8新特性07','黑马培训XX老师JDK8新特性19','11111111111111111111.mp4','黑马程序员_XXJDK8新特性16.avi','2016-05-25','no'),(20,'jdk8新特性07','黑马培训XX老师JDK8新特性20','11111111111111111111.mp4','黑马程序员_XXJDK8新特性16.avi','2016-05-25','no'),(21,'jdk8新特性07','黑马培训XX老师JDK8新特性21','11111111111111111111.mp4','黑马程序员_XXJDK8新特性16.avi','2016-05-25','no'),(22,'jdk8新特性07','黑马培训XX老师JDK8新特性22','11111111111111111111.mp4','黑马程序员_XXJDK8新特性16.avi','2016-05-25','no'); DROP TABLE IF EXISTS `t_exam`; CREATE TABLE `t_exam` ( `examId` INT(11) NOT NULL AUTO_INCREMENT, #试题编号 `examName` VARCHAR(66) DEFAULT NULL, #试题名称 `attachment` VARCHAR(55) DEFAULT NULL, #试题在目录下的真实名称 `attachmentOldName` VARCHAR(50) DEFAULT NULL,#试题的原始名称 `uploadTime` VARCHAR(50) DEFAULT NULL, #试题的上传时间 `del` VARCHAR(50) DEFAULT NULL, #是否删除 PRIMARY KEY (`examId`) ) ENGINE=INNODB DEFAULT CHARSET=utf8; INSERT INTO `t_exam` VALUES (NULL, 'java基础测试1', '1111111111111.doc', 'java基础测试题1.doc', '2016-05-19', 'no'); INSERT INTO `t_exam` VALUES (NULL, 'java基础测试2', '1111111111112.doc', 'java基础测试题2.doc', '2016-05-20', 'no'); INSERT INTO `t_exam` VALUES (NULL, 'java基础测试3', '1111111111113.doc', 'java基础测试题3.doc', '2016-05-21', 'no'); INSERT INTO `t_exam` VALUES (NULL, 'java基础测试4', '1111111111114.doc', 'java基础测试题4.doc', '2016-05-22', 'no'); INSERT INTO `t_exam` VALUES (NULL, 'java基础测试5', '1111111111115.doc', 'java基础测试题5.doc', '2016-05-23', 'no'); INSERT INTO `t_exam` VALUES (NULL, 'java基础测试6', '1111111111116.doc', 'java基础测试题6.doc', '2016-05-24', 'no'); DROP TABLE IF EXISTS `t_stu`; CREATE TABLE `t_stu` ( `stuId` INT(11) NOT NULL AUTO_INCREMENT, #学生编号 `stuNum` VARCHAR(66) DEFAULT NULL, #学生学号 `stuRealname` VARCHAR(50) DEFAULT NULL, #学生真实姓名 `stuSex` VARCHAR(50) DEFAULT NULL, #学生性别 `stuAge` VARCHAR(55) DEFAULT NULL, #学生年龄 `loginPw` VARCHAR(50) DEFAULT NULL, #学生登录密码 `status` VARCHAR(50) DEFAULT NULL, #学生是否毕业 y:已经毕业 n:未毕业 `del` VARCHAR(50) DEFAULT NULL, #学生是否删除 PRIMARY KEY (`stuId`) ) ENGINE=INNODB DEFAULT CHARSET=utf8; INSERT INTO t_stu VALUES (NULL,'2016001','tom','男','21','123456','y','no'); INSERT INTO t_stu VALUES (NULL,'2016001','mary','女','21','123456','y','no'); INSERT INTO t_stu VALUES (NULL,'2016001','lucy','女','22','123456','y','no'); INSERT INTO t_stu VALUES (NULL,'2016001','jack','男','22','123456','y','no'); INSERT INTO t_stu VALUES (NULL,'2016001','lily','女','23','123456','y','no'); INSERT INTO t_stu VALUES (NULL,'2016001','smith','男','23','123456','y','no'); INSERT INTO t_stu VALUES (NULL,'2016001','piter','男','21','123456','y','no'); SELECT * FROM t_stu; DROP TABLE IF EXISTS `t_message`; CREATE TABLE `t_message` ( `messageId` INT(11) NOT NULL AUTO_INCREMENT, #留言编号 `content` VARCHAR(2000) DEFAULT NULL, #留言内容 `leveWordTime` VARCHAR(55) DEFAULT NULL, #留言时间 `stuId` INT(11) DEFAULT NULL, #留言学生的编号 `replay` VARCHAR(2000) DEFAULT NULL, #回复内容 `replayTime` VARCHAR(50) DEFAULT NULL, #回复时间 PRIMARY KEY (`messageId`) ) ENGINE=INNODB DEFAULT CHARSET=utf8; ALTER TABLE t_message ADD CONSTRAINT FK_ID FOREIGN KEY(stuId) REFERENCES t_stu(stuId); INSERT INTO `t_message` VALUES (NULL, 'Javase中创建线程的方式1','2016-04-27','1','继承thread,实现Runnable接口,实现callback11','2016-04-27'); INSERT INTO `t_message` VALUES (NULL, 'Javase中创建线程的方式2','2016-04-26','2','继承thread,实现Runnable接口,实现callback22','2016-04-28'); INSERT INTO `t_message` VALUES (NULL, 'Javase中创建线程的方式3','2016-04-25','1','继承thread,实现Runnable接口,实现callback33','2016-04-29'); INSERT INTO `t_message` VALUES (NULL, 'Javase中创建线程的方式4','2016-04-24','2','继承thread,实现Runnable接口,实现callback44','2016-05-30'); INSERT INTO `t_message` VALUES (NULL, 'Javase中创建线程的方式5','2016-04-23','1','继承thread,实现Runnable接口,实现callback55','2016-05-01'); INSERT INTO `t_message` VALUES (NULL, 'Javase中创建线程的方式6','2016-04-22','2','继承thread,实现Runnable接口,实现callback66','2016-05-02'); INSERT INTO `t_message` VALUES (NULL, 'Javase中创建线程的方式7','2016-04-21','1','继承thread,实现Runnable接口,实现callback77','2016-05-03'); INSERT INTO `t_message` VALUES (NULL, 'Javase中创建线程的方式8','2016-04-21','1','继承thread,实现Runnable接口,实现callback77','2016-05-03'); INSERT INTO `t_message` VALUES (NULL, 'Javase中创建线程的方式9','2016-04-21','1','继承thread,实现Runnable接口,实现callback77','2016-05-02'); INSERT INTO `t_message` VALUES (NULL, 'Javase中创建线程的方式10','2016-04-21','1','继承thread,实现Runnable接口,实现callback77','2016-05-01'); INSERT INTO `t_message` VALUES (NULL, 'Javase中创建线程的方式11','2016-04-21','1','继承thread,实现Runnable接口,实现callback77','2016-05-03'); INSERT INTO `t_message` VALUES (NULL, 'Javase中创建线程的方式12','2016-04-21','1','继承thread,实现Runnable接口,实现callback77','2016-05-04'); INSERT INTO `t_message` VALUES (NULL, 'Javase中创建线程的方式13','2016-04-21','1','继承thread,实现Runnable接口,实现callback77','2016-05-05'); INSERT INTO `t_message` VALUES (NULL, 'Javase中创建线程的方式14','2016-04-21','1','继承thread,实现Runnable接口,实现callback77','2016-05-06'); INSERT INTO `t_message` VALUES (NULL, 'Javase中创建线程的方式15','2016-04-21','1','继承thread,实现Runnable接口,实现callback77','2016-05-07'); INSERT INTO `t_message` VALUES (NULL, 'Javase中创建线程的方式16','2016-04-21','1','继承thread,实现Runnable接口,实现callback77','2016-05-05'); INSERT INTO `t_message` VALUES (NULL, 'Javase中创建线程的方式17','2016-04-21','1','继承thread,实现Runnable接口,实现callback77','2016-05-06'); INSERT INTO `t_message` VALUES (NULL, 'Javase中创建线程的方式18','2016-04-21','1','继承thread,实现Runnable接口,实现callback77','2016-05-07'); INSERT INTO `t_message` VALUES (NULL, 'Javase中创建线程的方式19','2016-04-21','1','继承thread,实现Runnable接口,实现callback77','2016-05-04'); INSERT INTO `t_message` VALUES (NULL, 'Javase中创建线程的方式20','2016-04-21','1','继承thread,实现Runnable接口,实现callback77','2016-05-06'); INSERT INTO `t_message` VALUES (NULL, 'Javase中创建线程的方式21','2016-04-21','1','继承thread,实现Runnable接口,实现callback77','2016-05-08'); INSERT INTO `t_message` VALUES (NULL, 'Javase中创建线程的方式22','2016-04-21','1','继承thread,实现Runnable接口,实现callback77','2016-05-08'); INSERT INTO `t_message` VALUES (NULL, 'Javase中创建线程的方式23','2016-04-21','1','继承thread,实现Runnable接口,实现callback77','2016-05-09');
开发环境搭建
1_新建立项目
2_导入JAR包
mysql驱动包: 负责连接MYSQL数据库
jstl: 在JSP页面中引入JSP标签库
fileupload: 上传的时候使用
dbutils: 操作数据仓库使用,对JDBC的封装
c3p0:连接池工具
beanutils: 封装数据
3_建立各个包
BaseServlet: 让所有的Servlet都继承BaseServlet
EncodingFitler:解决乱码问题过滤器
<filter> <filter-name>EncodingFilter</filter-name> <filter-class>com.byndjf.web.filter.EncodingFilter</filter-class> </filter> <filter-mapping> <filter-name>EncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
5_导入HTML页面
实际开发页面HTML__>导入预先编写好的JSP页面
6_配置项目首页
<welcome-file-list> <!-- 配置系统首页 --> <welcome-file>/site/index.jsp</welcome-file> </welcome-file-list>
7_异步校验用户名是否存在(重点)
注意:
*_分析思路
*_实现功能的流程
*_出错情况下如何排除错误
*_分析功能思路
*_步骤实现:
1_准备工作
确定SQL语句编写
SELECT * FROM t_stu WHERE stuNum='2016001'
确定页面效果:
<span style="color:red">用户名已经存在</span>
2_页面加载完毕之后,为输入学号文本框绑定失去焦点事件,向服务端发起ajax请求
$(function(){
//页面加载完毕
//获取id为username的文本框,为其绑定失去焦点事件
$("#username").blur(function(){
//获取到用户输入的学号信息,如果为空提示请录入学号
var um=$("#username").val();
var username=$.trim(um);
if(null!=username&&""!=username){
//向服务端发起ajax请求,将用户录入的学号信息发送到服务端
$.post("/ndjf_system/StuServlet",{"method":"validateUserExist","username":username},function(data){
//打印服务端响应回客户端的数据
console.log(data);
if(data=="yes"){
$("#stuNumMsg").html("");
}
if(data=="no"){
$("#stuNumMsg").html("不存在此户名,请录入合适用户名");
}
});
}else{
//如果为空提示请录入学号
alert("请输入学号");
}
});
//获取id为btnLogin登录按钮,为其绑定点击事件
$("#btnLogin").click(function(){
//校验用户名和密码不能为空
//获取用户名
var username=$("#username").val();
//获取密码
var password=$("#userPw").val();
//截取字符串
var um=$.trim(username);
var up=$.trim(password);
if(null==um||""==um){
alert("请输入合法的学号");
return false;
}
if(null==up||""==up){
alert("请输入合法的密码");
return false;
}
//利用JS实现登录
document.getElementById("fm").submit();
});
});
function logout(){
//用户确认提示
if(confirm("确定要退出吗?")){
location.href="/ndjf_system/StuServlet?method=stuLogout";
}
}
3_实现服务端Servlet
导入C3P0数据文件,修改好配置信息
创建服务端会用到当前模块下的各种给StuServlet,StuService,StuDao,Student
获取到页面提交到服务端的数据学号
调用业务层功能:验证用户是否已经存在,返回学生对象
根据返回的学生是否为空判断仓库中是否有已经存在的账户
4_实现服务端Service
public Student validateUserExist(String um) throws SQLException {
// 调用DAO层功能
StuDao stuDao = new StuDao();
return stuDao.validateUserExist(um);
}
public Student stuLogin(String um, String up) throws SQLException {
// 调用DAO层功能
StuDao stuDao = new StuDao();
return stuDao.stuLogin(um,up);
}
5_实现服务端Dao
public Student validateUserExist(String um) throws SQLException { String sql="SELECT * FROM t_stu WHERE stuNum=?"; QueryRunner qr=new QueryRunner(JDBCUtils.getDataSource()); return qr.query(sql, new BeanHandler<Student>(Student.class),um); } public Student stuLogin(String um, String up) throws SQLException { String sql="select * from t_stu where stuNum= ? and loginPw = ?"; QueryRunner qr = new QueryRunner(JDBCUtils.getDataSource()); return qr.query(sql, new BeanHandler<Student>(Student.class),um,up); }
6_页面部分剩余的AJAX部分
8_用户登录
1_原理分析
2_步骤实现
*_页面端代码
页面端JS数据校验,如果没有录入学号或者密码或者录入空号或密码,不允许登录
设置表单中的action属性值,学号和密码输入框的name属性
*_实现服务端代码
StuServlet
StuService.java
StuDao.java
Index.jsp___>userLogin.jsp
代码详见userLogin.jsp
3_总结
实现流程,分析思路
9_用户退出
*_原理分析
*_步骤实现
1_实现页面端代码
2_实现服务端代码