Web-demo01-优化

本文探讨了前端Web应用的性能优化,包括利用反射技术提高效率,详细介绍了四个关键部分,旨在提升用户体验和应用程序的响应速度。
摘要由CSDN通过智能技术生成

PART 01

package com.atguigu.fruit.serlvets;

import com.atguigu.fruit.dao.FruitDAO;
import com.atguigu.fruit.impl.FruitDAOImpl;
import com.atguigu.fruit.pojo.Fruit;
import com.atguigu.myssm.springmvc.ViewBaseServlet;
import com.atguigu.myssm.util.StringUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

/**
 * @author crystal
 * @create 2022-11-19 17:17
 */
@WebServlet("/fruit.do")
public class FruitServlet2 extends ViewBaseServlet {

    FruitDAO fruitDAO = new FruitDAOImpl();

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        String operate = request.getParameter("operate");
        if(operate == null){
            operate = "index";
        }
        switch(operate){
            case "index":
                index(request,response);
                break;
            case "edit":
                edit(request,response);
                break;
            case "del":
                del(request,response);
                break;
            case "add":
                add(request,response);
                break;
            case "update":
                update(request,response);
                break;
            default:
                throw new RuntimeException("输入的opetate的值有误!");
        }
    }

    private void index(HttpServletRequest request,HttpServletResponse response) throws IOException {
        HttpSession session = request.getSession();
        Integer pageNo = 1;
        String oper = request.getParameter("oper");
        String keyword = null;
        if(StringUtil.isNotEmpty(oper) && "search".equals(oper)){
            pageNo = 1;
            keyword = request.getParameter("keyword");
            if(StringUtil.isEmpty(keyword)){
                keyword = "";
            }
            session.setAttribute("keyword",keyword);
        }else{
            String pageNoStr = request.getParameter("pageNo");
            if(StringUtil.isNotEmpty(pageNoStr)){
                pageNo = Integer.parseInt(pageNoStr);
            }
            Object keywordObj = session.getAttribute("keyword");
            if(keywordObj != null){
                keyword = (String) keywordObj;
            }else{
                keyword = "";
            }
        }
        session.setAttribute("pageNo",pageNo);

        List<Fruit> fruitList = fruitDAO.getAll(keyword, pageNo);
        session.setAttribute("fruitList",fruitList);

        Integer fruitCount = fruitDAO.getFruitCount(keyword);

        int pageCount = (fruitCount + 5 - 1) / 5;

        session.setAttribute("pageCount",pageCount);

        super.processTemplate("index",request,response);
    }

    private void edit(HttpServletRequest request,HttpServletResponse response) throws IOException {
        String fidStr = request.getParameter("fid");
        if(StringUtil.isNotEmpty(fidStr)){
            int fid = Integer.parseInt(fidStr);
            Fruit fruit = fruitDAO.getFruitById(fid);
            request.setAttribute("fruit",fruit);
            super.processTemplate("edit",request,response);
        }
    }

    private void update(HttpServletRequest request,HttpServletResponse response) throws IOException {
        String fidStr = request.getParameter("fid");
        Integer fid = Integer.parseInt(fidStr);
        String fname = request.getParameter("fname");
        String priceStr = request.getParameter("price");
        int price = Integer.parseInt(priceStr);
        String fcountStr = request.getParameter("fcount");
        Integer fcount = Integer.parseInt(fcountStr);
        String remark = request.getParameter("remark");

        fruitDAO.updateFruit(new Fruit(fid,fname, price ,fcount ,remark ));
        response.sendRedirect("fruit.do");
    }

    private void del(HttpServletRequest request,HttpServletResponse response) {
        String fidStr = request.getParameter("fid");
        if (StringUtil.isNotEmpty(fidStr)) {
            int fid = Integer.parseInt(fidStr);
            fruitDAO.delFruit(fid);
        }
    }

    private void add(HttpServletRequest request,HttpServletResponse response) throws IOException {
        String fname = request.getParameter("fname");
        Integer price = Integer.parseInt(request.getParameter("price")) ;
        Integer fcount = Integer.parseInt(request.getParameter("fcount"));
        String remark = request.getParameter("remark");

        Fruit fruit = new Fruit(0,fname , price , fcount , remark ) ;

        fruitDAO.insertFruit(fruit);

        response.sendRedirect("fruit.do");

    }
}

PART 02:使用反射技术

package com.atguigu.fruit.controllers;

import com.atguigu.fruit.dao.FruitDAO;
import com.atguigu.fruit.impl.FruitDAOImpl;
import com.atguigu.fruit.pojo.Fruit;
import com.atguigu.myssm.springmvc.ViewBaseServlet;
import com.atguigu.myssm.util.StringUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;

/**
 * @author crystal
 * @create 2022-11-16 15:15
 */

public class FruitController e
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值