1. 程式人生 > >如何獲得Http Request中的資訊

如何獲得Http Request中的資訊

一般情況都是,寫一個全域性的過濾器,通過這個過濾器來對所有request中的內容進行輸出,核心程式碼:

@Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)
        throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        logger.info(request.getRequestURL());


        logger.info("=========================Header Attribute========================"
); Enumeration<?> e = request.getHeaderNames(); while (e.hasMoreElements()) { String name = (String) e.nextElement(); String value = request.getHeader(name); logger.info(name + " = " + value); } logger.info("=========================Request Value========================"
); Enumeration<?> eq = request.getParameterNames(); while (eq.hasMoreElements()) { String name = (String) eq.nextElement(); String value = request.getParameter(name); logger.info(name + " = " + value); } filterChain.doFilter(servletRequest, servletResponse); }