科技行者

行者学院 转型私董会 科技行者专题报道 网红大战科技行者

知识库

知识库 安全导航

至顶网软件频道运用Filter,ThreadLocal和Reflection,实现form到bean的填充

运用Filter,ThreadLocal和Reflection,实现form到bean的填充

  • 扫一扫
    分享文章到微信

  • 扫一扫
    关注官方公众号
    至顶头条

以Filter截取请求,填充至bean,并将此bean保存至ThreadLocal,在使用时直接从ThreadLocal中获取。

作者:Nurhachi Jang 来源:CSDN 2008年2月28日

关键字: java ThreadLocal Filter

  • 评论
  • 分享微博
  • 分享邮件

在本页阅读全文(共4页)

 /**
     * Do actual work for filling.
     *
     * @param request The servlet request we are processing
     * @param result The servlet response we are creating
     * @param chain The filter chain we are processing
     *
     * @exception IOException if an input/output error occurs
     * @exception ServletException if a servlet error occurs
     */
    public void doFilter(ServletRequest request, ServletResponse response,
            FilterChain chain)
            throws IOException, ServletException {
        String beanName = ((HttpServletRequest)request).getParameter("formbean");
System.err.println(" form bean is " + beanName);
        Class beanClazz = null;
        Object beanObject = null;
        boolean isValid = false;
       
        if(beanName != null) {
            try {
                beanClazz = Class.forName(beanName);
                if(beanClazz != null) {
                    beanObject = beanClazz.newInstance();
                }
            } catch (ClassNotFoundException ex) {
                request.setAttribute("MainErrMsg", beanName + " not found");
                this.filterConfig.getServletContext().getRequestDispatcher("/index.jsp").forward(request, response);
                return;
            } catch (InstantiationException ex) {
                request.setAttribute("MainErrMsg", beanName + " can not be instantiated");
                this.filterConfig.getServletContext().getRequestDispatcher("/index.jsp").forward(request, response);
                return;
            } catch (IllegalAccessException ex) {
                request.setAttribute("MainErrMsg", beanName + " not access");
                this.filterConfig.getServletContext().getRequestDispatcher("/index.jsp").forward(request, response);
                return;
            }
        }
               
        if(beanObject != null) {
            Field[] fields = beanClazz.getDeclaredFields();

            for(Field field : fields) {
                Method beanMethod = null;
                String fieldName = field.getName();
                String fieldValue = ((HttpServletRequest)request).getParameter(fieldName);
                StringBuilder sb = new StringBuilder();
                sb.append("set");
                sb.append(fieldName.substring(0, 1).toUpperCase());
                sb.append(fieldName.substring(1));
               
                try {
                    beanMethod = beanClazz.getDeclaredMethod(sb.toString(), String.class);
                } catch (SecurityException ex) {
                    request.setAttribute("MainErrMsg", " Security reason ");
                    this.filterConfig.getServletContext().getRequestDispatcher("/index.jsp").forward(request, response);
                    return;
                } catch (NoSuchMethodException ex) {
                    request.setAttribute("MainErrMsg", beanName + " does not have method named " + sb.toString());
                    this.filterConfig.getServletContext().getRequestDispatcher("/index.jsp").forward(request, response);
                    return;
                }
    • 评论
    • 分享微博
    • 分享邮件
    邮件订阅

    如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。

    重磅专题
    往期文章
    最新文章