Java JSP getAttribute() 返回 null
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18490517/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JSP getAttribute() returning null
提问by Alex Botev
So in my Servlet I have the following:
所以在我的 Servlet 中,我有以下内容:
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
resp.setContentType("text/html");
req.setAttribute("colNames","ka");
req.setAttribute("items", new String[]{});
//System.out.println(req.getAttribute("colNames"));
req.getRequestDispatcher("/index.jsp").forward(req,resp);
}
My Servlet:
我的小服务程序:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page isELIgnored="false"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>NewGem OrderInfo</title>
<script src="sorttable.js"></script>
</head>
<body>
<%= request.getAttribute("colNames") %>
<table id="table" class="sortable">
<tr>
<c:forEach items="${param.colNames}" var="col">
<td>${col}</td>
</c:forEach>
</tr>
<c:forEach items="${param.items}" var="row">
<tr>
<c:forEach items="${row.elements()}" var="value">
<td>${value}</td>
</c:forEach>
</tr>
</c:forEach>
</table>
</body>
</html>
web.xml:
网页.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<display-name>EntityDumpServlet</display-name>
<welcome-file-list>
<welcome-file>dump</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>EntityDumpServlet</servlet-name>
<servlet-class>
com.jpmorgan.d1.ptg.web.EntityDumpServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>EntityDumpServlet</servlet-name>
<url-pattern>/dump</url-pattern>
</servlet-mapping>
</web-app>
So I'm just running the get, have only this servlet nothing else.
所以我只是运行 get,只有这个 servlet 没有别的。
I know that I should use JSTL and I am, but this was my way of checking that it was not a JSTL problem but some what a java problem. Anyone have any ideas?
我知道我应该使用 JSTL 并且我是,但这是我检查它不是 JSTL 问题而是一些 java 问题的方法。谁有想法?
PS: If I do just <%= request %>
I get org.apache.catalina.connector.RequestFacade@58c3fbeb
so the problem is not in not casting the result to String.
And if I do on the servlet System.out.println(req);
I get org.apache.catalina.connector.RequestFacade@4ac37ce2
, which means for some reason the request passed and received are different?
PS:如果我不只是<%= request %>
我得到org.apache.catalina.connector.RequestFacade@58c3fbeb
这样的问题是不是在不投结果为String。
如果我在System.out.println(req);
我得到的 servlet 上做org.apache.catalina.connector.RequestFacade@4ac37ce2
,这意味着由于某种原因传递和接收的请求是不同的?
Result: It turned out that for some reason the IDE is doing some weird stuff and introduced this problem in the forwarding. When I deployed it on with maven compiled WAR file on the tomcat it worked okay.
结果:事实证明,出于某种原因,IDE 正在做一些奇怪的事情,并在转发中引入了这个问题。当我在 tomcat 上使用 maven 编译的 WAR 文件部署它时,它工作正常。
回答by JNL
You are not typecasting it to String.
request.getAttribute()
will return an Object
.
您没有将其类型转换为 String。
request.getAttribute()
将返回一个Object
.
Try using this and see if it works:
尝试使用它,看看它是否有效:
String value = (String)request.getAttribute("colnames");
Or
或者
<%= (String)request.getAttribute("colNames") %>
Why are you using forEach
here? You just need to display a String
right?
Also, shouldn't var="col"
be ------>var = "colNames"
你为什么用forEach
在这里?你只需要显示一个String
吧?另外,不应该var="col"
是------>var = "colNames"
<tr>
<c:forEach items="${param.colNames}" var="col">
<td>${col}</td>
</c:forEach>
</tr>
回答by ChadNC
You need to use,
你需要使用,
request.getSession().setAttribute("colNames",yourObject);
To persist it through the request/response and then pull it out of the session on your JSP page.
通过请求/响应将其持久化,然后将其从 JSP 页面上的会话中拉出。
回答by wiTcH_doCtoR
For me it was an issue with the tomcat server 7. I updated it to 8.5 and its working fine now.
对我来说,这是 tomcat 服务器 7 的问题。我将其更新到 8.5,现在工作正常。