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

    }

  • 相关阅读:
    由于扩展配置问题而无法提供您请求的页面。如果该页面是脚本,请添加处理程序。
    中晟银泰国际中心酒店式公寓介绍 业主交流QQ群:319843248
    社保关系转移
    在中国,大数据的有效商业模式在哪里?
    指点传媒:在手机上做“精准营销”
    说说大型高并发高负载网站的系统架构【转】
    BI的相关问题[转]
    python 中有趣的库tqdm
    python之字符串操作方法
    比Screen更好用的神器:tmux
  • 原文地址:https://www.cnblogs.com/hwgok/p/5889029.html
Copyright © 2011-2022 走看看