JSP getAttribute()返回null

Ale*_*tev 6 java jsp servlets

所以在我的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);
}
Run Code Online (Sandbox Code Playgroud)

我的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> 
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

所以我只是运行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文件部署它时,它运行正常.

JNL*_*JNL 7

你没有将它类型转换为String. request.getAttribute()会回来的Object.

尝试使用它,看看它是否有效:

String value = (String)request.getAttribute("colnames");
Run Code Online (Sandbox Code Playgroud)

要么

<%= (String)request.getAttribute("colNames") %>
Run Code Online (Sandbox Code Playgroud)

你为什么在这里使用forEach?你只需要显示一个String权利?另外,不var="col" 应该------> var = "colNames"

     <tr>
        <c:forEach items="${param.colNames}" var="col">
            <td>${col}</td>
        </c:forEach>
    </tr>
Run Code Online (Sandbox Code Playgroud)