项目_1.3_手写SpringMVC框架
2024-10-19 18:38:57
初步修改项目
package com.hy.servlet; import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method; 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 com.hy.utils.StringUtil; @WebServlet("/cs")
public class CoreServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String ac = request.getParameter("ac");
System.out.println("======="+ac+"======");
if (StringUtil.isEmpty(ac))
ac = "index"; // 获取当前类的所有方法
Method[] methods = this.getClass().getDeclaredMethods(); for (Method m : methods) {
// 获取方法名称
String methodName = m.getName();
if (ac.equals(methodName)) {
// 找到和ac同名的方法,那么通过反射技术调用它
try {
m.invoke(this, request, response);
return;
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
e.printStackTrace();
}
}
} throw new RuntimeException("ac值错误");
} private void index(HttpServletRequest request, HttpServletResponse response) {
System.out.println("aaaaaaaaaaa");
}
}
package com.hy.utils; public class StringUtil {
//判断字符串是否为null或者""
public static boolean isEmpty(String str){
return str==null || "".equals(str);
} public static boolean isNotEmpty(String str){
return !isEmpty(str);
} }
成功
最新文章
- PHP扩展——C扩展实现滚动记录日志
- LevelDB源码分析--使用Iterator简化代码设计
- poj1979
- ubuntu16.04 编译安装mysql5.7.x,以及配置使用
- BeanDefinition的Resource定位——3
- trie树(前缀树)
- CSS的浮动和清除
- SqlBulkCopy的使用
- node.js 基础知识之 querystring.stringify querystring.escape
- 技术人应该学习的行话--UML统一建模语言
- Glide和Picasso的区别
- vue刷新当前路由
- 1.shell学习之常用语句
- C# 中BindingSource 的用法
- CRM 报表中 Filtered 无数据
- Word2007的自动插入题注!
- Spring WebSocket教程(二)
- LOJ 164 【清华集训2015】V——线段树维护历史最值
- layer的alert图
- 2018 徐州赛区网赛 G. Trace
热门文章
- Linux docker 安装nginx 配置ssl证书
- springboot集成xxl-job问题
- web基础(7): JavaScript 简介/语法
- html 1.0
- npm install报错C:\Users\Guyang\AppData\Roaming\npm-cache\_logs\xxx-14T01_06_33_159Z-debug-0.log
- 初学银河麒麟linux笔记 第四章 windows中开发的QT程序适配linux的修改——error: ‘QT_WARNING_DISABLE_DEPRECATED’ does not name a type
- linux下安装jdk8,nginx
- A - Yet Another Tetris Problem
- 【git】git基本使用以及设置
- 【根文件系统 】开发板通过nfs挂载ubuntu上的根文件系统