[x]
Posted via EE Mobile

Search, ask, and monitor your questions on the go with EE Mobile. Visit Experts Exchange from your mobile device and never be out of touch again.

Question
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

9.8

Servlet Filter example does not work based on locale

Asked by gudii9 in Java Servlets, J2EE, Java Programming Language

Hi,

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


import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class for Servlet: CountryStopper
 *
 */
 public class CountryStopper extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {
 
      protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            // TODO Auto-generated method stub
            String country= request.getLocale().getCountry();
            if(country.equals("ca")){
                  response.getWriter().println("hey let us go to tim horton coffee");
            }
            else
            {
                  response.getWriter().println("we love people who speak :"+ request.getLocale().getCountry());
            }
      }        
      
                        
}


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

package com.american.web.filter;

import javax.servlet.Filter;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.FilterChain;
import java.io.IOException;

public class AmericanFilter implements Filter {
      /* (non-Java-doc)
       * @see javax.servlet.Filter#init(FilterConfig arg0)
       */
      public void init(FilterConfig arg0) throws ServletException {
            // TODO Auto-generated method stub
      }

      /* (non-Java-doc)
       * @see javax.servlet.Filter#doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2)
       */
      public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
            String language=request.getLocale().getDisplayLanguage();
            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());
                  
            }
            System.out.println("time ater serveltt is full"+System.currentTimeMillis());
      }

      /* (non-Java-doc)
       * @see javax.servlet.Filter#destroy()
       */
      public void destroy() {
            // TODO Auto-generated method stub
      }

}


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


<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
      <display-name>
      FilterProject</display-name>
      <filter>
            <description>
            </description>
            <display-name>
            AmericanFilter</display-name>
            <filter-name>AmericanFilter</filter-name>
            <filter-class>com.american.web.filter.AmericanFilter</filter-class>
      </filter>
      <filter-mapping>
            <filter-name>AmericanFilter</filter-name>
            <url-pattern>/AmericanFilter</url-pattern>
      </filter-mapping>
      <filter-mapping>
            <filter-name>AmericanFilter</filter-name>
            <servlet-name>CountryStopper</servlet-name>
            <dispatcher>REQUEST</dispatcher>
      </filter-mapping>
      <servlet>
            <servlet-name>action</servlet-name>
            <servlet-class>
            org.apache.struts.action.ActionServlet</servlet-class>
            <init-param>
                  <param-name>config</param-name>
                  <param-value>/WEB-INF/struts-config.xml</param-value>
            </init-param>
            <init-param>
                  <param-name>debug</param-name>
                  <param-value>2</param-value>
            </init-param>
            <init-param>
                  <param-name>detail</param-name>
                  <param-value>2</param-value>
            </init-param>
            <init-param>
                  <param-name>validate</param-name>
                  <param-value>true</param-value>
            </init-param>
            <load-on-startup>2</load-on-startup>
      </servlet>
      <servlet>
            <description>
            blah blah</description>
            <display-name>
            CountryStopper</display-name>
            <servlet-name>CountryStopper</servlet-name>
            <servlet-class>
            CountryStopper</servlet-class>
      </servlet>
      <servlet-mapping>
            <servlet-name>action</servlet-name>
            <url-pattern>*.do</url-pattern>
      </servlet-mapping>
      <servlet-mapping>
            <servlet-name>CountryStopper</servlet-name>
            <url-pattern>/CountryStopper</url-pattern>
      </servlet-mapping>
      <welcome-file-list>
            <welcome-file>index.html</welcome-file>
            <welcome-file>index.htm</welcome-file>
            <welcome-file>index.jsp</welcome-file>
            <welcome-file>default.html</welcome-file>
            <welcome-file>default.htm</welcome-file>
            <welcome-file>default.jsp</welcome-file>
      </welcome-file-list>
</web-app>



Any ideas, suggestions, sample code, links highly appreciated.Thanks in advance
[+][-]08/26/09 03:46 PM, ID: 25193028Accepted Solution

View this solution now by starting your 30-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

About this solution

Zones: Java Servlets, J2EE, Java Programming Language
Sign Up Now!
Solution Provided By: objects
Participating Experts: 1
Solution Grade: A
 
[+][-]08/27/09 09:20 AM, ID: 25199960Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]08/27/09 05:09 PM, ID: 25203765Assisted Solution

Assisted solutions are selected by the member who asked the question as a comment that contributed to their question's solution.

Start your 30-day free trial to view this Assisted Solution or ask the Experts your question.

 
 
Loading Advertisement...
20091118-EE-VQP-93 - Hierarchy / EE_QW_3_20080625