⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 debugbean.java

📁 大家好啊 快来抢购J2ME东东 挺不错的啊 不要后悔啊 抓住机会
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package com.ora.jsp.util;

import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.jsp.*;

/**
 * This class is a bean that can be used to extract debug
 * information from a JSP PageContext. The debug info is
 * sent to the browser, System.out, and the servlet log
 * file, depending on the value of the "debug" request
 * parameter sent with the request for the JSP page:
 * "resp", "stdout" and "log". The values can be combined to
 * get the information directed to multiple targets.
 *
 * @author Hans Bergsten, Gefion software <hans@gefionsoftware.com>
 * @version 1.0.1
 */
public class DebugBean {
    static private final String LINE_FEED = System.getProperty("line.separator");
    private PageContext pageContext;
    private long startTime;
    private String debugType;
    private ServletContext context;
    
    /**
     * Creates an instance and initializes the timer.
     */
    public DebugBean() {
        startTime = System.currentTimeMillis();
    }
    
    /**
     * Sets the pageContext property.
     */
    public void setPageContext(PageContext pageContext) {
        this.pageContext = pageContext;
    }
    
    /**
     * Returns a String with the number of milliseconds that
     * has passed since the bean was created or the last time
     * this method was called.
     */
    public String getElapsedTime() {
        String elapsedTime = 
            new Long(System.currentTimeMillis() - startTime).toString() + 
                " ms";
        startTime = System.currentTimeMillis();
        return handleMsg("elapsedTime", elapsedTime);
    }
    
    /**
     * Returns a String with all basic request information.
     */
    public String getRequestInfo() {
        if (pageContext == null) {
            throw new IllegalStateException("The pageContext property is not set");
        }
        
        Hashtable info = new Hashtable();
        HttpServletRequest req = (HttpServletRequest) pageContext.getRequest();
        info.put("authType", nullToString(req.getAuthType()));
        info.put("characterEncoding", nullToString(req.getCharacterEncoding()));
        info.put("contentLength", new Integer(req.getContentLength()).toString());
        info.put("contentType", nullToString(req.getContentType()));
        info.put("contextPath", nullToString(req.getContextPath()));
        info.put("pathInfo", nullToString(req.getPathInfo()));
        info.put("protocol", nullToString(req.getProtocol()));
        info.put("queryString", nullToString(req.getQueryString()));
        info.put("remoteAddr", nullToString(req.getRemoteAddr()));
        info.put("remoteHost", nullToString(req.getRemoteHost()));
        info.put("remoteUser", nullToString(req.getRemoteUser()));
        info.put("requestURI", nullToString(req.getRequestURI()));
        info.put("scheme", nullToString(req.getScheme()));
        info.put("serverName", nullToString(req.getServerName()));
        info.put("serverPort", new Integer(req.getServerPort()).toString());
        info.put("servletPath", nullToString(req.getServletPath()));
        return handleMsg("requestInfo", info);
    }
    
    /**
     * Returns a String with all header information.
     */
    public String getHeaders() {
        if (pageContext == null) {
            throw new IllegalStateException("The pageContext property is not set");
        }
        
        Hashtable info = new Hashtable();
        HttpServletRequest req = (HttpServletRequest) pageContext.getRequest();
        Enumeration names = req.getHeaderNames();
        while (names.hasMoreElements()) {
            String name = (String) names.nextElement();
            Enumeration values = req.getHeaders(name);
            StringBuffer sb = new StringBuffer();
            boolean first = true;
            while (values.hasMoreElements()) {
                if (!first) {
                    sb.append(" | ");
                }
                first = false;
                sb.append(values.nextElement());
            }
            info.put(name, sb.toString());
        }
        return handleMsg("headers", info);
    }
    
    /**
     * Returns a String with all cookie information.
     */
    public String getCookies() {
        if (pageContext == null) {
            throw new IllegalStateException("The pageContext property is not set");
        }
        
        Hashtable info = new Hashtable();
        HttpServletRequest req = (HttpServletRequest) pageContext.getRequest();
        Cookie[] cookies = req.getCookies();
        for (int i = 0; i < cookies.length; i++) {
            info.put(cookies[i].getName(), cookies[i].getValue());
        }
        return handleMsg("cookies", info);
    }
    
    /**
     * Returns a String with all request parameter information.
     */
    public String getParameters() {
        if (pageContext == null) {
            throw new IllegalStateException("The pageContext property is not set");
        }
        
        Hashtable info = new Hashtable();
        HttpServletRequest req = (HttpServletRequest) pageContext.getRequest();
        Enumeration names = req.getParameterNames();
        while (names.hasMoreElements()) {
            String name = (String) names.nextElement();
            String[] values = req.getParameterValues(name);
            StringBuffer sb = new StringBuffer();
            for (int i = 0; i < values.length; i++) {
                if (i != 0) {
                    sb.append(" | ");
                }
                sb.append(values[i]);
            }
            info.put(name, sb.toString());
        }
        return handleMsg("parameters", info);
    }
    
    /**
     * Returns a String with all request scope variables.
     */
    public String getRequestScope() {
        if (pageContext == null) {
            throw new IllegalStateException("The pageContext property is not set");
        }
        
        Hashtable info = new Hashtable();
        HttpServletRequest req = (HttpServletRequest) pageContext.getRequest();
        Enumeration names = req.getAttributeNames();
        while (names.hasMoreElements()) {
            String name = (String) names.nextElement();
            Object value = req.getAttribute(name);
            info.put(name, toStringValue(value));
        }
        return handleMsg("requestScope", info);
    }
    
    /**
     * Returns a String with all page scope variables.
     */
    public String getPageScope() {
        if (pageContext == null) {
            throw new IllegalStateException("The pageContext property is not set");
        }
        
        Hashtable info = new Hashtable();
        Enumeration names = 
            pageContext.getAttributeNamesInScope(PageContext.PAGE_SCOPE);
        while (names.hasMoreElements()) {
            String name = (String) names.nextElement();
            Object value = pageContext.getAttribute(name);
            info.put(name, toStringValue(value));
        }
        return handleMsg("pageScope", info);
    }
    
    /**
     * Returns a String with all session scope variables.
     */
    public String getSessionScope() {
        if (pageContext == null) {
            throw new IllegalStateException("The pageContext property is not set");
        }
        
        Hashtable info = new Hashtable();
        HttpServletRequest req = (HttpServletRequest) pageContext.getRequest();
        HttpSession session = req.getSession();
        Enumeration names = session.getAttributeNames();
        while (names.hasMoreElements()) {
            String name = (String) names.nextElement();
            Object value = session.getAttribute(name);
            info.put(name, toStringValue(value));
        }
        return handleMsg("sessionScope", info);
    }
    
    /**
     * Returns a String with all application scope variables.
     */
    public String getApplicationScope() {
        if (pageContext == null) {
            throw new IllegalStateException("The pageContext property is not set");
        }
        
        Hashtable info = new Hashtable();
        ServletContext context = pageContext.getServletContext();
        Enumeration names = context.getAttributeNames();
        while (names.hasMoreElements()) {
            String name = (String) names.nextElement();
            Object value = context.getAttribute(name);
            info.put(name, toStringValue(value));
        }
        return handleMsg("applicationScope", info);
    }
    
    /**
     * Returns the String "null" if the value is null,

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -