欢迎来到Jsp编程课时十二——今天实现的目标是。@1将数据库的数据发送到浏览器。@2利用浏览器实现对数据库的增删改查操作。@3理解MVC三层架构的定义。

完成项目会有以下的效果。

JSP第二十课:Mvc+Mysql+Servlect+Jsp实现在浏览器上对数据库的增删改查。

 

MVC全名是Model View Controller,
是 @@1模型(model)
@@2-视图(view)
@@3-控制器(controller)的缩写,
一种软件设计典范,
用一种业务逻辑、数据、界面显示分离的方法组织代码,
将业务逻辑聚集到一个部件里面,在改进和个性化定制界面及用户交互的同时,
不需要重新编写业务逻辑。
MVC被独特的发展起来用于映射传统
的输入、处理和输出功能在一个逻辑
的图形化用户界面的结构中。

第一部分去分析MVC的两张图。

 


NO.one

欢迎来到Jsp编程课时十二——今天实现的目标是。@1将数据库的数据发送到浏览器。@2利用浏览器实现对数据库的增删改查操作。@3理解MVC三层架构的定义。_第1张图片

如果我要在浏览器查找数据库的所有的数据,我改如何操作呢?

首先我们来分析上面的流程图。

第一步。首先你要在jsp中建立一张表格。用于显示数据库的所有数据。

第二步。当你运行项目时。你要想到一个问题。数据是在数据库中,而你的目标是要将数据库的数据在浏览器中显示。首先你要思考用什么方式存放数据。你可以用四大域,也可以用实体类。

第三步:进行流程分解。

@1项目运行经过的是过滤器,过滤器设置编码格式。

response.setContentType("text/html");
		response.setCharacterEncoding("UTF-8");
		request.setCharacterEncoding("UTF-8");
		chain.doFilter(request, response);

@2项目从过滤器出来后经过的是控制器,控制器接到用户发来的查询命令,而控制器完成不了。

于是控制器发送命令发给业务逻辑层中的接口,定义一个查询的方法。接口中定义的方法,要在实体类中实现。

所以逻辑层的接口将查询的命令发送给,业务逻辑层的实体类。而业务逻辑层完成不了查询任务。

于是,业务逻辑层的实体类将任务发送给数据反问层的接口。而数据反问层的接口实现不了查询这个要求。

在此之前你要将数据库连接好。

于是数据访问层的接口发送给数据访问层的实体类进行数据的查询工作。开始连接数据库。一般来说定义一个工具类用于连接数据库和关闭数据库。

	String sql="select * from tb_student2";
		return DBUtil.jt.queryForList(sql);

最后:查询的结果要进行。

@1数据库的数据查询完毕返回到数据访问层的实体类,数据访问层的实体类将结果返回到数据访问层的接口。

@2数据访问层接口将结果返回给逻辑层的实体类。

@3逻辑层的实体类将结果返回到逻辑层的接口。

@4逻辑层的接口结果返回给控制器。

@5控制器将查询的数据库的数据储存起来返回给浏览器。

//将从数据库中查询出来的数据存储至request
		session.setAttribute("students", oList);
		//请求转发跳转页面
		request.getRequestDispatcher("index.jsp").forward(request, response);	

 

NO.two

欢迎来到Jsp编程课时十二——今天实现的目标是。@1将数据库的数据发送到浏览器。@2利用浏览器实现对数据库的增删改查操作。@3理解MVC三层架构的定义。_第2张图片

如果我在数据库增加一条或者几十条数据数据,我要求在浏览器中显示,我该如何去操作呢?

@1进行流程分解。(本人以删除一条数据为例)

用户在数据库中删除了一条数据。数据库将删除一条书籍信息返回给数据访问的实体类。数据访问实体类将删除一条数据信息返回给数据访问接口。

 

@2数据访问层接口将结果返回给逻辑层的实体类。

@3逻辑层的实体类将结果返回到逻辑层的接口。

@4逻辑层的接口结果返回给控制器。

@5控制器将删除一条数据信息返给浏览器。

@6浏览器进行查下操作,核对浏览器中的数据是否删除。

接下来的两个问题留给你们自行思考?

如果我在数据库增,删,改,查,任何一条或者几十条数据数据,我要求在浏览器中显示,我该如何去操作呢?

 如果我在浏览器中的表格中增,删,改,查,任何一条或者几十条数据数据,我要求在数据库中显示,我该如何去操作呢?

代码实操。


欢迎来到Jsp编程课时十二——今天实现的目标是。@1将数据库的数据发送到浏览器。@2利用浏览器实现对数据库的增删改查操作。@3理解MVC三层架构的定义。_第3张图片


上面是数据库的表单。

欢迎来到Jsp编程课时十二——今天实现的目标是。@1将数据库的数据发送到浏览器。@2利用浏览器实现对数据库的增删改查操作。@3理解MVC三层架构的定义。_第4张图片

 上面是项目构造图


欢迎来到Jsp编程课时十二——今天实现的目标是。@1将数据库的数据发送到浏览器。@2利用浏览器实现对数据库的增删改查操作。@3理解MVC三层架构的定义。_第5张图片

 欢迎来到Jsp编程课时十二——今天实现的目标是。@1将数据库的数据发送到浏览器。@2利用浏览器实现对数据库的增删改查操作。@3理解MVC三层架构的定义。_第6张图片


 上面是表格

欢迎来到Jsp编程课时十二——今天实现的目标是。@1将数据库的数据发送到浏览器。@2利用浏览器实现对数据库的增删改查操作。@3理解MVC三层架构的定义。_第7张图片

 

 上面是我增加的四条数据我们到数据库中看一下。

欢迎来到Jsp编程课时十二——今天实现的目标是。@1将数据库的数据发送到浏览器。@2利用浏览器实现对数据库的增删改查操作。@3理解MVC三层架构的定义。_第8张图片

 


欢迎来到Jsp编程课时十二——今天实现的目标是。@1将数据库的数据发送到浏览器。@2利用浏览器实现对数据库的增删改查操作。@3理解MVC三层架构的定义。_第9张图片

 

 

删我删除1001043这一条数据。

欢迎来到Jsp编程课时十二——今天实现的目标是。@1将数据库的数据发送到浏览器。@2利用浏览器实现对数据库的增删改查操作。@3理解MVC三层架构的定义。_第10张图片

欢迎来到Jsp编程课时十二——今天实现的目标是。@1将数据库的数据发送到浏览器。@2利用浏览器实现对数据库的增删改查操作。@3理解MVC三层架构的定义。_第11张图片

 

改。我修改最后2条数据,和第一条数据。

欢迎来到Jsp编程课时十二——今天实现的目标是。@1将数据库的数据发送到浏览器。@2利用浏览器实现对数据库的增删改查操作。@3理解MVC三层架构的定义。_第12张图片

 欢迎来到Jsp编程课时十二——今天实现的目标是。@1将数据库的数据发送到浏览器。@2利用浏览器实现对数据库的增删改查操作。@3理解MVC三层架构的定义。_第13张图片

 

欢迎来到Jsp编程课时十二——今天实现的目标是。@1将数据库的数据发送到浏览器。@2利用浏览器实现对数据库的增删改查操作。@3理解MVC三层架构的定义。_第14张图片

 

实现代码综合区。



	
		
		
		
		
	
	
		
用户名:
用户手机号:
用户QQ号:
用微信号:
用户身份证:

性别:
爱好:打游戏readingbooksleep






上传头像:












手机号:
搜索
留言表单:

返回顶部

返回界面


<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>




增加学生信息


增加学生信息

姓名: 年龄: 性别:

返回界面




	
		
购物车




返回界面

我的购物车

商品 描述 单价(元) 数量
新款春装韩版长款两件套连衣裙女名媛欧根纱长袖裙套装长裙 699 1
新款女装夏修身高腰长款连衣裙女雪纺短袖裙名媛粉色收腰长裙 399 1
新款棉麻衬衫女短袖白色衬衣亚麻纯色百搭宽松上衣 268 2
新款棉麻衬衫女短袖白色衬衣亚麻纯色百搭宽松上衣 268 2
新款棉麻衬衫女短袖白色衬衣亚麻纯色百搭宽松上衣 268 2
新款棉麻衬衫女短袖白色衬衣亚麻纯色百搭宽松上衣 268 2
删除选中 结算
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>




学生信息对话列表





	
	
学生信息列表
学号 姓名 年龄 性别 操作
${stu.id} ${stu.name} ${stu.age} ${stu.sex}      

MVC全名是Model View Controller,
是 @@1模型(model)
@@2-视图(view)
@@3-控制器(controller)的缩写,
一种软件设计典范,
用一种业务逻辑、数据、界面显示分离的方法组织代码,
将业务逻辑聚集到一个部件里面,在改进和个性化定制界面及用户交互的同时,
不需要重新编写业务逻辑。
MVC被独特的发展起来用于映射传统
的输入、处理和输出功能在一个逻辑
的图形化用户界面的结构中。

返回顶部

返回界面


 

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>





登录页面


欢迎来到2021年个人学生信息管理登录界面

用户名:
QQ号:
密码:
记住密码   自动登录

返回界面

返回界面

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>





注册页面


欢迎来到2021年个人学生注册界面

用户名:  
输入性别   男: 女:
QQ号:  
密码:  
手机号:  
家庭地址:  

可通过手机号找回密码

同意开通QQ空间
我已阅读并同意相关服务条款和隐私政策开始进行登录QQ页面

这是QQ的注册页面

返回界面

返回顶部







JavaScript getTime()

JavaScript 中的内部时钟从 1970 年 1 月 1 日午夜开始计算。

getTime() 函数返回从那时起的毫秒数:


JavaScript getFullYear()

getFullYear() 方法返回日期的完整年:


JavaScript getMonth()

getMonth()方法以 0 到 11 之间的数字返回日期的月份。

要获得正确的月份,您必须添加 1:


JavaScript getDate()

getDate() 方法以数字(1-31)返回日期的日:


JavaScript getHours()

getHours() 方法以数字(0-23)返回日期的小时:


JavaScript getMinutes()

getMinutes() 方法以数字(0-59)返回日期的分钟:


JavaScript getSeconds()

getSeconds() 方法以数字(0-59)返回日期的秒:


JavaScript getMilliseconds()

getMilliseconds() 方法以数字(0-999)的形式返回日期的毫秒数:


JavaScript getDay()

getDay() 方法将周名作为数字返回:


JavaScript getDay()

getDay() 方法将周名作为数字返回:

您可以使用数组来显示星期的名称:


<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>




修改学生


	

更新学生信息

姓名:
年龄:

返回界面

返回顶部

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>




Insert title here




欢迎来到学生信息管理注册登录界面


点击这里进行学生信息注册页面


点击这里进入学生图书管理页面


点击这里进入一些html5的回顾

当前的获取方式

返回顶部

返回界面

@2后端代码区第一部分

欢迎来到Jsp编程课时十二——今天实现的目标是。@1将数据库的数据发送到浏览器。@2利用浏览器实现对数据库的增删改查操作。@3理解MVC三层架构的定义。_第15张图片

 

package com.DengLu.And.Zhu.Ce;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Random;

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

/**
 * Servlet implementation class ServlectDengLuLogic
 */
@WebServlet("/ServlectDengLuLogic")
public class ServlectDengLuLogic extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ServlectDengLuLogic() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}
	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		/**
		 * do--post
		 */
		 response.setContentType("text/html");
		 response.setCharacterEncoding("UTF-8");
		ServletContext  sc=getServletContext();
		//获取web.xml文件中的镜头数据
		String a=sc.getInitParameter("QQ");
		String b=sc.getInitParameter("PSD");
		String c=sc.getInitParameter("USER");
		
		System.out.println(a+"@@@@@@@@"+b);
		 response.getWriter().append("ServletB获得静态数据用来访问web.xml"+a);
		
		 String aa=request.getParameter("qq");
		 String bb=request.getParameter("psd");
		 String cc=request.getParameter("user");


		  //不区分大小写equalsIgnoreCase(aa)
		  if(a.equalsIgnoreCase(aa)&&b.equalsIgnoreCase(bb)&&c.equalsIgnoreCase(cc)) {
			  //重定向跳转页面
			  System.out.println("登录成功跳转到2021登录成功");
			  response.sendRedirect("HomeServlet");
				/* response.setHeader("refresh","60;URL=Register.jsp"); */
			  
		  }else {
			  System.out.println("登录失败五秒后返回Register.jsp");
				/* response.sendRedirect("Register.jsp"); */
			  response.setHeader("refresh","3;URL=Register.jsp");
		  }
		
	}

}
package com.DengLu.And.Zhu.Ce;

import java.io.IOException;
import java.io.PrintWriter;

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

/**
 * Servlet implementation class ServlectDengLuLose
 */
@WebServlet("/DengLuChengGong")
public class ServlectDengLuLose extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ServlectDengLuLose() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html");
		response.setCharacterEncoding("utf-8");
		PrintWriter pw = response.getWriter();
		
		pw.append("60秒后回到ZhuCe.jsp");
		response.setHeader("refresh","60;URL=ZhuCe.jsp");
	}

	/**
	 * @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);
	}

}
package com.DengLu.And.Zhu.Ce;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Random;

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

/**
 * Servlet implementation class ServlectDengLuLogic
 */
@WebServlet("/ServlectZhuCe")
public class ServlectZhuCe extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ServlectZhuCe() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}
	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		/**
		 * do--post
		 */
		 response.setContentType("text/html");
		 response.setCharacterEncoding("UTF-8");
		ServletContext  sc=getServletContext();
		//获取web.xml文件中的镜头数据
		String a=sc.getInitParameter("USER");
		String b=sc.getInitParameter("QQ");
		String c=sc.getInitParameter("PSD");
		String d=sc.getInitParameter("SJ");
		String  e=sc.getInitParameter("HOME");
		
		
		
		System.out.println(a+"@@@"+b+"@@@"+c+"@@@"+d+"@@@"+e);
		 response.getWriter().append("

ServlectZhuCe获得静态数据用来访问web.xml

"); String aa=request.getParameter("user"); String bb=request.getParameter("qq"); String cc=request.getParameter("psd"); String dd=request.getParameter("sj"); String ee=request.getParameter("home"); //不区分大小写equalsIgnoreCase(aa) if(a.equalsIgnoreCase(aa)&&b.equalsIgnoreCase(bb)&&c.equalsIgnoreCase(cc)&&d.equalsIgnoreCase(dd)&&e.equalsIgnoreCase(ee)) { //重定向跳转页面 System.out.println("注册成功跳转到2021登录成功"); response.sendRedirect("Login.jsp"); /* response.setHeader("refresh","60;URL=Register.jsp"); */ }else { System.out.println("注册失败五秒后返回Register.jsp"); /* response.sendRedirect("Register.jsp"); */ response.setHeader("refresh","8;URL=ZhuCeLose"); } } }
package com.DengLu.And.Zhu.Ce;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Random;

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

/**
 * Servlet implementation class ServlectDengLuLose
 */
@WebServlet("/ZhuCeLose")
public class ZhuCeLose extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ZhuCeLose() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html");
		response.setHeader("refresh", "5");
		
		
		//设置要求浏览器 
		
		
		/* response.sendRedirect("Servlet1"); */
		int i=(int)(Math.random()*900000+100000);
		int a=(int)(Math.random()*9000+1000);
		int c=(int)(Math.random()*900+100);
		System.out.println("@@@@"+(new Random().nextInt(900000)+100000));
		/*
		 * 设置随机
		 */
		System.out.println(new Random().nextInt(100));
		System.out.println(Math.random());
		
		PrintWriter  pw =response.getWriter();
		pw.print("你的支付宝到账1000万元,需要的码号为:

"+i+"

,打死也不说 \"
\";"); pw.print("你的QQ到账1000万元,需要的码号为:

"+c+"

,打死也不说 \"
\";"); pw.print("你的微信到账1000万元,需要的码号为:"+a+",打死也不说"); pw.print("你的手机支付宝到账1000万"+i+""); pw.append(""); pw.append("

2021你年垮2022年QQ登陆失败的界面

"); pw.append("注册界面失败请重新注册"); response.setHeader("refresh","5;URL=Register.jsp"); pw.append("

点击这里返回注册界面进行重新注册

"); pw.append("如果用户一分钟后没有任何操作则在60秒后调到index.jsp文件"); response.setHeader("refresh","30;URL=index.jsp"); } /** * @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); } }

后端代码第二部分

欢迎来到Jsp编程课时十二——今天实现的目标是。@1将数据库的数据发送到浏览器。@2利用浏览器实现对数据库的增删改查操作。@3理解MVC三层架构的定义。_第16张图片

 


package com.entity;
/**
 * 
 * 图书实体类
 * @author MZFAITHDREAM
 * 2021/10/19
 *
 */
public class Book {
	 private String bookName;  //书名
	 private  String autor;		//作者
	 private  String price;
	 private  String address;//出版社
	 private  String buyTime;   //图书时间
	//生成get set方法
	//null构造方法 创建对象 在创建对象
	 //toString
	public Book() {
		// TODO Auto-generated constructor stub
	}

	public Book(String bookName, String autor, String price, String address, String buyTime) {
		super();
		this.bookName = bookName;
		this.autor = autor;
		this.price = price;
		this.address = address;
		this.buyTime = buyTime;
	}





	public String getBookName() {
		return bookName;
	}
	public void setBookName(String bookName) {
		this.bookName = bookName;
	}
	public String getAutor() {
		return autor;
	}
	public void setAutor(String autor) {
		this.autor = autor;
	}
	public String getPrice() {
		return price;
	}
	public void setPrice(String price) {
		this.price = price;
	}
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
	public String getBuyTime() {
		return buyTime;
	}
	public void setBuyTime(String buyTime) {
		this.buyTime = buyTime;
	}

	
	
	@Override
	public String toString() {
		return "Book [bookName=" + bookName + ", autor=" + autor + ", price=" + price + ", address=" + address
				+ ", buyTime=" + buyTime + "]";
	}
	
	
	
	 
	
	

}
package com.Filter;

import java.io.IOException;
import java.io.OutputStream;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

public class MyFilter4 implements Filter{
	


	public MyFilter4() {
		// TODO Auto-generated constructor
		System.out.println("我创建了第一个MyFilter过滤器");
	}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
			throws IOException, ServletException {
			response.setContentType("text/html");
			response.setCharacterEncoding("UTF-8");	
			System.out.println("我是myfilter开始拦截请求地址哦哦");
		
	}

	@Override
	public void init(FilterConfig config) throws ServletException {
		// TODO Auto-generated method stub

		
		System.out.println("FilterConfig用来加载初始化数据");
	}
	
	@Override
	public void destroy() {
		System.out.println("Filter所有方法运行完毕后销毁过滤器");
		
		
	}

}
package com.Listener;
/**
 * 第三类接口
 */
import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionBindingListener;

public class Listener  implements   HttpSessionBindingListener {
	 	private int id;
	    private String username;
	    private String password;
	    public Listener() {
			// TODO Auto-generated constructor stub
		}
	    
	
	
	public Listener(int id, String username, String password) {
			super();
			this.id = id;
			this.username = username;
			this.password = password;
		}



	public int getId() {
		return id;
	}



	public void setId(int id) {
		this.id = id;
	}



	public String getUsername() {
		return username;
	}



	public void setUsername(String username) {
		this.username = username;
	}



	public String getPassword() {
		return password;
	}



	public void setPassword(String password) {
		this.password = password;
	}



	@Override
	public String toString() {
		return "Listener [id=" + id + ", username=" + username + ", password=" + password + ", getId()=" + getId()
				+ ", getUsername()=" + getUsername() + ", getPassword()=" + getPassword() + ", getClass()=" + getClass()
				+ ", hashCode()=" + hashCode() + ", toString()=" + super.toString() + "]";
	}



	@Override
	public void valueBound(HttpSessionBindingEvent event) {
		// TODO Auto-generated method stub
		 System.out.println("HttpSession 与 Listener绑定");
		HttpSessionBindingListener.super.valueBound(event);
	}

	@Override
	public void valueUnbound(HttpSessionBindingEvent event) {
		// TODO Auto-generated method stub
		 System.out.println("HttpSession 与 Listener解绑绑");
		HttpSessionBindingListener.super.valueUnbound(event);
	}
	

}
package com.Servlect;

import java.io.IOException;
import java.io.PrintWriter;

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

/**
 * Servlet implementation class HomeServlect2
 */
@WebServlet("/HomeServlect2")
public class HomeServlect2 extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public HomeServlect2() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		//response.getWriter().append("Served at: ").append(request.getContextPath());
		response.setContentType("text/html");
		response.setCharacterEncoding("UTF-8");
		PrintWriter pw =response.getWriter();
		pw.append("

登录成功

"); pw.append("

本案例运用的对象\r\n" + "
\r\n" + "ServletConfig:对象获得静态数据\r\n" + "
\r\n" + "ServletContext对象:获取共享静态数据,获取共享动态数据,获取共享文件数据.\r\n" + "
\r\n" + "HttpServletResponse 对象HttpServlet调用HttpResponse的有关方法,生成响应数据\r\n" + "
\r\n" + "HttpServletRequest对象:HttpServlet调用HttpResponse的有关方法,生成响应数据,用于服务器上接收浏览器发送过来的数据信息\r\n" + "
\r\n" + "Cookie对象,对象Session对象。程序把每个用户的数据以Cookie/Session的形式写给用户各自的浏览器。\r\n" + "
\r\n" + "Filter:过滤器,阻止非法网站的访问。\r\n" + "
\r\n" + "Listener:\r\n" + "实现了特定接口的类为监听器,用来监听另一个Java类的方法调用或者属性改变;** 当被监听的对象发生了方法调用或者属性改变后,\r\n" + "监听器的对应方法就会立即执行.\r\n" + "
\r\n" + "九大内置对象的核心表格\r\n" + "
\r\n" + "out JspWriter 将文本信息输出浏览器\r\n" + "
\r\n" + "request HttpServletRequest 获得浏览器发送来的数据\r\n" + "
\r\n" + "respone HttpServletRequest 回复浏览器请求\r\n" + "
\r\n" + "seesion HttpSeesion 在不换浏览器放信息\r\n" + "
\r\n" + "exception Throwable 异常报错\r\n" + "
\r\n" + "page Servlect(this) jsp翻译出来类的对象\r\n" + "
\r\n" + "config ServlectConfig 静态数据\r\n" + "
\r\n" + "application ServlectContext 共享数据\r\n" + "
\r\n" + "pageContext PageContextjsp 管理者引出对象\r\n" + "
\r\n" + "seesion 浏览器不切\r\n" + "application 服务器不关闭\r\n" + "request:请求转发跳转页面的情况,只能两个页面共享数据\r\n" + "pageContext:该对象中只能页面自己用\r\n" + "才能用setAttribute的方法\r\n" + "将四作用域排序\r\n" + "pageContext request seesion application \r\n" + "

"); pw.append("

返回主界面

"); } /** * @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); } }

package com.Servlect;

import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;

/**
 * Servlet Filter implementation class MyFilter1
 */
//你要阻止Servlect2
@WebFilter("/Servlect2")

public class MyFilter1 implements Filter {

    /**
     * Default constructor. 
     */
    public MyFilter1() {
        // TODO Auto-generated constructor stub
    	System.out.println("构造方法");
    }

	/**
	 * @see Filter#destroy()
	 */
	public void destroy() {
		// TODO Auto-generated method stub
		System.out.println("销毁数据");
	}

	/**
	 * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
	 */
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
		response.setContentType("text/html");
		response.setCharacterEncoding("UTF-8");
		System.out.println("我是myfilter1开始拦截请求地址哦哦");
		//实施访问
		chain.doFilter(request, response);
	}

	/**
	 * @see Filter#init(FilterConfig)
	 */
	public void init(FilterConfig fConfig) throws ServletException {
		// TODO Auto-generated method stub
		System.out.println("加载数据");
	}

}
package com.Servlect;
/**
 * 这一个类让我们去了解Servlect的周期
 */
import java.io.IOException;

import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

public class ServlectOne  implements Servlet{
	public ServlectOne() {
		// TODO Auto-generated constructor stub
		System.out.println("@1 Servlec方法的构造方法");
	}

	@Override
	public void destroy() {
	System.out.println("@2 Servlect方法的销毁");
		
	}

	@Override
	public ServletConfig getServletConfig() {
		// TODO Auto-generated method stub
		System.out.println("@ 3Servlect方法加载静态数据");
		return null;
	}

	@Override
	public String getServletInfo() {
		// TODO Auto-generated method stub
		System.out.println("@ 4 Servlect的getServletInfo方法");
		return null;
	}

	@Override
	public void init(ServletConfig config) throws ServletException {
		// 获取静态数据的对象
		String a=config.getInitParameter("SFZ");
    	System.out.println(a);
		System.out.println("@5 Servlecto的init方法");
		System.out.println("获取静态数据的对象config");
		//动态数据的获取
		
		
	}


	@Override
	public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
		// TODO Auto-generated method stub
		System.out.println("@5 Servlecto的servlect方法");
	}

}

package com.servlet;

import java.io.IOException;
import java.io.PrintWriter;

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.entity.Book;

/**
 * Servlet implementation class AlertServlet
 */
@WebServlet("/AlertServlet")
public class AlertServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public AlertServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		Book book=(Book) request.getAttribute("book");
		String info="

当前《"+book.getBookName()+"书籍购买成功,如需需要再次购买请点击这里回到购书页面," + "或者10秒之后自动跳转至首页

"; response.setContentType("text/html"); response.setCharacterEncoding("UTF-8"); // response.setHeader("refresh", "1"+"开始倒计时"); //五秒后自动跳转到HomeServlet response.setHeader("refresh", "10;url=ZhuCe.jsp"); PrintWriter pw=response.getWriter(); pw.print(info); } /** * @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); } }
package com.servlet;

import java.io.IOException;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletContext;
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.entity.Book;
import com.util.BooksDBUtil;

/**
 * Servlet implementation class GetBuyBookInfoServlet
 */
@WebServlet("/GetBuyBookInfoServlet")
public class GetBuyBookInfoServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public GetBuyBookInfoServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//获得浏览器发送的下标
		String index=request.getParameter("index");
		int i=Integer.parseInt(index);
		//根据下标从书库中定位到用户要购买的是哪本书
		List oBooks=BooksDBUtil.getBooks();
		Book book=oBooks.get(i);
		//获得当前购买的实时时间
		String time=BooksDBUtil.getNowTime();
		book.setBuyTime(time);
		//存储至ServletContext对象中
		ServletContext sc=getServletContext();
		//先判断ServletContext是否有其他的购买记录
		//如果有,新的购买记录只需要添加到原有的list集合里面
		//如果没有,现在是第一次买书,ServletContext之前没有保存过购买记录,也就意味着该对象中并不存在list集合
		Object object=sc.getAttribute("orders");
		List oList=null;
		if (object==null) {
			//说明从来都没有保存过购买记录
			oList=new ArrayList();
			oList.add(book);
		}else {
			//如果以前有保存过购买记录
			oList=(List) object;
			oList.add(book);
		}
		//将更新后的list集合保存至ServletContext对象
		sc.setAttribute("orders", oList);
		//跳转至一个Servlet页面,告诉用户当前购买成功,如需再次购买请点击这里回到购书页面
		//如果Servlet之间跳转不需要携带数据,传递数据,一般使用重定向
		//否则使用请求转发
		request.setAttribute("book", book);
		request.getRequestDispatcher("AlertServlet").forward(request, response);
	}

	/**
	 * @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);
	}

}
package com.servlet;
/**
 * 浏览数据的主界面
 */
import java.io.IOException;
import java.io.PrintWriter;

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

/**
 * Servlet implementation class HomeServlet
 */
@WebServlet(urlPatterns = "/HomeServletBook",initParams = {
		@WebInitParam(name="buyBook",value="购买图书"),
		@WebInitParam(name="selectOrder",value="查看记录"),
		//增加一个界面为手机管理
		@WebInitParam(name="buyBookA",value="购买图书A ")
})
public class HomeServletBook extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
	String buyBook ="";
	String buyBookA ="";
	String  selectOrder="";
	
    /**
     * @see HttpServlet#HttpServlet()
     */
    public HomeServletBook() {
        super();
        System.out.println("HomeServlet() 在运行主界面中");
        
        // TODO Auto-generated constructor stub
    }

    
    @Override
    public void init(ServletConfig config) throws ServletException {
    	super.init(config);
    	//得到二个静态数据
    	 buyBook =config.getInitParameter("buyBook");
    	 buyBookA =config.getInitParameter("buyBookA");
    	 selectOrder =config.getInitParameter("selectOrder");
    	
    }
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//当浏览器访问Servlet的组成浏览器能看到的页面
		response.setContentType("text/html");
		response.setCharacterEncoding("utf-8");
		PrintWriter pw = response.getWriter();
		
		pw.append("

图书界面

"); pw.append("

"); pw.append(""+ buyBook+"  
"); pw.append(""+selectOrder+"  
"); pw.append("

"); System.out.println(buyBook+selectOrder); } /** * @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); } } -

package com.servlet;

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

import javax.servlet.ServletContext;
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.entity.Book;

/**
 * Servlet implementation class SelectOrderServlect
 */
@WebServlet("/SelectOrderServlect")
public class SelectOrderServlect extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public SelectOrderServlect() {
    	
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		 //从servletContext对象中,获取购买图书,并把图书信息在表格中能看到
		ServletContext sc=getServletContext();
		Object object	= sc.getAttribute("orders");
		response.setContentType("text/html");
		response.setCharacterEncoding("utf-8");
		PrintWriter pw = response.getWriter();
		//进行判断 ifobject为null
		if(object==null) {
			pw.print("查看记录");
			pw.print("

当前没有购买读书信息请购买图书信息!!!!

"); }else { List oBooks=(List) object; pw.append("
"); pw.append(""); pw.append(""); pw.append(""); pw.append(""); pw.append(""); pw.append(""); pw.append(""); pw.append(""); //根据循环遍历list集合,从list集合中获得每一本图书信息 for (int i = 0; i < oBooks.size(); i++) { pw.append(""); pw.append(""); pw.append(""); pw.append(""); pw.append(""); pw.append(""); pw.append(""); } pw.append("
书名作者单价出版社购买时间
"+oBooks.get(i).getBookName()+""+oBooks.get(i).getAutor()+""+oBooks.get(i).getPrice()+""+oBooks.get(i).getAddress()+""+oBooks.get(i).getBuyTime()+"
"); pw.append("
"); } } /** * @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); } }
package com.servlet;

import java.io.IOException;
import java.io.PrintWriter;
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.entity.Book;
import com.util.BooksDBUtil;

/**
 * Servlet implementation class ShowBuyBookServlet
 */
@WebServlet("/ShowBuyBookServlet")
public class ShowBuyBookServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ShowBuyBookServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//从其他类中获取出要显示在浏览器上的所有图书信息
		List oBooks=BooksDBUtil.getBooks();
		response.setContentType("text/html");
		response.setCharacterEncoding("UTF-8");
		PrintWriter pw=response.getWriter();
		pw.append("

手机浏览界面

"); pw.append("
"); pw.append(""); pw.append(""); pw.append(""); pw.append(""); pw.append(""); pw.append(""); pw.append(""); pw.append(""); //根据循环遍历list集合,从list集合中获得每一本图书信息 for (int i = 0; i < oBooks.size(); i++) { pw.append(""); pw.append(""); pw.append(""); pw.append(""); pw.append(""); pw.append(""); pw.append(""); } pw.append("
书名作者 单价出版社操作
"+oBooks.get(i).getBookName()+""+oBooks.get(i).getAutor()+""+oBooks.get(i).getPrice()+""+oBooks.get(i).getAddress()+"" + "
"); pw.append("
"); } /** * @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); } }

后端代码第三部分

欢迎来到Jsp编程课时十二——今天实现的目标是。@1将数据库的数据发送到浏览器。@2利用浏览器实现对数据库的增删改查操作。@3理解MVC三层架构的定义。_第17张图片

 


package com.student.controller;

import java.io.IOException;
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.student.service.IStudentService;
import com.student.service.StudentServiceImp;

/**
 * Servlet implementation class AddStudentServlect
 */
@WebServlet("/AddStudentServlect")
public class AddStudentServlect extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public AddStudentServlect() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.getWriter().append("Served at: ").append(request.getContextPath());
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String name=request.getParameter("name");
		String age=request.getParameter("age");
		
		
		IStudentService service =new StudentServiceImp();
		int i=service.add(name,Integer.parseInt(age));
		if(i>0) {
			System.out.println("增加成功");
			response.sendRedirect("HomeServlet");
			
		}
		
		
		
		
	}

}

package com.student.controller;

import java.io.IOException;
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.student.service.IStudentService;
import com.student.service.StudentServiceImp;

/**
 * Servlet implementation class DeleteStudent
 */
@WebServlet("/DeleteStudent")
public class DeleteStudent extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public DeleteStudent() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
			String id=request.getParameter("id");
			IStudentService service =new StudentServiceImp();
		int i=	service.delete(Integer.parseInt(id));
		
		if(i>0) {
			response.sendRedirect("HomeServlet");
		}
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		
		
		
		
	}

}

 

package com.student.controller;

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

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 javax.servlet.http.HttpSession;

import com.student.service.IStudentService;
import com.student.service.StudentServiceImp;

/**
 * Servlet implementation class HomeServlet
 */
@WebServlet("/HomeServlet")
public class HomeServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public HomeServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//将浏览器的任务接收,发给业务逻辑层处理
		IStudentService service=new StudentServiceImp();
		List> oList=service.getStudents();
		//
		HttpSession session =request.getSession();
		
		//将从数据库中查询出来的数据存储至request
		session.setAttribute("students", oList);
		//请求转发跳转页面
		request.getRequestDispatcher("index.jsp").forward(request, response);	
	}

	/**
	 * @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);
	}

}

package com.student.controller;

import java.io.IOException;
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.student.service.IStudentService;
import com.student.service.StudentServiceImp;

/**
 * Servlet implementation class UpdateStudentServlet
 */
@WebServlet("/UpdateStudentServlet")
public class UpdateStudentServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public UpdateStudentServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.getWriter().append("Served at: ").append(request.getContextPath());
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String id=request.getParameter("id");
		String name=request.getParameter("name");
		String age=request.getParameter("age");
		
		IStudentService service=new StudentServiceImp();
		int i=service.update(Integer.parseInt(id), name, Integer.parseInt(age));
		if (i>0) {
			//修改成功
			response.sendRedirect("HomeServlet");
		}
	}

}

package com.student.dao;
/**
 * 数据访问层操作学生表的接口
 * @author admin
 *
 */

import java.util.List;
import java.util.Map;

public interface IStudentDao {
	
	//接收业务逻辑层的任务,查询出所有学生信息
	List> getStudents();
	
	//接收信息
	int add(String name,int age);
	

	//Dao修改操作
	int update(int id,String name,int age);
	
	
	//删除
	int delete(int id);
	

	
}

 

package com.student.dao;

import java.util.List;
import java.util.Map;

import org.junit.Test;

import com.student.utils.DBUtil;

/**
 * 数据访问操作学生表的实现类
 * @author admin
 *
 */
public class StudentDaoImp implements IStudentDao {

	@Override
	public List> getStudents() {
		String sql="select * from tb_student2";
		return DBUtil.jt.queryForList(sql);
	}

	@Override
	public int add(String name, int age) {
		// TODO Auto-generated method stub
		String sql="insert into tb_student2(name,age) values (?,?)";
		return DBUtil.jt.update(sql,name,age);
	}

	//修改
	@Override
	public int update(int id, String name, int age) {
		String sql="update tb_student2 set name=?,age=? where id=?";
		return DBUtil.jt.update(sql,name,age,id);
	}

	@Override
	public int delete(int id) {
		// TODO Auto-generated method stub
		String sql="delete from tb_student2 where id=?";
		return DBUtil.jt.update(sql,id);
	}
	

	}
	
	
	

package com.student.filter;

import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;

/**
 * 用于拦截所有浏览器请求
 */
@WebFilter("/*")
public class MyFilter implements Filter {

    /**
     * Default constructor. 
     */
    public MyFilter() {
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see Filter#destroy()
	 */
	public void destroy() {
		// TODO Auto-generated method stub
	}

	/**
	 * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
	 */
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
		//设置请求和响应的字符编码格式为UTF-8
		response.setContentType("text/html");
		response.setCharacterEncoding("UTF-8");
		request.setCharacterEncoding("UTF-8");
		chain.doFilter(request, response);
	}

	/**
	 * @see Filter#init(FilterConfig)
	 */
	public void init(FilterConfig fConfig) throws ServletException {
		// TODO Auto-generated method stub
	}

}

package com.student.service;
/**
 * 业务逻辑层处理学生表功能的接口
 * @author admin
 *
 */

import java.util.List;
import java.util.Map;

public interface IStudentService {
	
	//获取数据库中学生表中的所有学生信息
	List> getStudents();
	
	//定义方法接收servlect发送过来的数据. 增删改返回值int类型
	int add (String name,int age);
	
	//准备接收Servlet发送的数据完成修改操作
	int update(int id,String name,int age);
		
	
	//删除
	int delete(int id);
	
	
}
package com.student.service;

import java.util.List;
import java.util.Map;

import com.student.dao.IStudentDao;
import com.student.dao.StudentDaoImp;

/**
 * 业务逻辑层操作学生表功能的实现类
 * @author admin
 *
 */
public class StudentServiceImp implements IStudentService {

	@Override
	public List> getStudents() {
		IStudentDao dao=new StudentDaoImp();
		return dao.getStudents();
	}

	//增加一条数据
	@Override
	public int add(String name, int age) {
		// TODO Auto-generated method stub
		IStudentDao dao=new StudentDaoImp();
		return dao.add(name, age);
	}

	

	//删除数据
	@Override
	public int delete(int id) {
		// TODO Auto-generated method stub
		
		IStudentDao dao=new StudentDaoImp();
		
		return dao.delete(id);
	}

	
	//修改数据的内容
	@Override
	public int update(int id, String name, int age) {
		IStudentDao dao=new StudentDaoImp();
		return dao.update(id, name, age);
	}

	
}

package com.student.utils;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import javax.sql.DataSource;

import org.springframework.jdbc.core.JdbcTemplate;

import com.alibaba.druid.pool.DruidDataSourceFactory;

/**
 * 连接数据库的工具类
 * @author admin
 *
 */
public class DBUtil {
	public static JdbcTemplate jt=null;
	
	//获得druid配置的数据库连接池
	static {
		//使用反射机制获得druid配置文件转换成输入流
		InputStream is=	DBUtil.class.getClassLoader()
			.getResourceAsStream("com/student/utils/druid.properties");
		Properties properties=new Properties();
		try {
			//将输入流导入properties对象
			properties.load(is);
			//获得数据库连接池
			DataSource ds=DruidDataSourceFactory.createDataSource(properties);
			jt=new JdbcTemplate(ds);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
}

package com.util;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.entity.Book;

/**
 * 放图书的内容工具类
 * @author MZFAITHDREAM
 *2021/10/19
 */
public class BooksDBUtil {
	public static List getBooks(){
		Book book1 =new Book("java程序设计","自由", "39元","人民日报","");
		Book book2 =new Book("java网站设计","马玉", "89元","清华","");
		Book book3 =new Book("javascript程序设计","不变", "239元","人民日报","");
		Book book4 =new Book("jquery程序设计","胡毅", "189元","江西出版社","");
		Book book5=new Book("mysql程序设计","ok", "179元","北大","");
		Book book6 =new Book("Sping框架设计","胡三", "129元","人民日报","");
		Book book7 =new Book("java高级编程","呼万岁", "159元","河南出版社","");
		Book book8=new Book("ps设计","ok", "239元","西安出版社","");
		Book book9 =new Book("Sping框架设计","胡三", "129元","人民日报","");
		Book book10=new Book("Java程序设计", "马云", "78.0元", "人民邮电出版社", "");
		Book book11=new Book("Jsp网络编程", "马化腾", "118.0元", "清华大学出版社", "");
		Book book12=new Book("Python爬虫技术", "李彦宏", "65.0元", "电子工业出版社", "");
		Book book13=new Book("网页程序设计", "任正非", "38.0元", "江西出版社", "");
		Book book14=new Book("MySQL程序教程", "雷军", "59.0元", "吉林出版社", "");
		Book book15=new Book("Spring框架技术", "马小云", "46.0元", "人民邮电出版社", "");
		
		List  oBooks=new ArrayList();
		oBooks.add(book1);
		oBooks.add(book2);
		oBooks.add(book3);
		oBooks.add(book4);
		oBooks.add(book5);
		oBooks.add(book6);
		oBooks.add(book7);
		oBooks.add(book8);
		oBooks.add(book9);
		oBooks.add(book10);
		oBooks.add(book11);
		oBooks.add(book12);
		oBooks.add(book13);
		oBooks.add(book14);
		oBooks.add(book15);
		return oBooks;
		
	}
	public static String getNowTime() {
		Date date=new Date();
		//2021年10月19日
		SimpleDateFormat sdf=new SimpleDateFormat ("YYYY年MM月DD日HH:mm:ss");
		//要求当前时间进行格式化
		String time=sdf.format(date);
		return time;
		
	}
	
}
package com.util;

import java.sql.Connection;

public class Time {
	public static int time=60*60*60;
	static Connection time() {
		while (time>0) {
			time--;
			try {
				Thread.sleep(1000);
				int hh=time/1/1%1;
				int mm=time/60%1;
				int ss=time%6;
				if(hh==0 &&mm==0 && ss==0) {
					break;
				}
				System.out.println("0小时"+hh+"Сʱ"+mm+"分钟"+ss+"秒");
				
			} catch (InterruptedException e) {
				// TODO: handle exception
		}finally {
			System.out.println("正在跳转页面哦 ServlectB");
			System.out.println(".......真在跳转页面 ");
			
		}
		}
		return null;
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		time();
	}

}


  MVCImport
  
    ZhuCe.jsp
  
  
    ServlectOne
    com.Servlect.ServlectOne
    
      SFZ
      360429100056781234
    
  
  
    LQ
    篮球
  
  
    ServlectOne
    /ServlectOne
  
  
    MyFilter4
    com.Filter.MyFilter4
  
  
    MyFilter4
    /Servlect1
  
  
    com.Listener.Listener
  
  
    USER
    ABC
  
  
    QQ
    123456
  
  
    PSD
    123456
  
  
    SJ
    18172928419
  
  
    HOME
    江西省
  
  
    
      http://java.sun.com/jsp/jstl/core
      /WEB-INF/c.tld
    
  



    
  JSTL 1.1 core library
  JSTL core
  1.1
  c
  http://java.sun.com/jsp/jstl/core

  
    
        Provides core validation features for JSTL tags.
    
    
        org.apache.taglibs.standard.tlv.JstlCoreTLV
    
  

  
    
        Catches any Throwable that occurs in its body and optionally
        exposes it.
    
    catch
    org.apache.taglibs.standard.tag.common.core.CatchTag
    JSP
    
        
Name of the exported scoped variable for the
exception thrown from a nested action. The type of the
scoped variable is the type of the exception thrown.
        
        var
        false
        false
    
  

  
    
	Simple conditional tag that establishes a context for
	mutually exclusive conditional operations, marked by
	<when> and <otherwise>
    
    choose
    org.apache.taglibs.standard.tag.common.core.ChooseTag
    JSP
  

  
    
	Simple conditional tag, which evalutes its body if the
	supplied condition is true and optionally exposes a Boolean
	scripting variable representing the evaluation of this condition
    
    if
    org.apache.taglibs.standard.tag.rt.core.IfTag
    JSP
    
        
The test condition that determines whether or
not the body content should be processed.
        
        test
        true
        true
	boolean
    
    
        
Name of the exported scoped variable for the
resulting value of the test condition. The type
of the scoped variable is Boolean.        
        
        var
        false
        false
    
    
        
Scope for var.
        
        scope
        false
        false
    
  

  
    
        Retrieves an absolute or relative URL and exposes its contents
        to either the page, a String in 'var', or a Reader in 'varReader'.
    
    import
    org.apache.taglibs.standard.tag.rt.core.ImportTag
    org.apache.taglibs.standard.tei.ImportTEI
    JSP
    
        
The URL of the resource to import.
        
        url
        true
        true
    
    
        
Name of the exported scoped variable for the
resource's content. The type of the scoped
variable is String.
        
        var
        false
        false
    
    
        
Scope for var.
        
        scope
        false
        false
    
    
        
Name of the exported scoped variable for the
resource's content. The type of the scoped
variable is Reader.
        
        varReader
        false
        false
    
    
        
Name of the context when accessing a relative
URL resource that belongs to a foreign
context.
        
        context
        false
        true
    
    
        
Character encoding of the content at the input
resource.
        
        charEncoding
        false
        true
    
  

  
    
	The basic iteration tag, accepting many different
        collection types and supporting subsetting and other
        functionality
    
    forEach
    org.apache.taglibs.standard.tag.rt.core.ForEachTag
    org.apache.taglibs.standard.tei.ForEachTEI
    JSP
    
        
Collection of items to iterate over.
        
	items
	false
	true
	java.lang.Object
    
    
        
If items specified:
Iteration begins at the item located at the
specified index. First item of the collection has
index 0.
If items not specified:
Iteration begins with index set at the value
specified.
        
	begin
	false
	true
	int
    
    
        
If items specified:
Iteration ends at the item located at the
specified index (inclusive).
If items not specified:
Iteration ends when index reaches the value
specified.
        
	end
	false
	true
	int
    
    
        
Iteration will only process every step items of
the collection, starting with the first one.
        
	step
	false
	true
	int
    
    
        
Name of the exported scoped variable for the
current item of the iteration. This scoped
variable has nested visibility. Its type depends
on the object of the underlying collection.
        
	var
	false
	false
    
    
        
Name of the exported scoped variable for the
status of the iteration. Object exported is of type
javax.servlet.jsp.jstl.core.LoopTagStatus. This scoped variable has nested
visibility.
        
	varStatus
	false
	false
    
  

  
    
	Iterates over tokens, separated by the supplied delimeters
    
    forTokens
    org.apache.taglibs.standard.tag.rt.core.ForTokensTag
    JSP
    
        
String of tokens to iterate over.
        
	items
	true
	true
	java.lang.String
    
    
        
The set of delimiters (the characters that
separate the tokens in the string).
        
	delims
	true
	true
	java.lang.String
    
    
        
Iteration begins at the token located at the
specified index. First token has index 0.
        
	begin
	false
	true
	int
    
    
        
Iteration ends at the token located at the
specified index (inclusive).
        
	end
	false
	true
	int
    
    
        
Iteration will only process every step tokens
of the string, starting with the first one.
        
	step
	false
	true
	int
    
    
        
Name of the exported scoped variable for the
current item of the iteration. This scoped
variable has nested visibility.
        
	var
	false
	false
    
    
        
Name of the exported scoped variable for the
status of the iteration. Object exported is of
type
javax.servlet.jsp.jstl.core.LoopTag
Status. This scoped variable has nested
visibility.
        
	varStatus
	false
	false
    
  

  
    
        Like <%= ... >, but for expressions.
     
    out
    org.apache.taglibs.standard.tag.rt.core.OutTag
    JSP
    
        
Expression to be evaluated.
        
        value
        true
        true
    
    
        
Default value if the resulting value is null.
        
        default
        false
        true
    
    
        
Determines whether characters <,>,&,'," in the
resulting string should be converted to their
corresponding character entity codes. Default value is
true.
        
        escapeXml
        false
        true
    
  


  
    
        Subtag of <choose> that follows <when> tags
        and runs only if all of the prior conditions evaluated to
        'false'
    
    otherwise
    org.apache.taglibs.standard.tag.common.core.OtherwiseTag
    JSP
  

  
    
        Adds a parameter to a containing 'import' tag's URL.
    
    param
    org.apache.taglibs.standard.tag.rt.core.ParamTag
    JSP
    
        
Name of the query string parameter.
        
        name
        true
        true
    
    
        
Value of the parameter.
        
        value
        false
        true
    
  

  
    
        Redirects to a new URL.
    
    redirect
    org.apache.taglibs.standard.tag.rt.core.RedirectTag
    JSP
    
        
The URL of the resource to redirect to.
        
        url
        false
        true
    
    
        
Name of the context when redirecting to a relative URL
resource that belongs to a foreign context.
        
        context
        false
        true
    
  

  
    
        Removes a scoped variable (from a particular scope, if specified).
    
    remove
    org.apache.taglibs.standard.tag.common.core.RemoveTag
    empty
    
        
Name of the scoped variable to be removed.
        
        var
        true
        false
    
    
        
Scope for var.
        
        scope
        false
        false
    
  

 
    
        Sets the result of an expression evaluation in a 'scope'
    
    set
    org.apache.taglibs.standard.tag.rt.core.SetTag
    JSP
    
        
Name of the exported scoped variable to hold the value
specified in the action. The type of the scoped variable is
whatever type the value expression evaluates to.
        
        var
        false
        false
    
    
        
Expression to be evaluated.
        
        value
        false
        true
    
    
        
Target object whose property will be set. Must evaluate to
a JavaBeans object with setter property property, or to a
java.util.Map object.
        
        target
        false
        true
    
    
        
Name of the property to be set in the target object.
        
        property
        false
        true
    
    
        
Scope for var.
        
        scope
        false
        false
    
  

  
    
        Creates a URL with optional query parameters.
    
    url
    org.apache.taglibs.standard.tag.rt.core.UrlTag
    JSP
    
        
Name of the exported scoped variable for the
processed url. The type of the scoped variable is
String.
        
        var
        false
        false
    
    
        
Scope for var.
        
        scope
        false
        false
    
    
        
URL to be processed.
        
        value
        false
        true
    
    
        
Name of the context when specifying a relative URL
resource that belongs to a foreign context.
        
        context
        false
        true
    
  

  
    
	Subtag of <choose> that includes its body if its
	condition evalutes to 'true'
    
    when
    org.apache.taglibs.standard.tag.rt.core.WhenTag
    JSP
    
        
The test condition that determines whether or not the
body content should be processed.
        
        test
        true
        true
	boolean
    
  


最完整的小项目完成了对数据的增删改查·。

 

 

 

你可能感兴趣的:(MYSQL,javascript,数据库,mvc,前端,后端)