Two filters at the door, a session behind it

An online pharmacy on bare servlets — the final project of a 2021 course. Before any servlet runs, every request passes EncodingFilter (UTF-8) and AuthorizationFilter (who are you, and may you come in?). Who you are is kept in the session; what you see depends on your role and your language.

This is what Spring Security, CharacterEncodingFilter and LocaleResolver do for you. Every request, response and SQL statement here is real: recorded from this repository's code.

Pharmacy - online drugstore
Tomcat→ EncodingFilter→ AuthorizationFilter→ Servlet→ SQL→ JSP
NO ENTRY

      

The journey, stage by stage

Pick a request, then step through it. Each stage is a class of this repository (or of Tomcat); the highlighted bytes and SQL are what that stage reads or writes. A red stage ends the journey early; crossed-out stages are not reached.

    Request sent by the browser

    Response written back

    SQL that PostgreSQL received while this request was handled (log_statement=all)

    The filter chain

    Both filters are mapped to /* with @WebFilter, so Tomcat puts them in front of every servlet, JSP and static file. For one request it builds an ApplicationFilterChain: the filters, then the servlet. Each filter decides whether to call chain.doFilter(request, response) — pass the request on — or to answer itself and stop.

    Tomcat

    Parses the bytes, finds the servlet for the URL, builds the chain.

    EncodingFilter

    setCharacterEncoding("UTF-8") on the request and the response, then always doFilter.

    AuthorizationFilter

    Open page? A user in the session? An /admin page and role ADMIN? Only then doFilter.

    Servlet

    doGet / doPost: services, DAO, SQL.

    JSP

    After forward: the HTML, in the language of the session.

    no user → sendRedirect("/login")USER on /admin… → sendRedirect("/user-medicines")

    filter/AuthorizationFilter.java — doFilter

    
            

    added in 2026: the role check, the open start page and logo, the redirect without the broken referer. The rest is the code of 2021.

    The same rules in Spring Security (for comparison, not in this repository)

    
            

    Spring Security is itself a servlet filter (DelegatingFilterProxy → SecurityFilterChain) in front of Spring's DispatcherServlet — the same place as AuthorizationFilter here.

    The order of two filters declared only with annotations is not fixed by the Servlet specification. Here it doesn't matter: AuthorizationFilter reads no parameters, so the encoding is set before any servlet reads the form. To fix an order, list the filters in web.xml (or use @Order / FilterRegistrationBean in Spring Boot).

    Who are you? The session

    HTTP forgets everything between two requests. Tomcat gives every browser a cookie JSESSIONID — only a key; the data (user, the cart, the language) stays in the server's memory under that key. Logging in means putting a UserDto there.

    1. The first request

    The filter calls getSession(): Tomcat creates a session — still without a user — and sends its id, even with the redirect to /login.

    2. A successful login

    changeSessionId(), then setAttribute("user", ...): the browser gets a new id. An id known before the login (session fixation) is useless after it.

    3. After the logout

    invalidate() threw the session away. The browser still sends the old id, Tomcat doesn't know it: a new, empty session, and the filter sends it to /login.

    A JSP is a servlet — and it speaks two languages

    The browser never gets login.jsp. On the first request Tomcat's JSP engine (Jasper) turns it into a Java class, compiles it and runs it like any servlet. <%@ include file="header.jsp"%> pastes the header into the page before that, so the generated class also sets the language: <fmt:setLocale> takes it from the session, <fmt:message key="..."/> looks the text up in translations.properties or translations_ru_RU.properties.

    header.jsp + login.jsp (what was written in 2021, shortened)

    
          

    login_jsp.java (what Tomcat generated and runs) —

    
          

    This is Spring, written by hand

    Every piece of this pharmacy has a Spring counterpart. Spring does not replace the ideas — filters, the session, bundles of texts — it writes the repetitive parts for you.

    This repository (2021)Spring MVC / Spring Boot / Spring Security
    EncodingEncodingFilter: setCharacterEncoding("UTF-8") for every requestCharacterEncodingFilter, registered by Spring Boot automatically
    Who may enterAuthorizationFilter: a set of open paths, sendRedirect("/login")SecurityFilterChain: permitAll(), authenticated(), formLogin()
    Rolesuri.startsWith("/admin") && role != ADMIN (2026).requestMatchers("/admin/**").hasRole("ADMIN") or @PreAuthorize
    LoginLoginServlet: UserDao.findByEmailAndPassword, the user into the sessionUsernamePasswordAuthenticationFilter + UserDetailsService + PasswordEncoder (hashes, not plain text)
    The logged-in usersession.getAttribute("user")SecurityContextHolder, kept in the same HttpSession; @AuthenticationPrincipal
    Logoutsession.invalidate(), redirect.logout() — LogoutFilter
    Session fixationreq.changeSessionId() after the login (2026)Done by default (sessionFixation().changeSessionId())
    LanguageLocaleServlet → sessionScope.lang → <fmt:setLocale>SessionLocaleResolver + LocaleChangeInterceptor (?lang=ru)
    Texts<fmt:message key>, translations*.propertiesMessageSource, messages*.properties, #{key} in Thymeleaf
    Form errorsValidationException with keys of texts (2026)@Valid + BindingResult, messages from MessageSource