• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Coffee Cram - Chapter 5 (Attributes and Listeners)

 
Nabila Mohammad
Ranch Hand
Posts: 664
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Q1.

When using a RequestDispatcher, the use of which methods can often lead to an IllegalStateEXception?


a.read
b.flush
c.write
d.getOutputStream
e.getResourceAsStream


The answer mentioned is b. (Which is vaguely correct)
I expected the answer to be c and d.
When we are flushing the stream , its works fine.
But when we try to write or get a stream AFTER committing or flushing , that's when the IllegalStateException Occurs.
So, in that case how is "flush" the right anwer.


Q2.
Which statements about ServletContext initialisatioin parameters are true


a.They should be used for data that rarely changes.
b.They should be used for data that frequently changes.
c.They can be accessed using ServletConfig.getParameter(String)
d.They can be accessed using ServletConfig.getInitParameter(String)
e.They should be used for data that is specific to a particular servlet
f.They should be used for data that is applicable to an entire web application.

Answer is a,d,f

I thought the answer was b,d,f
I thought Context parameters are used so that it can be acessed throught out the application and when the changes are FREQUENT.
So, that you dont have to follow up on each servlet or jsp to make the changes insteand make the change directly in you Deployment Descriptor.

Q3

I donot understnad the following statement.

"Depending on the class whose method creates a RequestDispatcher, the path to the resource to be forwarded to may change"

What do you mean by may or may not change?
Why would it change when it the path is clearly mentioned while requesting for the RequestDispatcher?


Q4
Given this code from an otherwise valid HttpServlet that has also been resistered as a ServletRequestAttributeListener:


What logging is generated?

a. A:a->b P:a->b
b. A:a->b M:a->c
c. A:a->b P:a->b M:a->c
d. A:a->b P:a->b P:a->null
e. A:a->b M:a->b A:a->c M:a->c
f. A:a->b M:a->b A:a->c P:a->null

The Answer is c.
I got the Answer correct based on the sequence of A P M ie. First value gets added , then replaced and then removed.
What I didnot get were the values.
how is P returning b and M returning c

A:a->b P:a->b M:a->c

Also, it would be great if some one could help me write the code to test it for myself.
I am not sure how to register ServletRequestAttributeListener.
And If I am not mistaken , I need DD and a Servlet Class or is there anything else... ??

(Phew!!)

Thanks

 
Vishwanath Krishnamurthi
Ranch Hand
Posts: 331
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For your Q3:

see this thread

For your Q4:
I got the Answer correct based on the sequence of A P M ie. First value gets added , then replaced and then removed.
What I didnot get were the values.
how is P returning b and M returning c


That's because the attribute listener methods return the old value of the attribute, not the new value.
 
Nabila Mohammad
Ranch Hand
Posts: 664
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

That's because the attribute listener methods return the old value of the attribute, not the new value.


I did not get why it returns the old values.

Thanks for the Thread. It was helpful.
 
vani venkat
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

[Post New]posted Today 8:32:41 AM private message [Email]
Quote [Up]
Q2.
Which statements about ServletContext initialisatioin parameters are true

a.They should be used for data that rarely changes.
b.They should be used for data that frequently changes.
c.They can be accessed using ServletConfig.getParameter(String)
d.They can be accessed using ServletConfig.getInitParameter(String)
e.They should be used for data that is specific to a particular servlet
f.They should be used for data that is applicable to an entire web application.

Answer is a,d,f

I thought the answer was b,d,f
I thought Context parameters are used so that it can be acessed throught out the application and when the changes are FREQUENT.
So, that you dont have to follow up on each servlet or jsp to make the changes insteand make the change directly in you Deployment Descriptor.



how can context parameters me accessed using ServletConfig.getInitParameter(String)? shouldn't it be ServletConfig.getServletContext().getInitParameter(String)
 
Vishwanath Krishnamurthi
Ranch Hand
Posts: 331
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vishwanath Murthi wrote:For your Q3:

That's because the attribute listener methods return the old value of the attribute, not the new value.


Oops! I should have said the event.getValue() used in the attributeXxxx() method returns the old value.....

Why should it return the old value? would help probably for logging purposes.
 
Vishwanath Krishnamurthi
Ranch Hand
Posts: 331
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nabila Mohammad wrote:Q1.

[b]Q2.
Which statements about ServletContext initialisatioin parameters are true

...
c.They can be accessed using ServletConfig.getParameter(String)
d.They can be accessed using ServletConfig.getInitParameter(String)




the book correctly says ServletContext.getInitParameter(String)
 
Nabila Mohammad
Ranch Hand
Posts: 664
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am sorry , that was my Typing Mistake

I wanted to know the reason for this:

"They should be used for data that rarely changes. "
I thought it was for data that frequently changes.

I am still not clear with why it returns old value , but i guess i ll do more research on it..

Can any one clear out the first question ...i mean its not a big deal just confusing on how the question is protrayed.

Thanks Vishwanath n Vani for your replies!
 
vani venkat
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Flush commits the response and writes out any data to output stream. IllegalStateException is thrown when forward is called after committing response.however any uncommited data is lost. though flushing the data itself doesnot throw illegal state exception, it is the trigger which might result in the exception.
 
vani venkat
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
for Q3:
request dispatcher can be called in two ways, either from request or from servletContext. we have to provide absolute path if we get request dispatcher from servletcontext, but can give relative path also if we get request dispatcher from request. hence,depending on how request dispatcher is obtained, path that we give may change.
 
vani venkat
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
for Q2,
They should be used for data that rarely changes

I related it in this way. as we know,web application uses context init parameters to make declared parameters available in the entire application. somethings like Database urls, or jndi context names which doesnot change that frequently. but in future, if we want to shift to another database for some reasons, this declarative approach of using context parameters makes it easy as it does not contain any code changes.
 
Nabila Mohammad
Ranch Hand
Posts: 664
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Vani , You have been a great help
Have a Great Day!!!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic