Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSP getAttribute() returning null

Tags:

java

jsp

servlets

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'm just running the get, have only this servlet nothing else.

I know that I should use JSTL and I am, but this was my way of checking that it was not a JSTL problem but some what a java problem. Anyone have any ideas?

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

Result: It turned out that for some reason the IDE is doing some weird stuff and introduced this problem in the forwarding. When I deployed it on with maven compiled WAR file on the tomcat it worked okay.

like image 796
Alex Botev Avatar asked Aug 28 '13 14:08

Alex Botev


People also ask

Why is getAttribute returning NULL?

Element.getAttribute() The getAttribute() method of the Element interface returns the value of a specified attribute on the element. If the given attribute does not exist, the value returned will either be null or "" (the empty string); see Non-existing attributes for details.

How can I get getAttribute in JSP?

1) First create data at the server side and pass it to a JSP. Here a list of student objects in a servlet will be created and pass it to a JSP using setAttribute(). 2) Next, the JSP will retrieve the sent data using getAttribute(). 3) Finally, the JSP will display the data retrieved, in a tabular form.

What is the difference between getAttribute () and getParameter () methods?

getParameter() returns http request parameters. Those passed from the client to the server. getAttribute() is for server-side usage only - you fill the request with attributes that you can use within the same request. For example - you set an attribute in a servlet, and read it from a JSP.

What is the return type of getAttribute in servlet request?

getAttribute. Returns the value of the named attribute as an Object , or null if no attribute of the given name exists. Attributes can be set two ways. The servlet container may set attributes to make available custom information about a request.


2 Answers

You are not typecasting 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? You just need to display a String right? Also, shouldn't var="col" be ------> var = "colNames"

     <tr>
        <c:forEach items="${param.colNames}" var="col">
            <td>${col}</td>
        </c:forEach>
    </tr>
like image 67
JNL Avatar answered Oct 05 '22 01:10

JNL


You need to use,

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

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

like image 42
ChadNC Avatar answered Oct 05 '22 01:10

ChadNC