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



我有一个请求对象,可能与这个类有关: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:如何打印请求参数?

我的硬编码尝试

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

但是如何在不对数组中的属性进行硬编码的情况下做到这一点呢?

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

离开 1990 年代的过去,停止将 java 代码放在 JSP 文件中。

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

别这样

如果您拒绝执行上述操作, 将所需的类导入到 JSP 文件中。 我不会包括将文件导入 JSP 文件的语法, 但请放心, 谷歌对这些事情的内疚比我少。

最新更新