Java 返回null的JSP getAttribute()
因此,在我的Servlet中,我有以下内容:Java 返回null的JSP getAttribute(),java,jsp,servlets,Java,Jsp,Servlets,因此,在我的Servlet中,我有以下内容: public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.setContentType("text/html"); req.setAttribute("colNames","ka"); req.setAttribut
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>
新宝石订单信息
${col}
${value}
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>
org.springframework.web.context.ContextLoaderListener
EntityDumpServlet
倾倒
EntityDumpServlet
com.jpmorgan.d1.ptg.web.EntityDumpServlet
1.
EntityDumpServlet
/倾倒
所以我只是运行get,只有这个servlet,没有其他功能
我知道我应该使用JSTL,但这是我检查这不是JSTL问题而是java问题的方法。有人有什么想法吗
PS:如果我只做
我会得到org.apache.catalina.connector。RequestFacade@58c3fbeb
因此问题不在于不将结果强制转换为字符串。如果我在servlet
System.out.println(req)上这样做代码>我得到org.apache.catalina.connector。RequestFacade@4ac37ce2
,这意味着由于某种原因,传递和接收的请求不同
结果:由于某种原因,IDE正在做一些奇怪的事情,并在转发中引入了这个问题。当我在tomcat上使用maven编译的WAR文件部署它时,它工作正常。您没有将它键入字符串。
request.getAttribute()
将返回一个对象
尝试使用此选项,看看是否有效:
String value = (String)request.getAttribute("colnames");
或
你需要使用
request.getSession().setAttribute("colNames",yourObject);
通过请求/响应将其持久化,然后将其从JSP页面上的会话中拉出。对我来说,这是tomcat服务器7的问题。我将其更新为8.5,现在运行良好。首先尝试一个简单的字符串,以了解问题是与数组有关还是与Dispatching有关。您能否以精确的流显示实际代码。仍然为空,这是一个分派问题,但我无法找到任何方法。您是否重定向到index.php,这就是你得到空值的地方?显示你的jsp页面我添加了执行的确切代码。
<tr>
<c:forEach items="${param.colNames}" var="col">
<td>${col}</td>
</c:forEach>
</tr>
request.getSession().setAttribute("colNames",yourObject);