Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Doubt in HFSJ..pg 511 creating simple tag with attribute

 
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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)
 
Sue Pillai
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i think I got my mistake corrected:

I moved a part of the code from doTag to setMovieList and added the line (shown in bold):

movieList=new ArrayList();
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);

I got the output now. But, is it correct to do it this way?
 
I'm not dead! I feel happy! I'd like to go for a walk! I'll even read a tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic