servlet数量太多,搞个轻量级springmvc

这两天用jsp+servlet+MySQL开发了一个企业网站,后台和前台都有,功能已经实现了,现在总结发现,servlet是真的多,我这个系统有7个表,后台实现一遍增删改查,就得有28个servlet,而且还有前台的部分,servlet的数量真的太多了,想想看springmvc,其实是可以写一个轻量级的springmvc的。

在Servlet中实现类似Spring的@Controller注解功能,主要是通过自定义注解和一个中央处理器(如Servlet过滤器或一个集中的Servlet)来解析这些注解并将请求映射到相应的方法。下面是一个简单的示例:

  1. 自定义注解 - ControllerRequestMapping
java 复制代码
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface Controller {
}

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface RequestMapping {
    String value();
}
  1. 自定义控制器类
java 复制代码
@Controller
public class MyController {

    @RequestMapping(value = "/hello")
    public void hello(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.getWriter().write("Hello, World!");
    }
}
  1. 创建一个过滤器来处理请求映射
java 复制代码
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

public class DispatcherFilter implements Filter {

    private Map<String, Method> handlerMapping = new HashMap<>();

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        // 扫描所有带有@Controller注解的类,并将它们注册到handlerMapping中
        // 这里需要使用反射或类路径扫描库(如ClassGraph)
        // 以下代码仅为示例,实际应用中需要根据项目结构进行调整
        Class<MyController> clazz = MyController.class;
        for (Method method : clazz.getDeclaredMethods()) {
            if (method.isAnnotationPresent(RequestMapping.class)) {
                RequestMapping annotation = method.getAnnotation(RequestMapping.class);
                handlerMapping.put(annotation.value(), method);
            }
        }
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        String uri = request.getRequestURI();
        Method method = handlerMapping.get(uri);
        if (method != null) {
            try {
                method.invoke(method.getDeclaringClass().newInstance(), request, response);
            } catch (Exception e) {
                throw new ServletException(e);
            }
        } else {
            response.sendError(HttpServletResponse.SC_NOT_FOUND);
        }
    }

    @Override
    public void destroy() {
    }
}
  1. 在web.xml中配置过滤器
xml 复制代码
<filter>
    <filter-name>dispatcher</filter-name>
    <filter-class>com.example.DispatcherFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>dispatcher</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

请注意,上述代码仅作为示例,实际应用中可能需要处理更多细节,如异常处理、类型安全、参数绑定等。此外,使用反射和类路径扫描可能会带来性能影响,因此在生产环境中应考虑优化。

相关推荐
java1234_小锋18 分钟前
MyBatis如何处理懒加载和预加载?
java·开发语言·mybatis
hdsoft_huge36 分钟前
小程序弱网 / 无网场景下 CacheManager 离线表单与拍照上传解决方案
java·小程序·uni-app
麦嘟学编程43 分钟前
开发环境搭建之JDK11+maven3.9.8+tomcat9安装
java
小坏讲微服务44 分钟前
使用 Spring Cloud Gateway 实现集群
java·spring boot·分布式·后端·spring cloud·中间件·gateway
wa的一声哭了44 分钟前
hf中transformers库中generate的greedy_search
android·java·javascript·pytorch·深度学习·语言模型·transformer
.格子衫.1 小时前
Maven的下载与安装
java·maven
Override笑看人生1 小时前
gitlab中maven私有库使用备忘
java·gitlab·maven
不知几秋1 小时前
配置JDK和MAVEN
java·开发语言·maven
没有bug.的程序员1 小时前
Spring Cloud Gateway 路由与过滤器机制
java·开发语言·spring boot·spring·gateway
oak隔壁找我1 小时前
Spring AI 实现MCP简单案例
java·人工智能·后端