JSTL Question

Discussion in 'Java' started by Mongoose, Oct 9, 2009.

  1. Mongoose

    Mongoose Guest

    Hi All,

    I have an arraylist of objects. Each object has 3 properties. They
    are:

    DefectID - Integer
    Description - VARCHAR
    PriorityID - Integer

    I hit my Oracle database and poplulate a list with the aforementioned
    objects. I'm just trying to display the list in my .jsp page. Can
    someone please tell me what is wrong the code in my .jsp page that is
    shown below?

    Thanks

    -------------------------------------------------------------------------------------------------------------------------------------------------

    <%@page import="java.util.ArrayList"%>
    <%@page import="EricEnhancementServiceImpl" %>
    <%@ page import="java.util.*" %>
    <%
    List results = new ArrayList();
    EricEnhancementServiceImpl E = new EricEnhancementServiceImpl();
    results = E.getDefects1();

    pageContext.setAttribute("defects", results, pageContext.PAGE_SCOPE);

    %>



    <html>
    <head>
    <title>display</title>
    <meta http-equiv="Content-Type" content="text/html;
    charset=ISO-8859-1">
    </head>
    <body>


    <table border="1" width="80%">
    <tr><th>Description</th></tr>
    <c:forEach var="defect" items="${PageScope.defects}">
    <tr>

    <td><c:eek:ut value="${defect.Description}"/></td>

    </tr>
    </c:forEach>
    </table>
    </body>
    </html>
     
    Mongoose, Oct 9, 2009
    #1
    1. Advertising

  2. Mongoose

    Lew Guest

    Mongoose wrote:
    > I have an arraylist [sic] of objects.  Each object has 3 properties.  They
    > are:
    >
    > DefectID          - Integer
    > Description      - VARCHAR [sic]
    > PriorityID         - Integer
    >
    > I hit my Oracle database and poplulate a list with the aforementioned
    > objects.  I'm just trying to display the list in my .jsp [sic] page.  Can
    > someone please tell me what is wrong the code in my .jsp [sic] page that is
    > shown below?
    >
    > Thanks
    >
    > -------------------------------------------------------------------------------------------------------------------------------------------------
    >
    > <%@page import="java.util.ArrayList"%>
    > <%@page import="EricEnhancementServiceImpl" %>
    > <%@ page import="java.util.*" %>
    > <%
    >         List results = new ArrayList();
    >         EricEnhancementServiceImpl E = new EricEnhancementServiceImpl();
    >         results = E.getDefects1();
    >
    >         pageContext.setAttribute("defects", results, pageContext.PAGE_SCOPE);
    >
    >  %>
    >
    > <html>
    > <head>
    > <title>display</title>
    > <meta http-equiv="Content-Type" content="text/html;
    > charset=ISO-8859-1">
    > </head>
    > <body>
    >
    > <table border="1" width="80%">
    >         <tr><th>Description</th></tr>
    >         <c:forEach var="defect" items="${PageScope.defects}">
    >                 <tr>
    >
    >                         <td><c:eek:ut value="${defect.Description}"/></td>
    >
    >                 </tr>
    >         </c:forEach>
    > </table>
    > </body>
    > </html>


    You don't tell us what, if any, error message(s) you receive. Is it
    something about "defect" not having a "Description" attribute? As far
    as I can see, you have given the compiler no clue that the type of the
    'List' element is anything other than 'Object'.

    By universal Java convention, variables and attributes should be named
    with an initial lower-case letter.

    JSP trainers and book writers like Marty Hall strongly recommend
    against scriptlet in JSPs.

    Oracle uses VARCHAR2 as a data type, not VARCHAR. Since VARCHAR is
    not a Java type, there is no way that your ArrayList elements have
    that as an attribute type.

    Any of these things could contribute to whatever problem you might be
    having, but since you haven't told us what problem you're having or
    even if you actually are having a problem, I cannot be sure.

    --
    Lew
     
    Lew, Oct 9, 2009
    #2
    1. Advertising

  3. Mongoose

    Mongoose Guest

    On Oct 9, 2:00 pm, Lew <> wrote:
    > Mongoose wrote:
    > > I have an arraylist [sic] of objects.  Each object has 3 properties.  They
    > > are:

    >
    > > DefectID          - Integer
    > > Description      - VARCHAR [sic]
    > > PriorityID         - Integer

    >
    > > I hit my Oracle database and poplulate a list with the aforementioned
    > > objects.  I'm just trying to display the list in my .jsp [sic] page.  Can
    > > someone please tell me what is wrong the code in my .jsp [sic] page that is
    > > shown below?

    >
    > > Thanks

    >
    > > ---------------------------------------------------------------------------­----------------------------------------------------------------------

    >
    > > <%@page import="java.util.ArrayList"%>
    > > <%@page import="EricEnhancementServiceImpl" %>
    > > <%@ page import="java.util.*" %>
    > > <%
    > >         List results = new ArrayList();
    > >         EricEnhancementServiceImpl E = new EricEnhancementServiceImpl();
    > >         results = E.getDefects1();

    >
    > >         pageContext.setAttribute("defects", results, pageContext.PAGE_SCOPE);

    >
    > >  %>

    >
    > > <html>
    > > <head>
    > > <title>display</title>
    > > <meta http-equiv="Content-Type" content="text/html;
    > > charset=ISO-8859-1">
    > > </head>
    > > <body>

    >
    > > <table border="1" width="80%">
    > >         <tr><th>Description</th></tr>
    > >         <c:forEach var="defect" items="${PageScope.defects}">
    > >                 <tr>

    >
    > >                         <td><c:eek:ut value="${defect.Description}"/></td>

    >
    > >                 </tr>
    > >         </c:forEach>
    > > </table>
    > > </body>
    > > </html>

    >
    > You don't tell us what, if any, error message(s) you receive.  Is it
    > something about "defect" not having a "Description" attribute?  As far
    > as I can see, you have given the compiler no clue that the type of the
    > 'List' element is anything other than 'Object'.
    >
    > By universal Java convention, variables and attributes should be named
    > with an initial lower-case letter.
    >
    > JSP trainers and book writers like Marty Hall strongly recommend
    > against scriptlet in JSPs.
    >
    > Oracle uses VARCHAR2 as a data type, not VARCHAR.  Since VARCHAR is
    > not a Java type, there is no way that your ArrayList elements have
    > that as an attribute type.
    >
    > Any of these things could contribute to whatever problem you might be
    > having, but since you haven't told us what problem you're having or
    > even if you actually are having a problem, I cannot be sure.
    >
    > --
    > Lew- Hide quoted text -
    >
    > - Show quoted text -


    Hi Lew,

    I actually don't receive any error messages. The .jsp page simply
    displays the "Description" Table Header and nothing else. Also, I
    realize that doing this is a poor practice but I was just trying to
    quickly display the output (that obviously didn't work out as
    planned..LOL). I would NEVER actually do this in any production
    application . . . I realize it is poor practice. Description is
    VARCHAR2 Oracle Datatype (typo). Since I don't get any error messages
    it's hard for me to determine what is going wrong. My guess is that
    I'm not using the correct syntax in <c:eek:ut> to refer to the
    description property of each object . . . however I'm not
    certain . . .
     
    Mongoose, Oct 9, 2009
    #3
  4. Mongoose

    Lew Guest

    Mongoose wrote:
    > I actually don't receive any error messages.  The .jsp page simply
    > displays the "Description" Table Header and nothing else.  Also, I
    > realize that doing this is a poor practice but I was just trying to
    > quickly display the output (that obviously didn't work out as
    > planned..LOL).  I would NEVER actually do this in any production
    > application . . . I realize it is poor practice.  Description is
    > VARCHAR2 Oracle Datatype (typo).  Since I don't get any error messages
    > it's hard for me to determine what is going wrong.  My guess is that
    > I'm not using the correct syntax in <c:eek:ut> to refer to the
    > description property of each object . . . however I'm not
    > certain . . .


    It's a mystery, but when I get simply no output from JSPs, I often
    find an error message in the error logs for Tomcat.

    I suspect that the compiler cannot tell that your element type is not
    'Object', and 'Object' does not have a 'Description' [sic] attribute.

    Maybe if you make your collection generic it would help. But look
    through the error log first.

    --
    Lew
     
    Lew, Oct 9, 2009
    #4
  5. Mongoose

    Lew Guest

    Lew wrote:
    > Maybe if you make your collection generic it would help.  But look
    > through the error log first.


    A few minutes with the taglib docs indicates I was wrong.

    Try dropping the "PageScope." from '<c:forEach var="defect" items="$
    {PageScope.defects}">', thus

    <c:forEach var="defect" items="${defects}">

    Do clean up the definition of your defects type so that it conforms to
    Java naming conventions.

    If you still have trouble, an SSCCE would be most useful for anyone
    trying to help you.

    Really, an SSCCE should be a prerequisite for any request for help.

    --
    Lew
     
    Lew, Oct 9, 2009
    #5
  6. On 2009-10-09 13:22:45 -0400, Mongoose <> said:

    > Hi All,
    >
    > I have an arraylist of objects. Each object has 3 properties. They
    > are:
    >
    > DefectID - Integer
    > Description - VARCHAR


    I'm fairly certain your object does not have a VARCHAR property, since
    VARCHAR isn't a (standard) Java type. Show the class definition for the
    objects in your list.

    > PriorityID - Integer


    Be aware that JSP Expression Language (EL) uses bean accessors, and
    isn't required to support properties with initial capital letters. The
    EL expression ${foo.bar} is converted into the Java expression
    foo.getBar() if there's an accessor, or foo.get('bar') if foo is a Map
    implementation, or possibly foo.bar if bar is a visible field.

    Your properties, as far as bean property names are concerned, are
    likely defectID, description, and priorityID (capitalized thus).

    > I hit my Oracle database and poplulate a list with the aforementioned
    > objects. I'm just trying to display the list in my .jsp page. Can
    > someone please tell me what is wrong the code in my .jsp page that is
    > shown below?
    >
    > Thanks
    >
    > -------------------------------------------------------------------------------------------------------------------------------------------------


    <%@page
    >
    > import="java.util.ArrayList"%>
    > <%@page import="EricEnhancementServiceImpl" %>
    > <%@ page import="java.util.*" %>
    > <%
    > List results = new ArrayList();
    > EricEnhancementServiceImpl E = new EricEnhancementServiceImpl();
    > results = E.getDefects1();
    >
    > pageContext.setAttribute("defects", results, pageContext.PAGE_SCOPE);
    >
    > %>


    Do not do this. Scriptlets in a JSP are bad form for a bunch fo
    relatively good reasons: tool support sucks, it's harder to
    automatically test, and it introduces the temptation to put *more* code
    in the page in the future. If you're not using an MVC framework of some
    kind, you can do a couple of things here:

    1. What I'd do as a first approximation: Write a servlet that loads
    E.getDefects1() into a request-scope attribute and then forwards to the
    JSP, separating the controller logic (which is mostly "load some data"
    here, but may not be in the future) into a servlet and leaving only
    view logic (below) in the JSP. The servlet's doGet method would look
    roughly like this untested snippet:

    private final EricEnhancementService enhancementService
    = new EricEnhancementServiceImpl();

    public void doGet(HttpServletRequest request,
    HttpServletResponse response) {
    List<?> results = enhancementService.getDefects1();
    request.setAttribute("defects", results);
    request.getRequestDispatcher("/WEB-INF/views/defects.jsp")
    .forward(request, response);
    }

    (I've also corrected some stylistic complaints; feel free to un-correct
    them, but take a minute to think about why I changed what I changed
    first. And if you can't figure it out, ask.)

    2. Move the above snippet into a custom JSP tag. You can write your own
    tags that declare page-scoped variables: have a look at the Orion
    taglib tutorial (it mostly uses standard EE APIs, so it can be adapted
    to other web containers):
    http://www.orionserver.com/docs/tutorials/taglibs/1.html

    > <html>
    > <head>
    > <title>display</title>
    > <meta http-equiv="Content-Type" content="text/html;
    > charset=ISO-8859-1">
    > </head>
    > <body>
    >
    >
    > <table border="1" width="80%">
    > <tr><th>Description</th></tr>
    > <c:forEach var="defect" items="${PageScope.defects}">


    There is no object named 'PageScope' in any of the scopes in your page,
    so the EL expression ${PageScope.defects} evaluates to null, so forEach
    loops over nothing. If you want to refer to the implicit page scope
    object directly, its name is 'pageScope'. However, as Lew mentioned,
    you don't need to refer to it at all here: you can simply write
    ${defects}. The JSP compiler will try the page and request scopes (in
    that order) automatically.

    You should have a quick read through the EL primer:
    http://java.sun.com/j2ee/1.4/docs/tutorial/doc/JSPIntro7.html

    Pay particular attention to the list of implicit objects, and note that
    EL (like the rest of Java) is case-sensitive.

    > <tr>
    >
    > <td><c:eek:ut value="${defect.Description}"/></td>


    As mentioned above, this is probably ${defect.description}.

    If you don't know for sure that the description attribute is free of
    HTML markup characters, you should pass escapeXml="true" to c:eek:ut as
    well. If you're absolutely sure there are no markup characters in the
    expression, and if your web.xml uses spec version 2.4 or higher, then
    you can write ${defect.descrption} without using the <c:eek:ut /> tag at
    all - the JSP engine in JSP 2 (Servlet 2.4) supports EL without any
    special support.

    >
    > </tr>
    > </c:forEach>
    > </table>
    > </body>
    > </html>


    -o
     
    Owen Jacobson, Oct 10, 2009
    #6
  7. Mongoose

    Lew Guest

    Owen Jacobson wrote:
    > You should have a quick read through the EL primer:
    > http://java.sun.com/j2ee/1.4/docs/tutorial/doc/JSPIntro7.html


    I would go with
    <http://java.sun.com/javaee/5/docs/tutorial/doc/bnahq.html>

    I can't find the equivalent chapter in the Java EE 6 Tutorial
    <http://java.sun.com/javaee/6/docs/tutorial/doc/>
    right now.

    --
    Lew
     
    Lew, Oct 10, 2009
    #7
  8. Mongoose

    Arne Vajhøj Guest

    Mongoose wrote:
    > I have an arraylist of objects. Each object has 3 properties. They
    > are:
    >
    > DefectID - Integer
    > Description - VARCHAR
    > PriorityID - Integer


    Those types are in the database - in Java they are int, String and int.

    > I hit my Oracle database and poplulate a list with the aforementioned
    > objects. I'm just trying to display the list in my .jsp page. Can
    > someone please tell me what is wrong the code in my .jsp page that is
    > shown below?


    > <%@page import="java.util.ArrayList"%>
    > <%@page import="EricEnhancementServiceImpl" %>
    > <%@ page import="java.util.*" %>
    > <%
    > List results = new ArrayList();


    No need for creating this object as you assign to result
    two lines below.

    > EricEnhancementServiceImpl E = new EricEnhancementServiceImpl();
    > results = E.getDefects1();
    >
    > pageContext.setAttribute("defects", results, pageContext.PAGE_SCOPE);
    >
    > %>
    >
    >
    >
    > <html>
    > <head>
    > <title>display</title>
    > <meta http-equiv="Content-Type" content="text/html;
    > charset=ISO-8859-1">
    > </head>
    > <body>
    >
    >
    > <table border="1" width="80%">
    > <tr><th>Description</th></tr>
    > <c:forEach var="defect" items="${PageScope.defects}">


    <c:forEach var="defect" items="${defects}">

    should be OK.

    > <tr>
    >
    > <td><c:eek:ut value="${defect.Description}"/></td>


    Try:

    <td><c:eek:ut value="${defect.description}"/></td>

    ot with a recent JSP version:

    <td>${defect.description}"</td>

    >
    > </tr>
    > </c:forEach>
    > </table>
    > </body>
    > </html>


    Arne
     
    Arne Vajhøj, Oct 11, 2009
    #8
  9. Mongoose

    Arne Vajhøj Guest

    Lew wrote:
    > Oracle uses VARCHAR2 as a data type, not VARCHAR.


    Oracle supports VARCHAR.

    It has the same functionality as VARCHAR2.

    Oracle recommends using VARCHAR2 in case they decide to
    make VARCHAR follow SQL standard semantics.

    I would not hold my breath until that happens.

    In reality it is more of a coding style convention.

    Arne
     
    Arne Vajhøj, Oct 11, 2009
    #9
  10. Mongoose

    Lew Guest

    Mongoose wrote:
    >> I have an arraylist of objects. Each object has 3 properties. They
    >> are:
    >>
    >> DefectID - Integer
    >> Description - VARCHAR
    >> PriorityID - Integer


    Arne Vajhøj wrote:
    > Those types are in the database - in Java they are int, String and int.


    Actually, Java's 'Integer' is a closer match to SQL's 'INTEGER' than Java's
    'int' is. Java 'int' does not support 'NULL' (Java's 'null').

    --
    Lew
     
    Lew, Oct 11, 2009
    #10
  11. Lew wrote:
    > Mongoose wrote:
    >>> I have an arraylist of objects. Each object has 3 properties. They
    >>> are:
    >>>
    >>> DefectID - Integer
    >>> Description - VARCHAR
    >>> PriorityID - Integer

    >
    > Arne Vajhøj wrote:
    >> Those types are in the database - in Java they are int, String and int.

    >
    > Actually, Java's 'Integer' is a closer match to SQL's 'INTEGER' than
    > Java's 'int' is. Java 'int' does not support 'NULL' (Java's 'null').


    Good point.

    Very often Integer is better than int just for this reason.

    Arne
     
    Arne Vajhøj, Oct 12, 2009
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Zane

    JSP/JSTL question

    Zane, Nov 14, 2003, in forum: Java
    Replies:
    1
    Views:
    643
    Chris Smith
    Nov 24, 2003
  2. Robert Mark Bram

    Custom JSTL that accepts JSTL expression

    Robert Mark Bram, Dec 15, 2004, in forum: Java
    Replies:
    0
    Views:
    976
    Robert Mark Bram
    Dec 15, 2004
  3. DartmanX
    Replies:
    1
    Views:
    3,435
    Chris Smith
    Jan 23, 2005
  4. Cardinal
    Replies:
    0
    Views:
    689
    Cardinal
    Jan 12, 2006
  5. DaFoot
    Replies:
    1
    Views:
    1,702
    DaFoot
    Oct 23, 2008
Loading...

Share This Page