Answer the question
In order to leave comments, you need to log in
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"/>
@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);
}
}
<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>
<c:out value="${cookie['lang'].value}"/>
Answer the question
In order to leave comments, you need to log in
Well, you are completely right. How cookies work.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question