简体   繁体   English

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

[英]java - How do I print attributes and values of a request object?

I have a request object that probably belons to this class: https://tomcat.apache.org/tomcat-6.0-doc/api/org/apache/catalina/connector/RequestFacade.html 我有一个可能属于此类的请求对象: https ://tomcat.apache.org/tomcat-6.0-doc/api/org/apache/catalina/connector/RequestFacade.html

I am trying to print the names of the attributes and their values. 我正在尝试打印属性的名称及其值。 This is my futile attempt. 这是我徒劳的尝试。

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

What I am missing? 我缺少什么? In another language there is a trivial answer to such question. 用另一种语言对这个问题有一个简单的答案。 How different it is done in java? 它在Java中有何不同? Rails: How do I print the request parameters? Rails:如何打印请求参数?

my hardcoded attempt 我的硬编码尝试

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

But how do I do it without hardcoding the attributes in the array? 但是,如何在不对数组中的属性进行硬编码的情况下做到这一点呢?

Not the answer you want, but the real answer to your question 不是您想要的答案,而是您问题的真正答案

Leave the 1990s in the past and stop putting java code in JSP files. 过去的1990年代,不再将Java代码放入JSP文件中。

Instead receive the message in a servlet (or Spring handler) and do java work therein. 取而代之的是在servlet(或Spring处理程序)中接收消息,并在其中执行Java工作。 Next create the display values and put them in some context (perhaps the response) and dispatch the request to your JSP page. 接下来,创建显示值并将它们放在某个上下文中(也许是响应),然后将请求分派到您的JSP页面。

Don't do this 不要这样

If you refuse to do the above, import the required classes into your JSP file. 如果您拒绝执行上述操作,请将所需的类导入到JSP文件中。 I will not include the syntax for importing files into a JSP file, but be assured, Google has fewer compunctions about such things than do I. 我不会介绍将文件导入到JSP文件中的语法,但是可以肯定的是,与我相比,Google在此类事情上的麻烦更少。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM