Raizekas

Untitled

Mar 12th, 2021
27
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package lt.kompanija.filtrai;
  2.  
  3. import javax.servlet.*;
  4. import javax.servlet.annotation.WebFilter;
  5. import java.io.IOException;
  6.  
  7. @WebFilter("/*")
  8. public class CharsetFilter implements Filter
  9. {
  10. private String encoding;
  11.  
  12. public void init(FilterConfig config) throws ServletException
  13. {
  14. encoding = config.getInitParameter("requestEncoding");
  15. if (encoding == null)
  16. {
  17. encoding = "UTF-8";
  18. }
  19. }
  20.  
  21. public void doFilter(ServletRequest request, ServletResponse response, FilterChain next)
  22. throws IOException, ServletException
  23. {
  24. // Respect the client-specified character encoding
  25. // (see HTTP specification section 3.4.1)
  26. if (null == request.getCharacterEncoding())
  27. {
  28. request.setCharacterEncoding(encoding);
  29. }
  30.  
  31. // Set the default response content type and encoding
  32. response.setContentType("text/html; charset=UTF-8");
  33. response.setCharacterEncoding("UTF-8");
  34.  
  35. next.doFilter(request, response);
  36. }
  37.  
  38. public void destroy()
  39. {
  40. }
  41. }
  42.  
RAW Paste Data