This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line!
See this thread for details.
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

JavaRanch » Java Forums » Java » Servlets
Bookmark "Servlet Fileter issues" Watch "Servlet Fileter issues" New topic

Servlet Fileter issues

Mathew Lee
Ranch Hand

Joined: Jun 08, 2009
Posts: 238

I am working on servlet filter example. My filter 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 '


The 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

Joined: Sep 14, 2006
Posts: 3

# 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

Joined: Aug 03, 2008
Posts: 9405

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

Joined: Mar 22, 2000
Posts: 13001
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.

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

Have you checked out Aspose?
subject: Servlet Fileter issues
It's not a secret anymore!