java - JSP getAttribute() returning null -
so in servlet have 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); }
my 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>
so i'm running get, have servlet nothing else.
i know should use jstl , am, way of checking not jstl problem java problem. have ideas?
ps: if <%= request %>
org.apache.catalina.connector.requestfacade@58c3fbeb
problem not in not casting result string.
, if on servlet system.out.println(req);
org.apache.catalina.connector.requestfacade@4ac37ce2
, means reason request passed , received different?
result: turned out reason ide doing weird stuff , introduced problem in forwarding. when deployed on maven compiled war file on tomcat worked okay.
you not typecasting string. request.getattribute()
return object
.
try using , see if works:
string value = (string)request.getattribute("colnames");
or
<%= (string)request.getattribute("colnames") %>
why using foreach
here? need display string
right? also, shouldn't var="col"
be ------> var = "colnames"
<tr> <c:foreach items="${param.colnames}" var="col"> <td>${col}</td> </c:foreach> </tr>
Comments
Post a Comment