Some Basics in JSP Documents

[nobr]With my first steps in JSP Documents I am a little irritated. It starts with the basic rules for printing out a variable. Imagine I have a variable defined called 'result'. Now I try to print it out, therefore I use:

<jsp:expression>result</jsp:expression>

This works quitze well. Now theres a second possibility like that (from a tutorial):

<c:forEach var="i" begin="1" end="10">

<c:out value="${i}"/>

</c:forEach>

But that really prints ten times the String "${i}".

This result is very similiar to that approach (from the j2ee tutorial):

<c:forEach var="counter" begin="1" end="3">

<jsp:text>${counter}</jsp:text>

</c:forEach>

Besides the thing, that only "${i}" is printed I thought that the jsp:text clause is only for separating template text from jsp code/tags.

I would be very thankful for anyone who explains me the difference and application of these tags and my mistakes in using them. For completeness, heres the example jsp I wrote for that purpose:

<?xml version="1.0" encoding="UTF-8"?>

<jsp:root version="2.0"

xmlns:jsp="http://java.sun.com/JSP/Page"

xmlns:c="http://java.sun.com/jsp/jstl/core">

<jsp:directive.page contentType="text/html;charset=UTF-8"/>

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>

<title>NextPage</title>

</head>

<body>

hallo<br/>

<jsp:declaration>long result;</jsp:declaration>

<jsp:scriptlet>

result = Math.round(10.99d);

</jsp:scriptlet>

<c:out value="result"/>

<br/>

<jsp:expression>result</jsp:expression>

<br/>

<c:forEach var="i" begin="1" end="10">

<c:out value="${i}" />

</c:forEach>

<br/>

<c:forEach var="counter" begin="1" end="3">

<jsp:text>${counter}</jsp:text>

</c:forEach>

</body>

</html>

</jsp:root>

[/nobr]

[2926 byte] By [Maurice_Ta] at [2007-11-27 5:46:02]
# 1

I don't think you've declared what taglib 'c' refers to. For example, in an application I recently wrote, the second line was:

<%@ taglib uri="/struts-tags" prefix="s" %>

So that any time a tag begins with "<s:", tomcat knows to look in struts-tags.tld (currently stored in one of the jars in my lib directory).

I don't think jsp has any logic tags (like for loops or if statements) natively; you need to get a taglib for that. I know the generic struts taglib (struts-tags) does have some logic tags in it; but it is specific to struts and so you'd probably be better off finding a more generic one. Does the tutorial talk about that at all?

Hope this helps.>

leptogenesisa at 2007-7-12 15:28:19 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

> I don't think you've declared what taglib 'c' refers

> to.

I should, with this line:

xmlns:c="http://java.sun.com/jsp/jstl/core"

its the equivalen of your

> %@ taglib uri="/struts-tags" prefix="s" %>

but in Document (xml) style as it is possible since JSP 2. After the xmlns you can see the prefix 'c' defined as the following name which is mapped in web.xml to the taglib. The java core taglib (java.sun.com/jsp/jstl/core) includes then the controle statements like choices and iterations.

Maurice_Ta at 2007-7-12 15:28:19 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3
Looks like you are using the xml syntax for jsp pages. That is a bit more tricky and less forgiving than the 'standard' tags.Regarding JSTL/EL check out [url http://forum.java.sun.com/thread.jspa?threadID=629437&tstart=0] this thread[/url] (reply #6) It may solve some
evnafetsa at 2007-7-12 15:28:19 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...