0

I am working on a back-end project where request is coming as JSON object to my servlet and response is sent as an JSON object as well. I found this LINK useful but need help to understand how filter can be used in my aforementioned back-end scenario to globalized my all classes/APIs through filter

I am new to Java and looking for advice that is it possible? OR I have to manually call ResourceBundle for each class

Thanks in anticipation

Community
  • 1
  • 1
Devdas
  • 69
  • 1
  • 2
  • 10
  • what do you mean by "globalize my all classes/API through filter". What are you really trying to accomplish? Please elaborate it. – Yohannes Gebremariam Aug 29 '17 at 18:21
  • in MVC model we can write a servlet filter (link is provided in my question) that sets country and language globally for all requests/responses for wohle jsp files but here I am getting country & language as JSON parameter and there is no view in my hand that is controlled my front-end designer – Devdas Aug 29 '17 at 18:45

1 Answers1

0

If you are receiving your i18n in json payload, you may try doing something as following

 // this parses the json
    JSONObject jObj = new JSONObject(request.getParameter("yourPramName")); 
    Iterator it = jObj.keys(); 

    while(it.hasNext())
    {
        String key = it.next(); // get key
        Object o = jObj.get(key); // get value
     req.getSession().setAttribute(key, o);
    }

Hope this helps.

Yohannes Gebremariam
  • 2,225
  • 3
  • 17
  • 23
  • This code has to be written in each class whereas I am looking for a code example that should be written once in whole project and use it in all classes rather than code it in every class. I have given example in my question that in MVC model we can use servlet filter that do this but right now I don't have control over "VIEW" as this is controlled by front-end designer and I am working on back-end – Devdas Aug 30 '17 at 07:39
  • You don't have to write it in each class. You can write this once in your filter. What you really need is a request object, The doFilter method of a servlet filter has both request and response objects as parameter. Once you have access to the request object , you can get the value and persist the data in a session. – Yohannes Gebremariam Aug 30 '17 at 13:24