利用反射实现Servlet公共类的抽取
一次请求的执行过程:
请求:发送请求地址-->到达web.xml中,找到地址对应的servlet类-->通过反射调用该类的构造函数,创建该servlet类的对象-->通过当前对象调用该servlet的init方法-->发现没有-->从其父类HttpServlet找init,还是没有-->再找其父类GenericServlet,有init,调用该init方法加载当前servlet类-->调用当前servlet的service方法-->发现没有,找父类HttpServlet-->父类有该方法,调用它,service中获取请求方式-->然后调用do post()方法,当前servlet的do post()方法-->最后销毁,调用disdroy。嗯,其实调用父类的init,service方法这种说貌似不妥,应该是调用继承自父类的这两个方法
对原本的service方法进行重写,利用反射调用子类的具体执行方法
package com.jixh.ss.web.base; import java.io.IOException;
import java.lang.reflect.Method; import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; public class BaseServlet extends HttpServlet { private static final long serialVersionUID = 1L; @Override
public void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // localhost:8080/store/productServlet?method=updateUser,url中必须带有所要调用的子类具体方法的方法名
String method = req.getParameter("method"); if (null == method || "".equals(method) || method.trim().equals("")) {
method = "execute";
} // 注意:此处的this代表的是子类的对象
// System.out.println(this);
// 子类对象字节码对象
@SuppressWarnings("rawtypes")
Class clazz = this.getClass(); try {
// 查找子类对象对应的字节码中的名称为method的方法.这个方法的参数类型是:HttpServletRequest.class,HttpServletResponse.class
@SuppressWarnings("unchecked")
Method md = clazz.getMethod(method, HttpServletRequest.class, HttpServletResponse.class);
if(null!=md){
String jspPath = (String)md.invoke(this, req, resp);
if (null != jspPath) {
req.getRequestDispatcher(jspPath).forward(req, resp);
}
}
} catch (Exception e) {
e.printStackTrace();
} } // 默认方法
public String execute(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
return null;
} }
子类Servlet只进行具体的业务处理
package com.jixh.ss.web.servlet; import java.util.List; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import com.jixh.peanut.domian.FirstMenu;
import com.jixh.peanut.domian.User;
import com.jixh.peanut.domian.UserPower;
import com.jixh.peanut.service.MenueService;
import com.jixh.peanut.service.UserService;
import com.jixh.peanut.service.impl.MenueServiceImpl;
import com.jixh.peanut.service.impl.UserServiceImpl;
import com.jixh.peanut.util.DateUtils;
import com.jixh.peanut.util.UUIDUtils;
import com.jixh.peanut.web.base.BaseServlet; import net.sf.json.JSONObject; public class userServlet extends BaseServlet {
private static final long serialVersionUID = 1L; // 修改用户
public String updateUser(HttpServletRequest request, HttpServletResponse response) {
try {
// 允许跨域访问的响应头
response.setHeader("Access-Control-Allow-Origin", "*"); String username = request.getParameter("username");
String password = request.getParameter("password");
// 验证用户名密码
UserService userService = new UserServiceImpl();
User user = userService.userLoging(username, password);
if (user != null) {
// 验证通过,进行下一步
String uid = (String) request.getSession().getAttribute("thisUser");
String utype = request.getParameter("utype");
int ustate = Integer.parseInt(request.getParameter("ustate"));
String powerIdArray = request.getParameter("powerIdArray");
System.out.println(uid+"=="+username + "==" + password + "==" + utype + "==" + ustate + "==" + powerIdArray);
//修改用户表
boolean result = userService.updateUser(uid, utype, ustate);
System.out.println("修改用户结果为"+result);
if(result) {
response.getWriter().write("success");
}else {
response.getWriter().write("faild");
}
} else {
// 直接响应
response.getWriter().write("error");
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
利用反射实现Servlet公共类的抽取的更多相关文章
- 如何利用反射简化Servlet操作
如何利用反射简化Servlet操作 一.反射的实现 新建类BaseServlet,继承HttpServlet(不需要在web.xml文件中配置) 1.在doPost()方法中处理请求乱码,并调用d ...
- Android利用反射机制为实体类属性赋值
在做android项目时,有时会遇到从网络上获取json类型数据,赋值给实体类,实体类属性少可以一个一个的赋值,如果实体类有很多属性,赋值可能就要耗很长的功夫了,幸好Java给我们提供了反射机制.下面 ...
- C#利用反射,遍历获得一个类的所有属性名,以及该类的实例的所有属性的值
转自goldeneyezhang原文 C#利用反射,遍历获得一个类的所有属性名,以及该类的实例的所有属性的值 C#利用反射,遍历获得一个类的所有属性名,以及该类的实例的所有属性的值总结: 对应某个类的 ...
- [转]C#利用反射实现两个类的对象之间相同属性的值的复制
本文转自:https://blog.csdn.net/u013093547/article/details/53584591 在使用c#进行程序编写时,会遇到一个问题,两个属性字段差不多相同的类要进行 ...
- C# 利用反射根据类名创建类的实例对象
“反射”其实就是利用程序集的元数据信息. 反射可以有很多方法,编写程序时请先导入 System.Reflection 命名空间. 1.假设你要反射一个 DLL 中的类,并且没有引用它(即未知的类型): ...
- 【转】C# 利用反射根据类名创建类的实例对象
原文地址:https://www.cnblogs.com/feiyuhuo/p/5793606.html “反射”其实就是利用程序集的元数据信息. 反射可以有很多方法,编写程序时请先导入 System ...
- c# 利用反射动态给实体类对象赋值
转:http://blog.sina.com.cn/s/blog_659a572b0100xp5s.html 例子如下 using System; using System.Collections.G ...
- 利用反射api查找一个类的具体信息
讲到这个实例,首先介绍下本人,我是一个php程序猿.从事drupal开发2年多.能够说从实习開始就接触这个,至今没有换过.drupal给我的感觉是俩字"强大",今天写一个views ...
- 利用反射优化Servlet抽象出父类BaseServlet
在编写servlet的时候发现每个servlet里面的doPost方法都如: protected void doPost(HttpServletRequest request, HttpServlet ...
随机推荐
- LeetCode 232: Implement Queue using Stacks
Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of ...
- iOS开发- Images can’t contain alpha channels or transparencies 解决的方法
新版的iTunesConnect真是各种问题阿... 蛋疼要命. 上传介绍图片(屏幕截图)的时候 , 遇到了这个问题 Images can't contain alpha channels or tr ...
- Android 手机影音 开发过程记录(六)
前一篇已经将音乐播放及切换的相关逻辑弄好了,今天主要理一下剩余的部分,包含: 1. 自己定义通知栏的布局及逻辑处理 2. 滚动歌词的绘制 3. 歌词解析 效果图 通知栏 自己定义布局: <?xm ...
- cpc,a wonderful concert
做完这道题突然就感觉自己脑子是不是已经秀逗了,tle到死后才想起来找规律, 就是求排列数的题目,按插入点对状态进行分类,可以暴力tle... #include<iostream> #inc ...
- 基于机器学习的web异常检测——基于HMM的状态序列建模,将原始数据转化为状态机表示,然后求解概率判断异常与否
基于机器学习的web异常检测 from: https://jaq.alibaba.com/community/art/show?articleid=746 Web防火墙是信息安全的第一道防线.随着网络 ...
- Laravel-自定全局函数
Laravel-自定全局函数 标签(空格分隔): php 习惯了 使用 ThinkPHP 框架,有一个公共方法类在代码编写上会快捷很多,所以有必要在此进行配置一番. 实现 在 app 创建文件夹 He ...
- SQL 循环30日
循环30日的统计 大概格式是 with Date as ( select cast(DATEADD(mm, DATEDIFF(mm,,getdate()), ) as datetime) Date u ...
- PostgreSQL Replication之第八章 与pgbouncer一起工作(5)
8.5 维护 pgbouncer 除了我们在本章已经说明的,pgbouncer有一个很好的能够执行基本管理和监控任务的交互式管理界面. 它是如何工作的呢?pgbouncer提供给您一个虚假的称为pgb ...
- 关于H5中 input消除默认,取消在手机上的点击高亮效果
input消除默认,代码如下 input{ -webkit-tap-highlight-color: rgba(255, 255, 255, 0); ...
- spring context对象
在 java 中, 常见的 Context 有很多, 像: ServletContext, ActionContext, ServletActionContext, ApplicationContex ...