JSP getAttribute()返回null [英] JSP getAttribute() returning null
问题描述
所以在我的Servlet中,我有以下内容:
So in my Servlet I have the following:
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);
}
我的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 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>
所以我只是运行get,只剩下这个servlet.
So I'm just running the get, have only this servlet nothing else.
我知道我应该使用JSTL,但这是我检查这不是JSTL问题,而是一些Java问题的方法.有人有什么想法吗?
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?
PS:如果我只是执行<%= request %>
,我会得到org.apache.catalina.connector.RequestFacade@58c3fbeb
,因此问题不在于不将结果强制转换为String.
如果我在servlet System.out.println(req);
上执行此操作,则会得到org.apache.catalina.connector.RequestFacade@4ac37ce2
,这意味着由于某种原因,传递和接收的请求是不同的?
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?
结果:事实证明,由于某种原因,IDE做了一些奇怪的事情,并在转发中引入了此问题.当我将其与tomcat上的maven编译的WAR文件一起部署时,它可以正常工作.
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.
推荐答案
您没有将其类型转换为String.
request.getAttribute()
将返回Object
.
You are not typecasting it to String.
request.getAttribute()
will return an Object
.
尝试使用它,看看是否有效:
Try using this and see if it works:
String value = (String)request.getAttribute("colnames");
或
<%= (String)request.getAttribute("colNames") %>
为什么在这里使用forEach
?您只需要显示String
,对吗?
另外,var="col"
不应------> var = "colNames"
Why are you using forEach
here? You just need to display a String
right?
Also, shouldn't var="col"
be ------> var = "colNames"
<tr>
<c:forEach items="${param.colNames}" var="col">
<td>${col}</td>
</c:forEach>
</tr>
这篇关于JSP getAttribute()返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!