• 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

ServletRequestListener problem

 
Ranch Hand
Posts: 160
Netbeans IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Netbeans IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Netbeans IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Netbeans IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Netbeans IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Another means to implement cross-cutting concerns is a Servlet filter.
 
ankur trapasiya
Ranch Hand
Posts: 160
Netbeans IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hmmmm.... Thanks for reply ...
reply
    Bookmark Topic Watch Topic
  • New Topic