package com.huawei.base;

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

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

/**
* @author Administrator
*
*/
public abstract class BaseController extends HttpServlet{

/**
*
*/
private static final long serialVersionUID = 4874135853046529162L;

protected HttpServletRequest request;

protected HttpServletResponse response;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doPost(req, resp);
}

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String _method = req.getParameter("_method");
_method = (_method == null)?"":_method;
/**
* 做一个反射的中转站
*
* 根据传入的_method 动态的去调用 所对应的方法
*
*
* eg:_method :delete
*/
//得到 目标class
Class<?> clazz = this.getClass();
this.request = req;
this.response = resp;
try {
//得到 指定的方法
Method method = clazz.getDeclaredMethod(_method,new Class[]{HttpServletRequest.class,HttpServletResponse.class});
//执行对应的方法
boolean access = method.isAccessible();
method.setAccessible(true);
method.invoke(this, req,resp);
method.setAccessible(access);
} catch (SecurityException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
this.findAll(req, resp);
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}

protected void findAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
String _method = this.getParameter("_method");
if(_method == null || _method.trim()==""){
response.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED, "缺少方法名!");
}else{
response.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED, "该"+_method+"没有被实现!");
}
}

protected String getParameter(String name){
return this.request.getParameter(name);
}

protected String[] getParameterValues(String name) {
return this.request.getParameterValues(name);
}

protected PrintWriter getWriter() {
try {
return this.response.getWriter();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}

protected OutputStream getOutputStream() {
try {
return this.response.getOutputStream();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}

protected void write(String msg){
this.getWriter().write(msg);
}

}

封装baseControl的更多相关文章

  1. BaseControl按钮合集

    BaseControl按钮合集 效果 源码 https://github.com/YouXianMing/Animations // // POPBaseControl.h // Animations ...

  2. 用UIControl封装Button

    用UIControl封装Button 效果 说明 UIControl在处理超出触摸范围的触摸事件时有bug 源码 基础类 // // BaseControl.h // BaseControl // / ...

  3. 玩转控件:封装Dev的LabelControl和TextEdit

    俗话说的好:"工欲善其事必先利其器",作为软件攻城狮也是同样道理,攻城狮开发的软件目的是简化客户的操作,让客户动动手指就可以完成很多事情,减少人力成本.这也是系统/软件存在的目的. ...

  4. [C#] 简单的 Helper 封装 -- RegularExpressionHelper

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  5. iOS开发之App间账号共享与SDK封装

    上篇博客<iOS逆向工程之KeyChain与Snoop-it>中已经提到了,App间的数据共享可以使用KeyChian来实现.本篇博客就实战一下呢.开门见山,本篇博客会封装一个登录用的SD ...

  6. Ajax实现原理,代码封装

    都知道实现页面的异步操作需要使用Ajax,那么Ajax到是怎么实现异步操作的呢? 首先需要认识一个对象 --> XMLHttpRequest 对象 --> Ajax的核心.它有许多的属性和 ...

  7. 用C语言封装OC对象(耐心阅读,非常重要)

    用C语言封装OC对象(耐心阅读,非常重要) 本文的主要内容来自这里 前言 做iOS开发的朋友,对OC肯定非常了解,那么大家有没有想过OC中NSInteger,NSObject,NSString这些对象 ...

  8. 【知识必备】RxJava+Retrofit二次封装最佳结合体验,打造懒人封装框架~

    一.写在前面 相信各位看官对retrofit和rxjava已经耳熟能详了,最近一直在学习retrofit+rxjava的各种封装姿势,也结合自己的理解,一步一步的做起来. 骚年,如果你还没有掌握ret ...

  9. 对百度WebUploader开源上传控件的二次封装,精简前端代码(两句代码搞定上传)

    前言 首先声明一下,我这个是对WebUploader开源上传控件的二次封装,底层还是WebUploader实现的,只是为了更简洁的使用他而已. 下面先介绍一下WebUploader 简介: WebUp ...

随机推荐

  1. 通过Java Api与HBase交互

    HBase提供了Java Api的访问接口,掌握这个就跟Java应用使用RDBMS时需要JDBC一样重要,本文将继续前两篇文章中blog表的示例,介绍常用的Api. import java.io.IO ...

  2. 《DSP using MATLAB》示例Example7.4

    代码: h = [-4, 1, -1, -2, 5, 6, 5, -2, -1, 1, -4]; M = length(h); n = 0:M-1; [Hr, w, a, L] = Hr_Type1( ...

  3. RF设置全局变量

    一般情况下,我们的测试用例会有很多公用数据,比如在测试购票功能的时候,可能是一直使用同一个列车号,这时候我们就没有必要在每一个Case中都去新建一个列车班次,而是设置一个全局变量: 1.Set Var ...

  4. 从数据库导出数据到excel之List<map>导出

    说明:很多时候取出来的数据是封装为List<Map<String,Object>>,可以直接导出excel表格 项目说明就在 “上一篇” 直接上代码(数据层和业务层不用说了,查 ...

  5. JS实现动态提示框

    引言 什么项目都有个需求,应开发需求,需要写一个公式编辑器选择公式的插件,下面给大家讲一下实现过程.(擦汗,强作淡定,咳,开嗓~) 看图说话 本小菜开发功能前乐于先写个需求思维导图(纯属个人爱好): ...

  6. 什么是spark(五)Spark SQL

       Spark SQL Spark SQL主要分为两部分,一部分是Spark Sql在scala中直接,使用作为执行层面上的应用,本质上就是生成DAG的另外一种形式:其发生试下Driver中生成: ...

  7. oracle 的一些基础查询

    select status,T.* from user_indexes Twhere table_name='T_ADMIN_DEALER' --查询表是否有了索引  select username, ...

  8. linux(centos)下安装ffmpeg

    [备忘]windows环境下20行php代码搞定音频裁剪 上次我的这篇文章将了windows下web中如何操作ffmpeg的文章,这里则记录下linux(centos)下的安装 首先:我花了中午大概1 ...

  9. linux性能监控——CPU、Memory、IO、Network

    一.CPU 1.良好状态指标 CPU利用率:User Time <= 70%,System Time <= 35%,User Time + System Time <= 70%. 上 ...

  10. GOF23设计模式之外观模式(facade)

    一.外观模式概述 外观模式也称为门面模式. 核心:为了系统提供统一的入口,封装子系统的复杂性,便于客户端调用. 二.外观模式场景导入与示例代码 场景:要想自己去注册一个公司,首先去工商局检测命名是否合 ...