This week's book giveaway is in the Server-Side JavaScript and NodeJS forum.
We're giving away four copies of Modern JavaScript for the Impatient and have Cay Horstmann on-line!
See this thread for details.
Win a copy of Modern JavaScript for the Impatient this week in the Server-Side JavaScript and NodeJS forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Help Needed in Filter Code

 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Given below is my ScheduleFilter, where I'm setting an arraylist in the ServletContext. Everytime, it is coming only to the if condn (null check) and it is not at all coming to the else loop.

Please correct me where I'm wrong.

public class ScheduleFilter implements Filter {
private FilterConfig config;
private ArrayList schedulesList;
public void init(FilterConfig config) throws ServletException {
this.config = config;
}

public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
System.out.println("Coming to doFilter()");
if (config.getServletContext().getAttribute("SCHEDULES-LIST") == null){
FilterMasterData scheduleData = new FilterMasterData();
schedulesList = scheduleData.getSchdeules();
System.out.println("Len is " + schedulesList.size());
config.getServletContext().setAttribute("SCHEDULES-LIST", schedulesList);
}
else{
System.out.println("In Else loop..");
ArrayList scList = (ArrayList) config.getServletContext().getAttribute("SCHEDULES-LIST");
System.out.println("ArrayLength::" + scList.size());

}
chain.doFilter(request,response);

}

public void destroy() {
this.config = null;
}

}
 
Ranch Hand
Posts: 643
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

check code above.
I have made some changes in above code.
it is working fine.
Output is-:
first time execution-:
Coming to doFilter()
Len is 1

2nd time execution-:
Coming to doFilter()
In Else loop..
ArrayLength::1
 
I like you because you always keep good, crunchy cereal in your pantry. This tiny ad agrees:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic