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

    }

  • 相关阅读:
    HarmonyOS 对象数据库
    springboot 整合/集成 jpa
    linux 安装docker和mysql
    HarmonyOS 多线程
    ElasticSearch 安装及配置 搭建集群
    java 集合
    HarmonyOS 基础数据库
    ElasticSearch 基本操作
    Windows驱动wdf驱动开发系列(一)
    32进程调用64dll的解决方法
  • 原文地址:https://www.cnblogs.com/hwgok/p/5889029.html
Copyright © 2011-2022 走看看