有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何打印请求对象的属性和值?

我有一个可能属于这个类的请求对象:https://tomcat.apache.org/tomcat-6.0-doc/api/org/apache/catalina/connector/RequestFacade.html

我试图打印属性的名称及其值。这是我徒劳的尝试

Enumeration rns = request.getAttributeNames();

while (rns.hasMoreElements()) {
    Object param = rns.nextElement();
    out.println("\nattribute name: " + param.toString());
    out.println("attribute value: " + request.getAttribute(param.toString()));
}

我错过了什么?在另一种语言中,这样的问题有一个微不足道的答案。在java中做的有多不同? Rails: How do I print the request parameters?

我的硬编码尝试

   Enumeration rns = request.getAttributeNames();
    while (rns.hasMoreElements()) {
        out.println("\nattribute name: " + rns.nextElement() );
    }
    // i have copied the logs output from above and edited it to create the array below
    String[] madAttrs = { "dayToGraph",
                          "yearFromGraph",
                          "yaxis",
                          "yearToGraph",
                          "monthToGraph",
                          "monthFromGraph",
                          "currentMachine",
                          "onlyGraph",
                          "currentSample",
                          "currentAnalyte",
                          "dayFromGraph",
                          "analyteid"};
    // then I got what I want.
    for (String an : madAttrs) {
        out.println("sttribute " + an);
        out.println("the value is: "+ request.getAttribute(an));
    }

但是,如果不对数组中的属性进行硬编码,该如何做呢


共 (1) 个答案

  1. # 1 楼答案

    不是你想要的答案,而是你问题的真正答案

    让90年代成为过去,停止在JSP文件中使用java代码

    而是在servlet(或Spring处理程序)中接收消息,并在其中执行java工作。 接下来,创建显示值并将它们放在某个上下文(可能是响应)中,然后将请求发送到JSP页面

    不要这样做

    如果你拒绝做上述事情, 将所需的类导入JSP文件。 我将不包括将文件导入JSP文件的语法, 但请放心, 谷歌对这些事情的内疚比我少