Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to run jstl program

 
satya mahapatra
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

As written in HF I copied the jstl.jar inside the WEB-INF/lib folder as specified below.

C:\Program Files\Apache Software Foundation\Tomcat 6.0\webapps\ch9-forEach\WEB-INF\lib\jstl.jar

But still I am not getting the output. Control is going to jsp and when I am using scriptlets, output is fine. But <c:forEach/> tag isn't working.

Code for your reference
===============
<%@ page import="com.example.Model.*" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> SuperHit Moveis.jsp</TITLE>
</HEAD>
<BODY>
The super hit movies of ${param.actor} are:
<%
String[] movies = (String[])request.getAttribute("movieNames");
for(int i=0;i<movies.length;i++)
{ %>
<b><%= movies[i] %><b> <br>
<% }%>

<c:forEach var="movie" items="${movieNames}" >
${movie}
</c:forEach>
</BODY>
</HTML>

Thanks,
Satya
 
Louis Moloney
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you need to tell the page about the tag lib

stick somthing like this at top of page

<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
 
satya mahapatra
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Louis.

Now it is able to recognize <c:forEach> tag. When I gave something like this

<c:forEach var="i" begin="1" end="10" step="1">
${i}
</c:forEach>

its printing the nos.

but the below code isn't working.

<c:forEach var="movie" items="${movieNames}" >
${movie}
</c:forEach>

Log file error message
========================
Aug 1, 2007 5:31:29 AM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet Hi threw exception
org.apache.jasper.JasperException: /result.jsp(12,0) According to TLD or attribute directive in tag file, attribute items does not accept any expressions
=============
It showing attribute itens doesn't accept any expression. Why this?

If you have any clue pls help.

Thanks,
Satya
 
Michael Ku
Ranch Hand
Posts: 510
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What type of object is movieNames? It has to be some kind of a collection.
Would you please post the code that puts the variable name 'movieNames' into some kind of a scope as an atttribute.

One of the most common mistakes is to declare the variable 'movieNames' at the page level but not place it into one of the avaialbe attribute scopes.
 
satya mahapatra
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem is solved. The taglib uri that Louis has specified in the above message:
<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>

He has missed jsp(typing mistake I guess ), thats why the problem. The correct uri is

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

Anyway thanks guys for your support.
 
Michael Ku
Ranch Hand
Posts: 510
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Satya, I am confused. you previously said that this code worked
______________
<c:forEach var="i" begin="1" end="10" step="1">
${i}
</c:forEach>
_______________
but when you tried to use a collection it did not.

If the problem was an incorrect uri, then both of the examples that you gave should not have worked. If one worked but the other did not, then what changed ?
 
Michael Ku
Ranch Hand
Posts: 510
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Satya, I would like to compliment you for trying to code the examples you are finding in HF. It is the best way to learn. If you continue to try and learn this way, you will have no problem passing the exam
 
satya mahapatra
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Michael,

You have pointed it correct. "If the problem was an incorrect uri, then both of the examples that you gave should not have worked."

So once again I tried this. As happens before this time also the same thing.Means it worked for <c:forEach var="i" ....> but didn't work for collection. I don't know why this. Or may be I am doing some mistakes.

Anyway thanks once again for your support.

Thanks,
Satya
 
Michael Ku
Ranch Hand
Posts: 510
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Again, movieNames needs to be in one of the attribute scopes or else this will not work. Would you please provide the code that places movieNames into one of the attribute scopes?
 
khushhal yadav
Ranch Hand
Posts: 242
Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Satya

I tried this code and it's working fine.
The only thing which I have done is that I have also put standard.jar in /WEB-INF/lib as it provides library's implementation classes. jstl.jar provides API classes.

My Servlet Code:

package scwcd;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;


public class TestServlet1 extends HttpServlet
{
public void doPost(HttpServletRequest request, HttpServletResponse
response)throws IOException, ServletException
{
java.util.ArrayList movieNames = new java.util.ArrayList();
movieNames.add("Pan's Labyrinth");
movieNames.add("My Sassy Girl");
movieNames.add("Black Hawk Down");


request.setAttribute("movieNames", movieNames);
request.setAttribute("person", p);

RequestDispatcher view = request.getRequestDispatcher("result.jsp");
view.forward(request, response);
}

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException
{
doPost(request, response);
}
}


JSP code

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

<c:forEach var="i" begin="1" end="10" step="1">
${i}
</c:forEach>
<br>

<c:forEach var="movie" items="${movieNames}">
${movie}<br>
</c:forEach>

</body></html>


And Output is:


1 2 3 4 5 6 7 8 9 10
Pan's Labyrinth
My Sassy Girl
Black Hawk Down



Regards,
Khushhal
 
satya mahapatra
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Michael,

Find the below code for your reference.

package com.example.Web;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;

import com.example.Model.*;



public class SelectMovie extends HttpServlet
{
public void doPost(HttpServletRequest req,HttpServletResponse res)
throws IOException,ServletException
{
System.out.println("*****Coming inside servlet class*****");
res.setContentType("text/html");
PrintWriter out=res.getWriter();
String name = req.getParameter("actor");
System.out.println("Get the name from form:::::"+name);
getServletContext().log("*****************INSIDE SELECTNAME SERVLET CLASS***********");
movieModel movies = new movieModel();
System.out.println("Before calling getMovies method in servlet class");
List movieNames = movies.getMovies(name);
System.out.println("In servelt class movie names are:::::"+movieNames);
req.setAttribute("movieNames",movieNames);
System.out.println("After If blocks=========");
RequestDispatcher view = req.getRequestDispatcher("result.jsp");
view.forward(req,res);
}
}
 
Marc Peabody
pie sneak
Sheriff
Posts: 4727
Mac Ruby VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From external source found from a Google search:
Error Message: According to TLD or attribute directive in tag file, attribute items does not accept any expressions. When working on a J2EE (Java Enterprise Edition) web application with JSTL (JSP Standard Tag Library), this is a pretty common error.

Reason for this error is version incompatibility between JSP, Servlet and JSTL. Matching versions are listed below.

* JSP 1.2 , Servlet 2.3 , JSTL 1.0
* JSP 2.0 , Servlet 2.4 , JSTL 1.1

More info can be found through the link.
 
Shanika Wijerathna
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But hey i have a problem.. when we just copy and paste the jar file to our application, where we declare the uri of the standard tag library. i mean we dont declare it in our web.xml file. Then how container know the uri. i checked the content of jar file its just having a set of class files and META-INF directory and inside the META-INF it has MANIFEST.mf. i'm assuming that JSTL also just another taglibrary
ok thanks
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you also copy standard.jar ?
http://faq.javaranch.com/view?SetupJstlForJsp2
 
satya mahapatra
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Christophe,

Yah I have copied the standard.jar and jstl.jar in WEB-INF\lib folder.


Thanks,
Satya
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, I was asking Shanika, who doesn't understand why the uri is set. If he/she could try to look into standard.jar's META-INF...
[ August 07, 2007: Message edited by: Christophe Verre ]
 
Shanika Wijerathna
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Christophe
Yeah I didn't copy the standard.jar before and didn't check the content of it. And now I have found the relevant TLDs inside the MATA-INF of standerd.jar. thanks
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic