Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ServletRequestListener problem

 
ankur trapasiya
Ranch Hand
Posts: 160
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello ..

I want one attribute to be added in request. Therefore I used ServletRequestListener listener for this. Now i have nearly 10 servlets in my app but the need of attribute addition in request is required in one servlet only. So is there any way to make this listener to make specific for only one servlet ?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65339
97
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is no such class as ServletResponseListener. The ServletRequestListener class listens for requests to come into and out of scope, not for attributes to be added to requests.

Why would need such a listener? You add attributes (scoped variables) with the setAttribute() method, not a listener.
 
ankur trapasiya
Ranch Hand
Posts: 160
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry ... by mistake i wrote ServletResponseListener.
..
My question is the need of attributed to be added into request is arise in one servlet only then can't we make this ServletRequestListener specific to one servlet only ??? because it's method is getting called for request of each of the servlet.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65339
97
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And I ask again, why do you think that a ServletRequestListener has anything to do with adding attributes to a request? It doesn't.
 
ankur trapasiya
Ranch Hand
Posts: 160
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


here this code will run for all the servlet request and therefore here attribute will be added to the request object in all the servlets. What i want is i need this attribute to be added in only 1 servlet. can't i restrict this ServletRequestListener to work for any one specific servlet?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65339
97
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you need to do this only for a single servlet it makes no sense to do this in a listener. Just do it in the single servlet in which you need it done. Why over-complicate things needlessly?
 
ankur trapasiya
Ranch Hand
Posts: 160
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but if I implement ServletRequestListener in the servlet in which i require it then the overridden method of ServletRequestListener are not getting called whether i register the servlet as a listener in web.xml or not.
 
Paul Clapham
Sheriff
Posts: 21581
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ankur trapasiya wrote:but if I implement ServletRequestListener...

You aren't paying attention. If you want to add an attribute to the request in Servlet X, then simply write one line of code in the doGet or doPost method which adds that attribute. ServletRequestListener isn't helpful here so don't use it.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65339
97
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe an analogy will help.

Person 1: How do I use this nutcracker to saw this board in half?
Person 2: You don't use a nutcracker, it's the wrong tool for the job.
Person 1: But the nutcracker doesn't work when I try to saw the board.
Person 2: You don't use a nutcracker, it's the wrong tool for the job.
Person 1: But when I try to saw the board with a nutcracker, it doesn't work.

Are you actually trying to set a scoped variable (attribute) onto the request, or are you just trying to learn how request listeners work (using a flawed example)?
 
ankur trapasiya
Ranch Hand
Posts: 160
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got what i was doing wrong..... Thanks to you both for your replies.... Now i got understood at where i was making mistake .... I can use request.setAttribute() in the required servlet....

So what i learned from this is that whenever we require the request processing in the entire web application's all the servlets at that time i have to use ServletRequestListener...

Am i getting right ??
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65339
97
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another means to implement cross-cutting concerns is a Servlet filter.
 
ankur trapasiya
Ranch Hand
Posts: 160
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmmmm.... Thanks for reply ...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic