• 封装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);
    }

    }

  • 相关阅读:
    六十三:CSRF攻击与防御之系统准备之登录与转账功能
    六十二:CSRF攻击与防御之系统准备之注册功能
    六十一:Flask.Session之flask操作session
    并发编程之多进程篇之二
    并发编程之多进程篇之一
    网络编程之文件传输实例
    网络编程基础之粘包现象与UDP协议
    网络编程基础之Socket套接字简单应用
    TCP协议的三次握手和四次挥手
    网络编程基础之Socket套接字
  • 原文地址:https://www.cnblogs.com/hwgok/p/5889029.html
Copyright © 2020-2023  润新知