File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes Servlet Fileter issues Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Servlet Fileter issues" Watch "Servlet Fileter issues" New topic
Author

Servlet Fileter issues

Mathew Lee
Ranch Hand

Joined: Jun 08, 2009
Posts: 238
Hi,

I am working on servlet filter example. My filter CountryStopper.java servlet code looks like





which supposed to say
'only american spanish people can view this filetert '



otherwise if locale language is something else like chinese etc it should say
'we love people who speak : chinese '

etc


The AmericaFilter.java filter looks like






I am changing within the IE, google chrome etc browser tools options to chinese etc language
Even after refreshing or rerunning servlet
but it always displays like
'only american spanish people can view this filetert '

never i could see output like

we love people who speak : chinese etc

irrespective of change in browser locale setting in IE as well as google chrome



my web.xml looks like




My goal is to see

we love people who speak : chinese etc

as well. i am bit confused about the flow.When we run the servlet it supposed to hit the filter and based on the browser local it supposed to display values from filter right. Please advise

Any ideas, suggestions, sample code, links highly appreciated.Thanks in advance
veda bandaru
Greenhorn

Joined: Sep 14, 2006
Posts: 3
Hi,


# if (language!= "spansih"){
# response.getWriter().println("only american spanish people can view this filetert");
# }
# else{
# response.getWriter().println("we love people who speak :"+ request.getLocale().getCountry());
#
# }


Is the If condition correct?
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9304
    
  17

Your servlet code is never executed. In your filter, you never call chain.doFilter(request, response);. So the request never reaches your servlet. Moreover I think you'll get an IllegalStateException since you are writing some response in your filter and then again in your servlet...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12803
    
    5
Furthermore, the statement:


will never do what you expect. String comparisons MUST use the equals method. When used with object references, == and related operators compare references, not contents.


This is a common beginner mistake.

Bill
Mathew Lee
Ranch Hand

Joined: Jun 08, 2009
Posts: 238
wonderful it worked fine now...after i changed the filter code to



through IE 6.0 where i kept default locale it showed output like

only american spanish people can view this filetert

where as in google chrome when i change the locale to some different language it showed output like



we love people who speak :US


as in attachments.

I am confused about the flow, output , filter function on how it is integrated with this servlet example in this particular scenario based on locale. Please advise



 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Servlet Fileter issues