一个servlet类处理多个请求(反射优化),简单易懂

目录

介绍

项目结构

BaseServlet

UserServlet

GoodsServlet

index.jsp


介绍

传统的Servlet类只能处理前端传来的一个请求,要想一个servlet类要获取多个请求,则在UserServlet类上加入@WebServlet("/user/*"),接收某一所要操作的一类的所有请求。因为每一个请求路径不同,所以可以获取其路径不同之处,写出对应的方法,最后通过反射,将不同对象的不同请求执行对应的方法。

项目结构

BaseServlet

                注意:项目虚拟路径为/Module01

package com.xc;

import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class BaseServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("get启动!");

        //获取URI路径
        String uri = request.getRequestURI();

        //把"/Module01/indexServlet/"删除,并追加”Method“
        String methodName = new StringBuffer(uri.substring(uri.lastIndexOf("/") + 1)).append("Method").toString();

        //根据方法名获取对应的方法对象,最后执行方法
        Method method = null;
        try {
            //哪一个对象调用doGet方法,则this代表哪个对象
            method = this.getClass().getMethod(methodName, HttpServletRequest.class, HttpServletResponse.class);
            method.invoke(this, request, response);
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("post启动!");
        this.doGet(req, resp);
    }
}
UserServlet
package com.xc;

import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

@WebServlet("/user/*")  //接收所有/Module01/indexServlet/下的请求
public class UserServlet extends BaseServlet {
    public void addMethod(HttpServletRequest request, HttpServletResponse response) {
        System.out.println("用户...addMethod启动");

    }

    public void deleteMethod(HttpServletRequest request, HttpServletResponse response) {
        System.out.println("用户...deleteMethod启动");
    }

    public void updateMethod(HttpServletRequest request, HttpServletResponse response) {
        System.out.println("用户...updateMethod启动");
    }

    public void selectMethod(HttpServletRequest request, HttpServletResponse response) {
        System.out.println("用户...selectMethod启动");
    }
}
GoodsServlet
package com.xc;

import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

@WebServlet("/goods/*")
public class GoodsServlet extends BaseServlet {
    public void addMethod(HttpServletRequest request, HttpServletResponse response) {
        System.out.println("商品...addMethod启动");

    }

    public void deleteMethod(HttpServletRequest request, HttpServletResponse response) {
        System.out.println("商品...deleteMethod启动");
    }

    public void updateMethod(HttpServletRequest request, HttpServletResponse response) {
        System.out.println("商品...updateMethod启动");
    }

    public void selectMethod(HttpServletRequest request, HttpServletResponse response) {
        System.out.println("商品...selectMethod启动");
    }
}
index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>反射测试</title>
</head>
<body>
    用户操作:
    <a href="${pageContext.request.contextPath}/user/add">添加</a>
    <a href="${pageContext.request.contextPath}/user/delete">删除</a>
    <a href="${pageContext.request.contextPath}/user/update">修改</a>
    <a href="${pageContext.request.contextPath}/user/select">查询</a>
    <br>
    <br>
    商品操作:
    <a href="${pageContext.request.contextPath}/goods/add">添加</a>
    <a href="${pageContext.request.contextPath}/goods/delete">删除</a>
    <a href="${pageContext.request.contextPath}/goods/update">修改</a>
    <a href="${pageContext.request.contextPath}/goods/select">查询</a>
</body>
</html>

实现:

点击 用户操作的添加和商品操作的删除

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值