在主页里添加注册,删除,和修改超链接
先在页面导入jstl的核心类库
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>成功</title>
</head>
<h3>欢迎${user.username}来到主页</h3>
<table>
<tr>
<th><a href="addGoods.jsp">添加商品</a></th>
</tr>
<tr>
<th>商品编号</th>
<th>商品名称</th>
<th>商品价格</th>
<th>商品说明</th>
<th>操作</th>
</tr>
<c:forEach items="${goodsList}" var="goods">
<tr>
<td>${goods.gid}</td>
<td>${goods.gname}</td>
<td>${goods.price}</td>
<td>${goods.mark}</td>
<td><a href="upd?gid=${goods.gid}">修改</a></td>
<td><a href="del?gid=${goods.gid}">删除</a></td>
</tr>
</c:forEach>
点击商品添加来到商品添加页面
<html>
<head>
<title>商品添加</title>
</head>
<body>
<form action="addGoods" content="post">
商品名称:<input type="text" name="gname" value=""><br/>
商品价格:<input type="number" step="0.01" name="price" value=""><br/>
商品介绍:<input type="text" name="mark" value=""><br/>
<input type="submit" name="" value="确定">
</form>
</body>
</html>
使用Servlet编写添加商品
@WebServlet("/addGoods")
public class AddGoods extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//设置编码格式
request.setCharacterEncoding("utf-8");//请求编码
response.setCharacterEncoding("utf-8");//响应编码
// resp.setContentType("text/html;charset=UTF-8");
//获取请求的参数
Goods goods=new Goods();
goods.setGname(request.getParameter("gname"));
goods.setPrice(Double.parseDouble(request.getParameter("price")));
goods.setMark(request.getParameter("mark"));
GoodsDaolmpl goods1=new GoodsDaolmpl();
int row = goods1.add(goods);
if(row>0){
request.getRequestDispatcher("ServletGoodsAll").forward(request,response);
}else {
request.setAttribute("sb","添加商品信息失败");
request.getRequestDispatcher("sb.jsp").forward(request,response);
}
request.getRequestDispatcher("ServletGoodsAll").forward(request,response);
request.getRequestDispatcher()是请求转发,前后页面共享一个request ; 这个是在服务端运行的,对浏览器来说是透明的。
response.sendRedirect()是重新定向,前后页面不是一个request。而这个是在浏览器端运行的。
相当于当注册完信息点击确定则会跳转到商品页面并实现立刻刷新 显示出最新产品
GoodsDaolmpl goods1=new GoodsDaolmpl();
int row = goods1.add(goods);
使用调用JDBC代码实现分层
public int add(Goods goods){
try {
connection = JDBCUtil.connection();
//定义sql语句
String sql = "insert into t_goods(gname,price,mark) values(?,?,?)";
//获取预处理
ps = connection.prepareStatement(sql);
//传参
ps.setObject(1,goods.getGname());
ps.setObject(2,goods.getPrice());
ps.setObject(3,goods.getMark());
//执行添加
row=ps.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
} finally {
JDBCUtil.colse(ps, connection);
}
return row;
}
跳转后自行获取点击的超链接ID
<a href="upd?gid=${goods.gid}
1.forEach()方法用于调用数组的每个元素,并将元素传递给回调函数。
2.forEach()有三个参数,第一个是function()函数,第二个是对象;
3.函数里面有三个参数,第一个是数组的每一项值(必选),第二个是数组下标(可选),第三个是原数组(可选)
4.forEach()第二个参数对象,使第一个参数函数的this指向对象
<c:forEach items="${goodsList}" var="goods">
</c:forEach>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>成功</title>
</head>
<h3>欢迎${user.username}来到主页</h3>
<table>
<tr>
<th><a href="addGoods.jsp">添加商品</a></th>
</tr>
<tr>
<th>商品编号</th>
<th>商品名称</th>
<th>商品价格</th>
<th>商品说明</th>
<th>操作</th>
</tr>
<c:forEach items="${goodsList}" var="goods">
<tr>
<td>${goods.gid}</td>
<td>${goods.gname}</td>
<td>${goods.price}</td>
<td>${goods.mark}</td>
<td><a href="upd?gid=${goods.gid}">修改</a></td>
<td><a href="del?gid=${goods.gid}">删除</a></td>
</tr>
</c:forEach>
根据Id删除商品数据
@WebServlet("/del")
public class DelGoods extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
int gid = Integer.parseInt(request.getParameter("gid"));
//根据id执行数据库的删除
GoodsDaolmpl goods = new GoodsDaolmpl();
int row = goods.del(gid);
System.out.println(row);
if (row>0) {
request.getRequestDispatcher("ServletGoodsAll").forward(request, response);
} else {
request.setAttribute("sb", "删除出现错误");
request.getRequestDispatcher("sb.jsp").forward(request, response);
}
}
public int del(int gid){
try {
connection = JDBCUtil.connection();
//定义sql语句
String sql = "";
//获取预处理
ps = connection.prepareStatement(sql);
//传参
ps.setObject(1,gid);
//执行添加
row=ps.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
} finally {
JDBCUtil.colse(ps, connection);
}
return row;
}
完成后试图
先完成跳转后自行获取点击的超链接ID
public Goods selectupd(int gid){
Goods goods=null;
try{
connection = JDBCUtil.connection();
String sql="select * from t_goods where gid=?";
ps=connection.prepareStatement(sql);
ps.setObject(1,gid);
rs= ps.executeQuery();
if (rs.next()){
//把当前数据行中的数据去出来,存储到Goods对象中
goods= new Goods();
goods.setGid(rs.getInt("gid"));
goods.setGname(rs.getString("gname"));
goods.setPrice(rs.getDouble("price"));
goods.setMark(rs.getString("mark"));
}
} catch (Exception e){
e.printStackTrace();
}finally {
JDBCUtil.colse(rs,ps,connection);
}
return goods;
}
在书写注册页面
<html>
<head>
<title>修改商品</title>
</head>
<body>
<form action="updGoods" content="post">
商品序号:<input type="text" name="gid" readonly="readonly" value="${goods.gid}" ><br/>
商品名称:<input type="text" name="gname" value="${goods.gname}"><br/>
商品价格:<input type="number" step="0.01" name="price" value="${goods.price}"><br/>
商品介绍:<input type="text" name="mark" value="${goods.mark}"><br/>
<input type="submit" name="" value="确定">
</form>
</body>
</html>
value传入Servlet里Goods对象参数
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
Goods goods=new Goods();
goods.setGid(Integer.parseInt(request.getParameter("gid")));
goods.setGname(request.getParameter("gname"));
goods.setPrice(Double.parseDouble(request.getParameter("price")));
goods.setMark(request.getParameter("mark"));
UpdateDaolmpl upd=new UpdateDaolmpl();
int row = upd.updGoods(goods);
if (row>0) {
request.setAttribute("row",row);
request.getRequestDispatcher("ServletGoodsAll").forward(request, response);
} else {
request.setAttribute("sb", "查询修改出现错误");
request.getRequestDispatcher("sb.jsp").forward(request, response);
}
}
修改JDBC代码
public int updGoods(Goods goods){
try{
connection = JDBCUtil.connection();
String sql="update t_goods SET gname=?,price=?,mark=? where gid=?";
ps=connection.prepareStatement(sql);
ps.setObject(1,goods.getGname());
ps.setObject(2,goods.getPrice());
ps.setObject(3,goods.getMark());
ps.setObject(4,goods.getGid());
row= ps.executeUpdate();
System.out.println(row);
} catch (Exception e){
e.printStackTrace();
}finally {
JDBCUtil.colse(ps,connection);
}
return row;
}
写完后点击修改是这个页面就成功了在去尝试修改
去到主页编写一个输入框查询商品
@WebServlet("/seach")
public class SeachGoods extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("utf-8");
resp.setCharacterEncoding("utf-8");
String keyword = req.getParameter("keyword");
UpdateDaolmpl upd=new UpdateDaolmpl();
List<Goods> goodsList=upd.seach(keyword);
HttpSession session=req.getSession();
session.setAttribute("goodsList",goodsList);
resp.sendRedirect("cg.jsp");
}
public List<Goods> seach(String keyword){
List<Goods> goodsList=new ArrayList<>();
try{
connection = JDBCUtil.connection();
String sql="select * from t_goods where gname like concat('%',?,'%')";
ps=connection.prepareStatement(sql);
ps.setObject(1,keyword);
rs= ps.executeQuery();
while (rs.next()){
//把当前数据行中的数据去出来,存储到Goods对象中
Goods goods= new Goods();
goods.setGid(rs.getInt("gid"));
goods.setGname(rs.getString("gname"));
goods.setPrice(rs.getDouble("price"));
goods.setMark(rs.getString("mark"));
//把Goods对象存储到集合中
goodsList.add(goods);
}
} catch (Exception e){
e.printStackTrace();
}finally {
JDBCUtil.colse(rs,ps,connection);
}
return goodsList;
}