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

    }

  • 相关阅读:
    简单自定义控件在view下可以运行在传统模式下运行显示空白
    symbian菜单2运行时更换菜单实现右键菜单功能
    得到文件路径信息 TParse
    Symbian菜单:创建一个最简单的菜单
    RFs 目录操作
    作品[RFileRead/WriteStream 练习] for s60 fp2
    对话框的练习
    文件浏览工具s60第二版
    转描述符
    得到驱动器信息
  • 原文地址:https://www.cnblogs.com/hwgok/p/5889029.html
Copyright © 2011-2022 走看看