JSP getAttribute() 返回 null

6
在我的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);
}

我的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,没有别的。

我知道我应该使用JSTL,并且我正在使用,但这是我检查它不是JSTL问题而是一些Java问题的方式。有人有任何想法吗?

PS:如果我只做<%= request %>,我得到org.apache.catalina.connector.RequestFacade@58c3fbeb,所以问题不在于不将结果强制转换为String。
而且,如果我在servlet上执行System.out.println(req);,我会得到org.apache.catalina.connector.RequestFacade@4ac37ce2,这意味着某种原因导致传递和接收的请求不同?

结果:事实证明,由于IDE正在执行一些奇怪的操作并引入了此转发问题。当我在tomcat上部署使用maven编译的WAR文件时,它可以正常工作。


先尝试一个简单的字符串,以确定问题是出在数组还是分派上。 - Charaf JRA
1
你能展示你的实际代码和准确的流程吗? - Rohit Jain
仍然是 null,这是一个调度问题,但我想不出任何解决方法。 - Alex Botev
尝试过了,但不幸的是还是不行。我的web.xml文件没问题吧? - Alex Botev
我尝试使用String[],但没有成功,为了简单起见,我将其更改为只有String。 - Alex Botev
显示剩余6条评论
3个回答

7
您没有将其转换为字符串。 request.getAttribute()将返回一个Object对象。
尝试使用以下代码,看看是否可行:
String value = (String)request.getAttribute("colnames");

或者
<%= (String)request.getAttribute("colNames") %>

为什么你在这里使用forEach?你只需要显示一个String,对吧?此外,var="col" 不应该是变成 var = "colNames" 吗?
     <tr>
        <c:forEach items="${param.colNames}" var="col">
            <td>${col}</td>
        </c:forEach>
    </tr>

1

你需要使用,

request.getSession().setAttribute("colNames",yourObject);

通过请求/响应将其持久化,然后从会话中提取到您的JSP页面。

0

对我来说,这是与Tomcat服务器7有关的问题。我将其更新为8.5,现在它可以正常工作了。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接