B
B
Biaci_Anj2022-04-10 00:30:06
Java
Biaci_Anj, 2022-04-10 00:30:06

Servlets, the language changes only from the second click on the link, why?

Help, please, with internalization in servlets through cookies.

This is how I added the bundle to the page

<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<fmt:setLocale value="${cookie['lang'].value}"/>
<fmt:setBundle basename="messages"/>

Here is a filter that adds the lang cookie to the response

@WebFilter("/*")
public class CookieLocaleFilter implements Filter  {

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest req = (HttpServletRequest) request;
        HttpServletResponse res = (HttpServletResponse) response;

        if (req.getParameter("locale") != null) {
            Cookie cookie = new Cookie("lang", req.getParameter("locale"));
            res.addCookie(cookie);
        }

        chain.doFilter(request, response);
    }
}


This is how I change the language
<li><a href="?locale=en"><fmt:message key="label.lang.en" /></a></li>
<li><a href="?locale=ua"><fmt:message key="label.lang.ua" /></a></li>

The problem is that the language changes only from the second click on the link.
The first time - a cookie is added to the browser, but nothing happens on the page and

does not change the value at all. On the second click on the link - the language is already changing.
What am I doing wrong?

PS I'll
explain in more detail:
Let's say I follow the link ?locale=ua. Displays en , the first press - nothing (only cookies appeared in the browser), the second press - the output changed to ua It feels like cookies are read only from the second transition to the page. Or the page loads somehow before the cookie updates.
<c:out value="${cookie['lang'].value}"/>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Michael, 2022-04-10
@Biaci_Anj

Well, you are completely right. How cookies work.

  1. The server sends a Set-Cookie header to the browser
  2. The server sends the page along with the header.
  3. Having received the Set-Cookie header, the browser saves the "cookie" to itself.
  4. The next time a page is requested from this site, the browser attaches a Cookie header to the request, in which it transmits the saved cookies.
  5. In response, the server can again send Set-Cookie, then the existing "cookie" will be added or updated.

Thus, in your case, it is enough not to follow the link again, but to press F5 - the language will switch.
In general, to solve your problem, I would take the language from the URL and only if it is not there - from the "cookie". Then switching to the page with the desired language would switch it immediately (since the value in the URL takes precedence) and save it in a cookie for subsequent requests.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question