The example given is not complete, right?
I understood that example as foll:
movieList is a collection of movie objects, which is a
java bean with name and genre properties. movieList and movie must be set as attributes, right? We need to write code for Movie and add some movies to the movieList and set it as an attribute right?
This is the app I wrote based on the snippet provided. I am getting an exception and am unable to figue why. I dont have much experience with coding, am I doing some blunder somewhere?
Tag Handler class package com.example;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
import java.io.IOException;
import java.util.*;
public class SimpleTagHandler5 extends SimpleTagSupport
{
private List movieList;
public void setMovieList(List movieList)
{
this.movieList=movieList;
}
public void doTag() throws IOException, JspException
{
Movie movie1=new Movie();
movie1.setName("Fahrenheit911");
movie1.setGenre("Documentary");
movieList.add(movie1);
Movie movie2=new Movie();
movie2.setName("Cinderella Man");
movie2.setGenre("Action");
movieList.add(movie2);
Movie movie3=new Movie();
movie3.setName("FindingNemo");
movie3.setGenre("Animated");
movieList.add(movie3);
getJspContext().setAttribute("movieCollection", movieList);
Iterator i=movieList.iterator();
while (i.hasNext())
{
Movie movie=(Movie)i.next();
getJspContext().setAttribute("movie", movie);
getJspBody().invoke(null);
}
}
}
The Movie class is as foll: package com.example;
public class Movie
{
public
String name;
public String genre;
public void setName(String name)
{
this.name=name;
}
public void setGenre(String genre)
{
this.genre=genre;
}
public String getName()
{
return name;
}
public String getGenre()
{
return genre;
}
}
The JSP: <%@ taglib prefix="myTags" uri="SimpleTag5Uri" %>
<html><body>
<table>
<myTags:Simple5 movieList="${movieCollection}">
<tr>
<td>${movie.name}</td>
<td>${movie.genre}</td>
<tr>
</myTags:Simple5>
</table>
</body></html>
The TLD: <?xml version="1.0" encoding="ISO-8859-1" ?>
<taglib xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"
version="2.0">
<tlib-version>0.9</tlib-version>
<short-name>MySimpleTag</short-name>
<uri>SimpleTag5Uri</uri>
<tag>
<description>This is a Simple Tag with an attribute </description>
<name>Simple5</name>
<tag-class>com.example.SimpleTagHandler5</tag-class>
<body-content>scriptless</body-content>
<attribute>
<name>movieList</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
<type>java.util.List</type>
</attribute>
</tag>
</taglib>
The DD: <web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4">
<
servlet>
<servlet-name>SimpleTag5Test</servlet-name>
<jsp-file>/SimpleTag5.jsp</jsp-file>
</servlet>
<servlet-mapping>
<servlet-name>SimpleTag5Test</servlet-name>
<url-pattern>/simpletag5.do</url-pattern>
</servlet-mapping>
</web-app>
Exception type Exception report
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
org.apache.jasper.JasperException
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:373)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:295)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:245)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
root cause
java.lang.NullPointerException
com.example.SimpleTagHandler5.doTag(SimpleTagHandler5.java:19)
org.apache.jsp.SimpleTag5_jsp._jspx_meth_myTags_Simple5_0(org.apache.jsp.SimpleTag5_jsp:76)
org.apache.jsp.SimpleTag5_jsp._jspService(org.apache.jsp.SimpleTag5_jsp:50)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:99)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:325)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:295)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:245)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)