• 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
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

please help me with this app - URGENT !

 
Ranch Hand
Posts: 1277
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all...

I am trying to write a web app that satisfies the following purpose -

index.html - has a hyperlink thats mapped to the MovieServlet.java

MovieServlet.java - the servlet that makes three 'Movie' objects, add all
the three in a Arraylist, and sets this
list as a request scoped attribute, and forwards the
request to customTag4.jsp

Movie.java - a simple POJO that has 2 properties namely name and year

SimpleTagHandler.java, customTag4.tld - both these define and use a custom
tag, that retrieves the request
scoped
variable set by the servlet,
and sets it as a attribute,
to be retrieved by the jsp

customTag4.jsp - uses the attribute ( of type List ) set by the simple tag
handler and uses html <td> and <tr> to display
the movie name and year.

the problem - in SimpleTagHandler.java's doTag( ) method i am getting a
null pointer exception for the statement

movieName = (ArrayList)getJspContext().getAttribute("moviesFromServlet");

here moveieName getting assigned to null, i.e. the attribute set by the servelt isnt retrieved here

guys please help out.any help will be appreciated.

thanks in advance. the code for this is given below -

____________________________________________________________________________

MovieServlet.java



____________________________________________________________________________

SimpleTagHandler.java



____________________________________________________________________________

cutomTag4.jsp



____________________________________________________________________________

Movie.java



____________________________________________________________________________

the D.D


____________________________________________________________________________

index.html


____________________________________________________________________________
 
Ranch Hand
Posts: 572
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are setting the arraylist at request scope



whereas you are trying to retrieve it through page scope


this is the problem. Retrieve it through the request scope
 
arch rival
Posts: 2813
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why was this so urgent?
 
Ali Gohar
Ranch Hand
Posts: 572
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I guess he was about to write the paper
 
Ranch Hand
Posts: 310
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am giving you the correct code. Check it out

MovieServlet.java


Movie.java


SimpleTagHandler.java


web.xml


customTag4.jsp



AnyTLDFile.tld


enjoy
 
Niranjan Deshpande
Ranch Hand
Posts: 1277
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks fo you kind help srreraj,

but, i wanted to do it this way -

the servlet sets the list of movies
the doTag( ) retrieves it and sets it in a attribute named "movies"
the jsp uses this attribute to display the list of movie

so i want something like this


in doTag( )

List movies = (ArrayList)getJspContext()
.getAttribute("moviesFromServlet");

int the for loop retireve each elemnt in the list and set it as

getJspBody().setAttribute("movies");

in jsp

<code4:MyTag movieName="${movies}">

tell me if this can be done ?
thanks !
 
Sreeraj G Harilal
Ranch Hand
Posts: 310
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
getJspBody().setAttribute("movies");
You are setting this attribute inside Tag files So this attribute will only available inside the body.
<code4:MyTag movieName="${movies}"> this is wrong Because whenever you are invoke this tag the movies attribute will not available. Inside the tag file you are setting movie attribute.That means the movies attribute is available only after you call the getJspBody().invoke(null).
 
Niranjan Deshpande
Ranch Hand
Posts: 1277
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sreeraj...in hfsj, we do have sample codes in chapter 10, where in the jsp's tag invocation depends on the doTsg( ) to set a attribute...

and that code works..

my problem is -

the servlet sets movie list and forwards it to forward.jsp which uses a tag...so SimpleTagHandler's doTag( ) runs....in which i am not able to retrieve the attribute...i am getting null...! got it ?


 
Sreeraj G Harilal
Ranch Hand
Posts: 310
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Man, try to understand what i am saying.
use this


<code4:myTag movieName="${moviesFromServlet}">
<tr>
<td>${movies.name}</td>
<td>${movies.year}</td>
</tr>
</code4:myTag>



instead of your code like


<code4:myTag movieName="${movies}">
<tr>
<td>${movies.name}</td>
<td>${movies.year}</td>
</tr>
</code4:myTag>



because movies is accessable only after the doStartTag()(that means, because it is simple tag, after <code4:myTag movieName="${moviesFromServlet}"> ) you can access movie attribute.

I hope you can understand now.
 
Sreeraj G Harilal
Ranch Hand
Posts: 310
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
or you can use like this also.



remove the movieName attribute from the tag handler class.Because you are trying to access the request attribute from the tag handler class itself.

use this code in doTag method as shown in below
movieName = (ArrayList)getJspContext().getAttribute("moviesFromServlet",PageContext.REQUEST_SCOPE);
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic