Win a copy of The Business Blockchain this week in the Cloud forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Help Needed in Filter Code

 
Viji Elango
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;
}

}
 
Gowher Naik
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic