BaseServlet详解(每行代码均有详细注释)

1、BaseServlet的作用

  知其然,知其所以然,要用好一个东西必然是需要去了解它的作用与目的,BaseServlet是一个工具类,它不被直接调用而是用于被其余Servlet继承,因此不需要注解,目的是为了降低servlet冗余,可以将多个方法写到同一个servlet,通过子类继承BaseServlet,子类来获取前端需要调用servlet中的那一个方法,而未继承BaseServlet的时候对同一个类的每种操作(增、删、改、查)均需要一个Servlet去实现(一个doGet/doPost只能实现一种方法)。

2、BaseServlet的具体实现

  我就不多说废话了,下面是我的BaseServlet的具体实现,内附有超详细注释。

public class BaseServlet extends HttpServlet {
    /*
     * init用于servlet初始化时调用,只调用一次,后续再次请求将不会再次调用init
     * service:每次请求servlet时都会调用service
     * service方法会调用doGet、doPost方法(HTTPServlet重写了service方法调用了doGet、doPost)
     * 重写了Service就可以不重写doGet、doPost,直接在service中进行了处理
     *
     * 继承BaseServlet的子类servlet会自动调用service方法
     * */

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");

        //获取传来的方法名字,要知晓需要使用哪个方法
        String methodName = req.getParameter("method");

        Method method = null;

        try {

            /*
             * Class<? extends BaseServlet> aClass = this.getClass();
             * this.getClass()获取当前servlet类的class类对象,
             * 进行了泛型约束Class<? extends BaseServlet>, 获取的是BaseServlet或者其子类的类对象
             */
            /* getMethod(methodName,HttpServletRequest.class, HttpServletResponse.class);
             * 获取方法对象(method类对象),需要的参数:需要获取的方法的方法名、HttpServletRequest.class, HttpServletResponse.class
             */
            Class<? extends BaseServlet> aClass = this.getClass();
            method = aClass.getMethod(methodName, HttpServletRequest.class, HttpServletResponse.class);

            /*
             * this:当前类,使用service方法的类,这里是继承BaseServlet的类
             * method.invoke在this这个类中调用method方法
             */
            method.invoke(this, req, resp);

        } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
            e.printStackTrace();
        }

    }
}

  以上所有内容均为个人见解,如有错误欢迎批评与指正,更欢迎广大同胞们表达自己的看法共同进步。

  • 2
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
首先,我们需要创建一个Product实体类,用于存储商品信息。假设该类包含以下属性: ```java public class Product { private int id; private String name; private double price; private String description; // 其他属性 // getter和setter方法 } ``` 接着,我们需要创建一个ProductDao类,用于从数据库中获取商品信息。假设我们使用JDBC连接数据库,可以在ProductDao类中编写以下代码: ```java public class ProductDao { // 获取数据库连接的方法 // 查询所有商品信息的方法 public List<Product> getAllProducts() { List<Product> products = new ArrayList<>(); // 获取数据库连接 // 执行查询语句 // 将查询结果封装为Product对象,并添加到products列表中 // 关闭连接 return products; } } ``` 然后,我们需要创建一个ProductService类,用于调用ProductDao类中的方法获取商品信息。可以在ProductService类中编写以下代码: ```java public class ProductService { private ProductDao productDao; public ProductService() { productDao = new ProductDao(); } // 获取所有商品信息的方法 public List<Product> getAllProducts() { return productDao.getAllProducts(); } } ``` 接着,我们需要创建一个BaseServlet类,用于处理页面的请求。可以在BaseServlet类中编写以下代码: ```java public abstract class BaseServlet extends HttpServlet { // 处理GET请求的方法 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } // 处理POST请求的方法 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 获取请求的路径 // 根据路径调用相应的处理方法 } // 显示所有商品信息的方法 protected void showAllProducts(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { ProductService productService = new ProductService(); List<Product> products = productService.getAllProducts(); request.setAttribute("products", products); request.getRequestDispatcher("/showAllProducts.jsp").forward(request, response); } } ``` 最后,我们需要创建一个showAllProducts.jsp页面,用于展示所有商品信息。可以在showAllProducts.jsp页面中编写以下代码: ```html <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>所有商品信息</title> </head> <body> <table> <thead> <tr> <th>ID</th> <th>名称</th> <th>价格</th> <th>描述</th> </tr> </thead> <tbody> <c:forEach items="${products}" var="product"> <tr> <td>${product.id}</td> <td>${product.name}</td> <td>${product.price}</td> <td>${product.description}</td> </tr> </c:forEach> </tbody> </table> </body> </html> ``` 以上代码演示了如何使用Java Web技术展示所有商品信息。当用户访问showAllProducts路径时,BaseServlet会调用showAllProducts方法,该方法会调用ProductService中的getAllProducts方法获取所有商品信息,并将其存储在request对象中,然后转发到showAllProducts.jsp页面展示。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值