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.
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.
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.
Parses the bytes, finds the servlet for the URL, builds the chain.
setCharacterEncoding("UTF-8") on the request and the response, then always doFilter.
Open page? A user in the session? An /admin page and role ADMIN? Only then doFilter.
doGet / doPost: services, DAO, SQL.
After forward: the HTML, in the language of the session.
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).
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.
The filter calls getSession(): Tomcat creates a session — still without a user — and sends its id, even with the redirect to /login.
changeSessionId(), then setAttribute("user", ...): the browser gets a new id. An id known before the login (session fixation) is useless after it.
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.
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) —
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 | |
|---|---|---|
| Encoding | EncodingFilter: setCharacterEncoding("UTF-8") for every request | CharacterEncodingFilter, registered by Spring Boot automatically |
| Who may enter | AuthorizationFilter: a set of open paths, sendRedirect("/login") | SecurityFilterChain: permitAll(), authenticated(), formLogin() |
| Roles | uri.startsWith("/admin") && role != ADMIN (2026) | .requestMatchers("/admin/**").hasRole("ADMIN") or @PreAuthorize |
| Login | LoginServlet: UserDao.findByEmailAndPassword, the user into the session | UsernamePasswordAuthenticationFilter + UserDetailsService + PasswordEncoder (hashes, not plain text) |
| The logged-in user | session.getAttribute("user") | SecurityContextHolder, kept in the same HttpSession; @AuthenticationPrincipal |
| Logout | session.invalidate(), redirect | .logout() — LogoutFilter |
| Session fixation | req.changeSessionId() after the login (2026) | Done by default (sessionFixation().changeSessionId()) |
| Language | LocaleServlet → sessionScope.lang → <fmt:setLocale> | SessionLocaleResolver + LocaleChangeInterceptor (?lang=ru) |
| Texts | <fmt:message key>, translations*.properties | MessageSource, messages*.properties, #{key} in Thymeleaf |
| Form errors | ValidationException with keys of texts (2026) | @Valid + BindingResult, messages from MessageSource |