JSP getAttribute () returns null

So, in my Servlet, I have the 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 just run get, I only have this servlet.

I know that I should use JSTL, and it is, but it was my way of checking that this is not a JSTL problem, but some kind of java problem. Does anyone have any ideas?

PS: If I only do <%= request %> , I get org.apache.catalina.connector.RequestFacade@58c3fbeb , so the problem is not that the result is not passed to String.
And if I do on the servlet System.out.println(req); I get org.apache.catalina.connector.RequestFacade@4ac37ce2 , which means that for some reason, the sent and received request is different?

Result: it turned out that for some reason, the IDE does some strange things and introduces this problem into forwarding. When I deployed it with a WAR file compiled in tomcat maev, it worked fine.

+4
source share
3 answers

You do not attribute it to String. request.getAttribute() will return an Object .

Try using this and see if it works:

 String value = (String)request.getAttribute("colnames"); 

Or

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

Why are you using forEach here? Do you just need to display String to the right? Also, you should not var="col" be ------> var = "colNames"

  <tr> <c:forEach items="${param.colNames}" var="col"> <td>${col}</td> </c:forEach> </tr> 
+4
source

You need to use

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

To continue it through the request / response, and then pull it out of the session on the JSP page.

+1
source

For me it was a problem with the tomcat 7 server. I upgraded it to 8.5 and now it works fine.

0
source

Source: https://habr.com/ru/post/1499410/


All Articles