首页 > 其他分享 >每周总结第九周

每周总结第九周

时间:2023-06-09 23:24:43浏览次数:37  
标签:总结 每周 servlet request import 第九 com response itheima

本周进行了团队作业的二阶段任务:

任务很多,要完成对于一阶段的后端的功能实现:

展示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

相关文章

  • 第六周总结
    今天写了第二个自测题,其中遇到了错误拦截的问题,对结构流程还是不够熟练publicbooleanInsert(studentstu){booleanf=false;Connectionconnection=DBUtil.getConnection();PreparedStatementpreparedStatement=null;try{Stringsql="insertintostudent(id,name,......
  • 第一周总结
    本周为软件工程开课第一周,学习mvc框架做信息征集系统,收获很大某个文本文件中存储了60W条记录,以\r\n作为分隔符,现在需要从文本中一次性取出所有值并存放到一个string[]数组中。StreamReadersr=newStreamReader(strFilePath,System.Text.UnicodeEncoding.GetEncoding("utf-......
  • 第二周总结
    本周进行课程介绍,完成代码--返回一个整数数组中最大子数组的和(一)packageyiweishuzu;importjava.util.Scanner;/***贪心算法*@authorLenovo**/publicclassshuzu_3{privatestaticScannerscan;publicstaticvoidmain(String[]args){//确定......
  • 第三周总结
    完成代码--返回一个整数数组中最大子数组的和(二)这次,在上次的情况下增加了一些新的要求。· 要求数组从文件读取。·  如果输入的数组很大, 并且有很多大的数字, 就会产生比较大的结果(考虑一下数的溢出),请保证你的程序能正常输出。好,下面就是对代码的构......
  • 第四周总结
    完成代码--返回一个整数数组中最大子数组的和(三)--环形这次,在上次的要求中又多了一个条件——要求这个数组是环形的。 在上次的基础上,将这个代码进行改进,得到以下结果。下面把代码贴出来。packageyiweishuzu;importjava.util.Scanner;publicclassshuzu_3{priv......
  • 每周总结第八周
    本周进行了期末一些学科的复习。如数据库,数学建模。学习了数据库基础知识,sql语句的基础运行,授权和权限管理,x锁和s锁等等数据库知识。数学建模完成了对初等数学建模,线性规划建模,动态规划建模以及概率建模进行了复习。......
  • 每周总结第七周
    本周开始了团队作业:本阶段任务完成页面即可:<!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><linkrel="icon"href="lib/logo.ico"type="images/x-ico"/><title>智能......
  • 每周总结第五周
    本周进行了结对作业完成了web地铁查询系统整体还是使用jsp组成页面,jdbc连接数据库的方法进行编写。 文件太多,不在细写。 总之,地铁的换站功能没能完成。 ......
  • 每周总结第四周
    本周完成了算法作业: 课堂练习01题目:计算最长英语单词链。一、题目内容:大家经常玩成语接龙游戏,我们试一试英语的接龙吧:一个文本文件中有N个不同的英语单词,我们能否写一个程序,快速找出最长的能首尾相连的英语单词链,每个单词最多只能用一次。最长的定义是:最多单词数量,和单词......
  • 【做题笔记】做题经验总结
    1.int*int会爆int,记得开longlong2.一般情况下,对于一棵树,树根没有父亲3.一定要看输入和输出格式4.多测不清空,爆零两行泪......