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 Doubt in JSP Custom tags (Simple Tag) in Dynamic Drop Down Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "Doubt in JSP Custom tags (Simple Tag) in Dynamic Drop Down" Watch "Doubt in JSP Custom tags (Simple Tag) in Dynamic Drop Down" New topic
Author

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

Salman Raut
Greenhorn

Joined: Mar 31, 2009
Posts: 15
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


Best Regards
Milton Ochoa
Ranch Hand

Joined: Oct 23, 2007
Posts: 336

Exactly what is the problem?
Salman Raut
Greenhorn

Joined: Mar 31, 2009
Posts: 15
Hi Milton,

The problem is in the line 6 of the JSP.

Regards
Milton Ochoa
Ranch Hand

Joined: Oct 23, 2007
Posts: 336

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

Joined: Mar 31, 2009
Posts: 15
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

Joined: Oct 23, 2007
Posts: 336

O sorry, i didnt see.

Can you tell what is the error?

when you access to the JSP?
Salman Raut
Greenhorn

Joined: Mar 31, 2009
Posts: 15
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
Abdul Rahman
Ranch Hand

Joined: Jul 04, 2008
Posts: 63
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.


Abdul
SCJP5, SCWCD5, [scwcd wall of fame]
Salman Raut
Greenhorn

Joined: Mar 31, 2009
Posts: 15
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
Ranjit Gopinathan
Ranch Hand

Joined: Jul 03, 2007
Posts: 46

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

Joined: Mar 31, 2009
Posts: 15
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

Joined: Jul 04, 2008
Posts: 63
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));
Amit Ghorpade
Bartender

Joined: Jun 06, 2007
Posts: 2712
    
    5

"Saachin Tendulkar " please check your private messages for an important administrative matter. You can check them by clicking the My Private Messages link above.


SCJP, SCWCD.
|Asking Good Questions|
Salman Raut
Greenhorn

Joined: Mar 31, 2009
Posts: 15
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
Greg Messner
Greenhorn

Joined: Mar 11, 2009
Posts: 3
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.
jihui lee
Greenhorn

Joined: Oct 03, 2009
Posts: 1

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Doubt in JSP Custom tags (Simple Tag) in Dynamic Drop Down
 
Similar Threads
Using EL to generate attribute/value pairs inside of an XHTML element
Doubt on page 547 HFSJ
unable to access the attribute in any of the container scope
setDynamicAttribute not being invoked
Tutorial on Dynamic Attributes