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. for(j=0,i=0;j

    for(j=0,i=0;j<6,i<10;j++,i++) { k=i+j; } k 值最后是多少? <script type="text/javascript" ...

  2. Gixy Nginx 配置分析工具

    项目简介 Gixy 是一款用来分析 Nginx 配置的工具. Gixy 的主要目标是防止安全配置错误,并自动进行缺陷检测. 目前支持的 Python 版本是 2.7 和 3.5+ . 免责声明:Gix ...

  3. bzoj2748(HAOI2018)音量调节

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2748 水得不想写.别忘了-1. #include<iostream> #incl ...

  4. Linux history时间用户ip设置

    Linux history时间用户ip设置        在使用linux服务器的时候发生一些不知道谁操作的问题,google一下说history命令可以查看到历史记录,用过之后发现还是不够详细,再g ...

  5. 下ue节点

    #!/bin/bash action=$1 port=$2 file="/home/operation/workspace/renderingengine/engine/services.t ...

  6. ShowDialog窗体的return问题

    最近的一个项目里,打开新窗口用到了ShowDialog()这种方式,发现在新窗口做保存操作的时候,保存按钮事件下的程序执行完(无论有没有return)都会关闭子窗口. 网上查了一下,发现大家说的方法在 ...

  7. [转]VS2010中使用模块定义文件(.def)

    都知道在写DLL的时候,使用模块定义文件(.def)可以防止DLL里的命名变更. vc6.0中只要在当前目录下添加.def文件,然后编译就Ok了 但在vs2010里这样做是不可以的,必须在项目--属性 ...

  8. windows下编译nginx+nginx_rtmp_modue(vs2013)

    阅读官方编译windows版本的方法 http://nginx.org/en/docs/howto_build_on_win32.html 我的环境 Windows 7 Ultimate 64,Vis ...

  9. nginx基于TCP的反向代理

    一.4层的负载均衡 Nginx Plus的商业授权版开始具有TCP负载均衡的功能.从Nginx 1.7.7版本开始加入的,现在变成了一个商业收费版本,想要试用,需要在官网申请.也就是说,Nginx除了 ...

  10. python中scipy学习——随机稀疏矩阵及操作

    1.生成随机稀疏矩阵: scipy中生成随机稀疏矩阵的函数如下: scipy.sparse.rand(m,n,density,format,dtype,random_state) 1 参数介绍: 参数 ...