JavaEE/MySQL/JDBC+HTML/CSS/JavaScript基础
优点:充分利用客户端机器的资源,减轻服务器的负荷
一部分安全要求不高的计算任务、存储任务放在客户端执行,不需要把所有的计算和存储在服务器端执行,从而减轻服务器压力,减轻网络负荷
缺点:需要给每台客户端安装;
升级维护成本较高;
优点:客户端不需要安装;维护成本低
缺点:服务器负荷较重
免费、性能较好、稳定的web容器
官网下载安装解压
https://tomcat.apache.org/migration-8.html
确定提前配置好Java环境变量
运行startup
启动成功
重启Tomcat
访问http://localhost:8080/test/demo09.html
使用IDEA可以一键部署
开始部署
如何改端口号?
注意将hello01与WEB-INF放置在同一级目录
运行
package com.fancy; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; public class AddServlets extends HttpServlet { //响应post请求 @Override public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //获取请求包中的各项参数 String fname = request.getParameter("fname"); String priceStr = request.getParameter("price"); Integer price = Integer.parseInt(priceStr); String fcountStr = request.getParameter("fcount"); Integer fcount = Integer.parseInt(fcountStr); String remark = request.getParameter("remark"); System.out.println("fname = "+fname); } }
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Test</title> </head> <body> <form action="add" method="post"> 名称:<input type="text" name="fname"/><br/> 价格:<input type="text" name="price"/><br/> 库存:<input type="text" name="fcount"/><br/> 备注:<input type="text" name="remark"/><br/> <input type="submit" value="添加"/> </form> </body> </html>
用户发送请求,action=add
Tomcat中,web.xml中找到URL-patten = /add
找servlet-name = AddServlet
找和servlet-mapping中servlet-name一致的servlet
找servlet-class:com.fancy.AddServlets
用户发的是post请求,Tomcat执行AddServlet中doPost方法
Data Access Object访问数据信息的类和接口,包括了对数据的CRUD(Create、Retrival、Update、Delete),而不包含任何业务相关的信息。有时也称作:BaseDAO
作用:为了实现功能的模块化,更有利于代码的维护和升级。
package com.fancy.dao; import com.fancy.pojo.Fruit; import java.util.List; public interface FruitDAO { //查询库存列表 List<Fruit> getFruitList(); //新增库存 boolean addFruit(Fruit fruit); //修改库存 boolean updateFruit(Fruit fruit); //根据名称查询特定库存 Fruit getFruitByFname(String fname); //删除特定库存记录 boolean delFruit(String fname); }
@Override public boolean addFruit(Fruit fruit) { String sql = "insert into t_fruit values(0,?,?,?,?)"; int count = super.executeUpdate(sql,fruit.getFname(),fruit.getPrice(),fruit.getFcount(),fruit.getRemark()) ; //insert语句返回的是自增列的值,而不是影响行数 //System.out.println(count); return count>0; }
注意加入jar包、根据自己的mysql配置好以下参数
public final String DRIVER = "com.mysql.jdbc.Driver" ; public final String URL = "jdbc:mysql://localhost:3306/fruitdb?useUnicode=true&characterEncoding=utf-8&useSSL=false"; public final String USER = "localhost"; public final String PWD = "123456" ;
package com.fancy; import com.fancy.dao.FruitDAO; import com.fancy.dao.impl.FruitDAOImpl; import com.fancy.pojo.Fruit; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; public class AddServlets extends HttpServlet { //响应post请求 @Override public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //设置request编码规则 request.setCharacterEncoding("UTF-8"); //获取请求包中的各项参数 String fname = request.getParameter("fname"); String priceStr = request.getParameter("price"); Integer price = Integer.parseInt(priceStr); String fcountStr = request.getParameter("fcount"); Integer fcount = Integer.parseInt(fcountStr); String remark = request.getParameter("remark"); FruitDAO fruitDAO = new FruitDAOImpl(); Boolean flag = fruitDAO.addFruit(new Fruit(0,fname,price,fcount,remark)); System.out.println(flag ? "添加成功":"添加失败!"); System.out.println(price); } }
目录结构