zoukankan      html  css  js  c++  java
  • 封装baseControl

    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);
    }

    }

  • 相关阅读:
    图文详解k8s自动化持续集成之GitLab CI/CD
    GitLab CI/CD 进行持续集成
    高性能伪事务之Lua in Redis
    面向对象之组合VS继承:继承过时了?
    Go依赖模块版本之Module避坑使用详解
    Maven3路程(三)用Maven创建第一个web项目(1)
    C#通过WIN32 API实现嵌入程序窗体
    Selenium自動化測試(Python+VS2013)-基礎篇-環境安裝
    VS2013中Python学习笔记[环境搭建]
    win7 windows server 2008R2下 https SSL证书安装的搭配(搭配https ssl本地测试环境)
  • 原文地址:https://www.cnblogs.com/hwgok/p/5889029.html
Copyright © 2011-2022 走看看