• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Doubt in JSP Custom tags (Simple Tag) in Dynamic Drop Down

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

There is an example in Head First Servlets and JSP book.
This is in chapter 10 (Pg 543). The mission is to implement the select tag handler for generatinf dynamic drop down.
This will help us in getting away from hard coding the drop down box in the HTML page.

Following are the files:
1) SelectTagHandler.java


2) TLD File



3) JSP Page:



Can some one figure out what am I dong wrong?
It will helpful if some one can provide the flow of this custom tag handler?

Thank you all for your time and consideration.

Best Regards
 
Ranch Hand
Posts: 336
Firefox Browser Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Exactly what is the problem?
 
Salman Raut
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Milton,

The problem is in the line 6 of the JSP.

Regards
 
Milton Ochoa
Ranch Hand
Posts: 336
Firefox Browser Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
maybe you problem is this:

in the JSP:



the attribute size is not specify on the tag (TLD), and you want that this attribute be automatic (Dynamic Attribute).

You must specify on TLD inside of tag: <tag>,

<dynamic-attributes>true</dynamic-attributes>



and in the class, implements DynamicAttributes, and his method setDynamicAttribute.

Sorry my bad english!
 
Salman Raut
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Milton,

I have specified the attribute size in the TLD file on line 28.

Dynamic attributes are mainly used for optional attributes.

In this example, I am only testing with required attributes.

The problem is surely in the jsp though

Thanks for taking the time and effort
 
Milton Ochoa
Ranch Hand
Posts: 336
Firefox Browser Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
O sorry, i didnt see.

Can you tell what is the error?

when you access to the JSP?
 
Salman Raut
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I am not sure if it is right to copy paste the error.

But here it is:

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

org.apache.jasper.JasperException: java.lang.NullPointerException
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:522)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:416)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:342)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:267)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)

root cause

java.lang.NullPointerException
com.badal.handlers.SelectTagHandler.addOptionsList(SelectTagHandler.java:33)
com.badal.handlers.SelectTagHandler.doTag(SelectTagHandler.java:53)
org.apache.jsp.index_jsp._jspx_meth_myTags_005fselectTag_005f0(index_jsp.java:97)
org.apache.jsp.index_jsp._jspService(index_jsp.java:63)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:374)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:342)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:267)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)


Thanks
 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sachin,
First of all it feels nice to suggest Sachin Tendulkar on topic other than cricket

private List optionsList;


change this to private List optionsList = new ArrayList();
or in the method addOptionsList() add this line of code: optionsList = new ArrayList();
this will solve your

org.apache.jasper.JasperException: java.lang.NullPointerException


however after this you are likely to get exceptions in your size attribute .
check and verify.
 
Salman Raut
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I made the changes in the Tag Handler file.
But still I am getting a null pointer exception in the for loop. I have bolded that part in the code.
This is the modified tag handler file.





Are you skeptical about the size attribute because it is String (and not int) OR there is some other reason.

Thanks for taking the time.

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

Hi Sachin,

Do you have a collection "optionsList" in applicationScope?.

# public void setOptionsList(List optionsList)
# {
# this.optionsList=optionsList;
# }

will set a null to optionsList variable and when you are trying to iterate a null object...You know what you get ...

HTH
 
Salman Raut
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

Thanks for your help.

I referred to Bryan Basham's tutorial on Dynamic Attributes and figured it out.

The basic problem was I did not use servlet to set the List. Now, I passed the request to the servlet. Populated the list in the servlet. Put the list in request scope. Then retrieve that list in jsp using EL (${requestScope.colorList}).

For future reference, here are all the required files:

1) input.html



From here the request goes to MyServlet1.java which populates the list.

2) MyServlet1.java



the servlet forward the response to form.jsp.


3) form.jsp



4) SelectTagHandler.java


5) TLD File




6) web.xml





This would solve the mission of writing a custom tag for dynamic select. javascript:emoticon('');

Thanks for all your time and support.
 
Abdul Rahman
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sachin,

i was wondering what you were trying to do with this piece of code:

# out.print(String.format(ATTR_TEMPLATE,"name",this.name));
# out.print(String.format(ATTR_TEMPLATE,"size",this.size));

 
Bartender
Posts: 2856
10
Firefox Browser Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"Saachin Tendulkar " please check your private messages for an important administrative matter. You can check them by clicking the My Private Messages link above.
 
Salman Raut
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Abdul,

Sorry for answering so late.

# out.print(String.format(ATTR_TEMPLATE,"name",this.name));
# out.print(String.format(ATTR_TEMPLATE,"size",this.size));





String.format() is basically used to replace complex String concatenation.
Instead of writing:





This complex String concatenation uses nesting of double quotes (") and single quotes (');
This can be eliminated by declaring constants.


and then using these constants.
The following code achieves the same result as that of complex nesting of double and single quotes.



The above code is a lot more pretty and readable.

This is particularly useful when we have multiple occurrences of the same attribute that requires nesting of double and single quotes.

Hope I answered your question.

Best Regards
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Back to the original question of this thread. The book (Head First Servlets & JSP) had an example of a Custom JSP tag and in an attempt to get people to think left off were applicationScope.colorList was being defined and set. The proper place for colorList is in the Model, which in this case should have been BeerExpert.java and then added as an attribute in the init() method of the controller servlet (BeerSelect.java). Here is how I implemented it:

Added to BeerExpert.java:


Added to BeerSelect.java:


In addition we have to add something to the servlet configuration in web.xml to load the BeerSelect servlet on startup so that the Model is created:


Once you have done this and re-deployed the app you can now access the colorList in EL as follows:



Hope this clears things up for anyone working through this example from the book.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hello. Thanks for this reference. It helped a lot for me. Because I had also same problem when I was studying HFSJ chapter 10. that example.

But I have a question... seems java makes me more questions when I learned new things about java.

I followed your tutorial but if I want to use doPost() method, do I have to use html always first?
(I am sorry if it's stupid question. I haven't studied long time for jsp& servlet.)
Because when I did't use html first and I tried to use only jsp( when I use servlet with doPost() and put this code(down) inside of jsp

) ,this always gives me 405 error.



thanks.
 
reply
    Bookmark Topic Watch Topic
  • New Topic