【问题标题】:JSP getAttribute() returning nullJSP getAttribute() 返回 null
【发布时间】:2013-08-28 14:17:23
【问题描述】:

所以在我的 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:如果我只做&lt;%= request %&gt;,我会得到org.apache.catalina.connector.RequestFacade@58c3fbeb,所以问题不在于没有将结果转换为字符串。
如果我在 servlet System.out.println(req); 上执行操作,我会得到 org.apache.catalina.connector.RequestFacade@4ac37ce2,这意味着由于某种原因传递和接收的请求不同?

结果:事实证明,由于某种原因,IDE 做了一些奇怪的事情,并在转发中引入了这个问题。当我在 tomcat 上使用 maven 编译的 WAR 文件部署它时,它工作正常。

【问题讨论】:

  • 首先尝试一个简单的字符串,以了解您的数组或调度是否存在问题
  • 你能用准确的流程展示你的实际代码吗?
  • 仍然为空,这是一个调度问题,但我想不出任何方法。
  • 你是否被重定向到 index.php ,这是你得到空值的地方?向我们展示你的 jsp 页面
  • 我添加了执行的确切代码。

标签: java jsp servlets


【解决方案1】:

您没有将其类型转换为字符串。 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>

【讨论】:

    【解决方案2】:

    你需要使用,

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

    通过请求/响应将其持久化,然后将其从 JSP 页面上的会话中拉出。

    【讨论】:

      【解决方案3】:

      对我来说,这是 tomcat 服务器 7 的问题。我将它更新到 8.5,现在它可以正常工作了。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2010-11-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-03-23
        • 2021-03-09
        • 1970-01-01
        相关资源
        最近更新 更多