本周进行了团队作业的二阶段任务:
任务很多,要完成对于一阶段的后端的功能实现:
展示sevlet代码
添加
package com.itheima.web.servlet; import com.alibaba.fastjson.JSON; import com.itheima.pojo.Brand; import com.itheima.service.BrandService; import com.itheima.service.impl.BrandServiceImpl; 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 java.io.BufferedReader; import java.io.IOException; import java.util.List; @WebServlet("/addServlet") public class AddServlet extends HttpServlet { private BrandService brandService = new BrandServiceImpl(); @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //1.接收品牌的数据 BufferedReader br = request.getReader(); String params = br.readLine();//json字符串 //转为Brand对象 Brand brand = JSON.parseObject(params,Brand.class); //2.调用service brandService.add(brand); //3.响应一个成功的标识 response.getWriter().write("success"); } @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doGet(request,response); } }
删除
package com.itheima.web.servlet; import com.alibaba.fastjson.JSON; import com.itheima.service.BrandService; import com.itheima.service.impl.BrandServiceImpl; import org.apache.ibatis.annotations.Param; import javax.servlet.*; import javax.servlet.http.*; import javax.servlet.annotation.*; import java.io.BufferedReader; import java.io.IOException; @WebServlet("/deleteByIdsServlet") public class deletByIdsServlet extends HttpServlet { private BrandService brandService = new BrandServiceImpl(); @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //1.接受数据[1,2,3] BufferedReader br = request.getReader(); String params = br.readLine(); //转为int[] int[] ids = JSON.parseObject(params,int[].class); //2.调用service brandService.deleteByIds(ids); //3.响应成功的标识 response.getWriter().write("success"); } @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doGet(request,response); } }
查询所有
package com.itheima.web.servlet; import com.alibaba.fastjson.JSON; import com.itheima.pojo.Brand; import com.itheima.service.BrandService; import com.itheima.service.impl.BrandServiceImpl; import javax.servlet.*; import javax.servlet.http.*; import javax.servlet.annotation.*; import java.io.IOException; import java.util.List; @WebServlet("/selectAllServlet") public class SelectAllServlet extends HttpServlet { private BrandService brandService = new BrandServiceImpl(); @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //1.调用service查询 List<Brand> brands = brandService.selectAll(); //2.转为JSON String jsonString = JSON.toJSONString(brands); //3.写数据 response.setContentType("text/json;charset=utf-8"); response.getWriter().write(jsonString); } @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doGet(request,response); } }
条件模糊查询
package com.itheima.web.servlet; import com.alibaba.fastjson.JSON; import com.itheima.pojo.Brand; import com.itheima.pojo.PageBean; import com.itheima.service.BrandService; import com.itheima.service.impl.BrandServiceImpl; import javax.servlet.*; import javax.servlet.http.*; import javax.servlet.annotation.*; import java.io.BufferedReader; import java.io.IOException; @WebServlet("/selectByPageAndConditionServlet") public class selectByPageAndConditionServlet extends HttpServlet { private BrandService brandService = new BrandServiceImpl(); @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //1.接收参数 当前页码 和 每页展示条数 url?currentPage=1&&pageSize=5 String _currentPage = request.getParameter("currentPage"); String _pageSize = request.getParameter("pageSize"); int currentPage = Integer.parseInt(_currentPage); int pageSize = Integer.parseInt(_pageSize); //获取对应的查询条件对象 BufferedReader br = request.getReader(); String params = br.readLine(); // json字符串 //转为Brand Brand brand = JSON.parseObject(params,Brand.class); //2.调用service查询 PageBean<Brand> pageBean = brandService.selectByPageAndCondition(currentPage,pageSize,brand); //3.转为JSON String jsonString = JSON.toJSONString(pageBean); //4.写数据 response.setContentType("text/json;charset=utf-8"); response.getWriter().write(jsonString); } @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request,response); } }
修改
package com.itheima.web.servlet; import com.alibaba.fastjson.JSON; import com.itheima.pojo.Brand; import com.itheima.service.BrandService; import com.itheima.service.impl.BrandServiceImpl; import javax.servlet.*; import javax.servlet.http.*; import javax.servlet.annotation.*; import java.io.BufferedReader; import java.io.IOException; @WebServlet("/updateByIdServlet") public class UpdateServlet extends HttpServlet { private BrandService brandService = new BrandServiceImpl(); @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //1.接收品牌的数据 BufferedReader br = request.getReader(); String params = br.readLine();//json字符串 //转为Brand对象 Brand brand = JSON.parseObject(params,Brand.class); //2.调用service brandService.update(brand); //3.响应一个成功的标识 response.getWriter().write("success"); } @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doGet(request,response); } }
标签:总结,每周,servlet,request,import,第九,com,response,itheima From: https://www.cnblogs.com/sxwgzx23/p/17470478.html