Frage

I am having problem printing(Displaying) array data from JSP page which is send from Servlet. Here are the code and the out of the JSP page. Kindly, help me to trace the error i am making.

MY SERVLET CODE:

//inside doGET method
String title="Reading cookies example";
String[] info={"What the hell!!","Nothing"};

request.setAttribute("title", title);
request.setAttribute("name", info);

RequestDispatcher rd=request.getRequestDispatcher("myjsp.jsp");
rd.forward(request, response);

MY JSP CODE:

<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%-- 
    Document   : myjsp
    Created on : Apr 5, 2014, 1:19:35 PM
    Author     : sabin
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <c:forEach var="pagetitle" items="title">
            <h1>${title}</h1>
        </c:forEach>
        <c:forEach var="data" items="{name}" >
            <C:OUT value="${data}">  </C:OUT> 
            <h1>${name}</h1>

        </c:forEach>


   </body>
</html>

OUTPUT PAGE:

Reading cookies example [Ljava.lang.String;@195019d

Instead of printing "What the hell" and Nothing its printing [Ljava.lang.String;@195019d. What is the reason and what i am missing? Would be very thankful if someone could assist me with this problem. Thanking you in Advance.

War es hilfreich?

Lösung

try this ,

    ${title}
    <c:forEach var="temp" items="${name}">
        <c:out value="${temp}"></c:out>
    </c:forEach>

Because title is not an array its just a string also read this to understand the functionality of for-each tag will print output like this ,

Reading cookies example What the hell!! Nothing

Andere Tipps

Please correct foreach loop as below

 <c:forEach var="data" items="${requestScope.name}" >

Your code should be like this

<c:forEach var="data" items="${requestScope.name}" >
            <c:out value="${data}">  </c:out> 
            <h1>${name}</h1>
</c:forEach>


1) <c:out value="${name}" />
2) <c:out value="${requestScope.name}" />

requestScope when you absoluetely want your object to come from the request, and not from the page, session or application scope , while using ${name} will search for a name attribute in the page, then in the request, then in the session, then in the application.

Let's say that some other code in the JSP set a name attribute in the page scope. But you want to access the name in the request: you're forced to use requestScope.

Let's say the session might have a name attribute. Not using requestScope.name would return the session-scoped name if the JSP forgot to set the name attribute in the request scope.

If the goal of the JSP fragment is to access something set in the enclosing JSP, maybe this JSP fragment should be a JSP tag, and you should pass the name as an argument to this tag.

Putting a table format

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>myjsp.jsp</title>
</head>
<body>
    <h1>${title} </h1>
    <br/><br/><br/>
    <table border="1">
             <c:forEach var="temp" items="${name}">     
            <tr>              
                <td>
                    <c:out value="${temp}" escapeXml="false" ></c:out>
                </td>
            </tr>
        </c:forEach>
    </table>
    
</body>
</html>
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top