File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Simple Tag with Attribute Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "Simple Tag with Attribute" Watch "Simple Tag with Attribute" New topic
Author

Simple Tag with Attribute

Nabila Mohammad
Ranch Hand

Joined: Nov 05, 2007
Posts: 661
I am trying to runt the program mentioned in HFSJ .pg.521

Where am i supposed to set the attribute for setMovieList.
Is it by using
<jsp:setProperty >
if yes how. Do i have to set two properties - name and genre?


The servlet code is:





JSP Tag Invocation




The future belongs to those who believe in the beauty of their dreams.Dream BIG!
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

It looks like you need to set an attribute called "movieCollection" in a scope. There are different ways to do it. One would be to make a servlet, set the "movieCollection" attribute in the request scope, and forward to the JSP file. Can you figure out how to do it ?


[My Blog]
All roads lead to JavaRanch
Nabila Mohammad
Ranch Hand

Joined: Nov 05, 2007
Posts: 661
From your response , This is what i could come up with ...




But it still doesn't map anyway to the MyTag.jsp
How do I make this work :


And also , how do i link it to SimpleTagAttr.java file...?

If you could give me a little more help , I ll try to figure it out.

Thanks!
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9302
    
  17

Your movieCollection contains Strings, the elements in that list should be Objects of type Movie...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Nabila Mohammad
Ranch Hand

Joined: Nov 05, 2007
Posts: 661
Does that mean creating a Movie class with set and get methods.

And creating Movie objects and setting the name property to the name of the movie..

and then adding them to List...?
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

Great Nabila, you were very close

And creating Movie objects and setting the name property to the name of the movie.

Yes, a Movie class with a name and a genre.

Waiting for your next answer.
Nabila Mohammad
Ranch Hand

Joined: Nov 05, 2007
Posts: 661
This is the file i created (Hope its Correct!) :

Movie.java



MyServlet.java



I still have to create the Web.xml file.
But i cant figure out how the Tag handler class ie.SimlpleTagAttr. java is going to be linked to the Servlet...
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

I still have to create the Web.xml file.

"web.xml", all you need is too declare your servlet and its url mapping. You should have seen this already in your book.

But i cant figure out how the Tag handler class ie.SimlpleTagAttr. java is going to be linked to the Servlet...

Tag handlers are not linked to servlets. Your servlet just puts a List in the request scope, and forwards the request to your JSP file. Once the JSP is called, the container will see your tag, and will look for its class, using a TLD (Tag Library Descriptors). You have to make a TLD for your tag, and put it under WEB-INF. This should also be explained in your book.
Salil Vverma
Ranch Hand

Joined: Sep 06, 2009
Posts: 255

You shall have to make the tld file to get the tag working. The sample tld for your code is as mentioned below. You shall have to put this file in web-inf directory.



Regards
Salil Verma
Nabila Mohammad
Ranch Hand

Joined: Nov 05, 2007
Posts: 661
What's the url supposed to be...?

http://localhost:8080/SimpleTagAttr/MyTag.jsp

-OR-


http://localhost:8080/SimpleTagAttr/MiSer.do (And why)


My Web.xml file is


And the rest of the files are same.

I am getting this error with both the urls:


org.apache.jasper.JasperException: Exception in JSP: /MyTag.jsp:8

5:
6: THe movie list is :
7: <table>
8: <myTags:simple movieList="${movieCollection}">
9:
10:
11:
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

What's the url supposed to be...?

You want to call your servlet, which will redirect to the jsp. So you should use the servket mapping that you defined in web.xml : http://localhost:8080/SimpleTagAttr/MiSer.do

I am getting this error with both the urls:

Your tag is not closed.
Nabila Mohammad
Ranch Hand

Joined: Nov 05, 2007
Posts: 661
I did close my tag with </myTags:simple> before closing the table.



Either way , its not working.. I am getting the same error
Hasnain Javed Khan
Ranch Hand

Joined: Nov 23, 2007
Posts: 73
Hi Nabila.

Can you post your JSP file as well.

Kind regards.
Hasnain.


UPDATE: Sorry my bad. its already posted.

Kind Regards.
Hasnain.
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9302
    
  17

Can you show the stack trace, what you gave was the output in the browser, check the logs to see what actual exception is coming. Show us the relevant excerpts of the logs. Your getName and getGenre methods are also not public in the Movie class...
Nabila Mohammad
Ranch Hand

Joined: Nov 05, 2007
Posts: 661
My Jsp file is :





I am also getting this along with the previous error message i mentioned.

root cause

javax.servlet.ServletException: javax.servlet.jsp.JspException: Unable to find a value for "name" in object of class "foo.Movie" using operator "."
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9302
    
  17

Ankit Garg wrote:Your getName and getGenre methods are also not public in the Movie class...
Nabila Mohammad
Ranch Hand

Joined: Nov 05, 2007
Posts: 661
I am getting the out put:

He movie list is : Now Naming movie and Genre: Now Naming movie and Genre: Now Naming movie and Genre: Now Naming movie and Genre: Now Naming movie and Genre:
The DaVinci Code Drama
The Wedding Planner Romance
Matrix Action
Live Free or Die Hard Action
Shawshank Redemption Drama


I am not getting any value for ${movieCollection}.
Is it because it's an list of objects...

I guess the error was my get method not being public.

Thanks all for taking the time to give me a response on this!
Hasnain Javed Khan
Ranch Hand

Joined: Nov 23, 2007
Posts: 73
Nabila Mohammad wrote:My Jsp file is :





I am also getting this along with the previous error message i mentioned.

root cause

javax.servlet.ServletException: javax.servlet.jsp.JspException: Unable to find a value for "name" in object of class "foo.Movie" using operator "."



As Ankit said, the getters in the Movie class are not public.make the getters public. That should solve the problem most probably.

Let us know when you make the changes and try it.

kind Regards.
Hasnain.
Nabila Mohammad
Ranch Hand

Joined: Nov 05, 2007
Posts: 661
Christophe Verré wrote:It looks like you need to set an attribute called "movieCollection" in a scope. There are different ways to do it. One would be to make a servlet, set the "movieCollection" attribute in the request scope, and forward to the JSP file. Can you figure out how to do it ?
i


What are other ways to do it... Just curious!

And why am I not getting any value for ${movieCollection}.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Simple Tag with Attribute