Quick revision
The short answer
The container translates a JSP page into Java servlet source, compiles it, and runs the resulting servlet; later requests reuse the compiled class, and in development mode the container recompiles when the file changes. JSTL tags such as c:forEach, c:if and c:out, together with Expression Language (${...}) that reads attributes from the page, request, session and application scopes, replace scriptlets that mix Java into markup. The risk of ${param.name} is that plain EL output is not HTML-escaped, so it allows reflected XSS; we should write <c:out value="${param.name}"/> or use fn:escapeXml instead.
What to remember
In this answer, we saw that the container translates each JSP into a servlet, compiles it once and reuses it, recompiling in development mode when the file changes. JSTL tags and EL replace scriptlets and read data from the four scopes, keeping pages as templates. Bare ${param.name} output is not escaped and causes reflected XSS, so user data must go through c:out or fn:escapeXml, and under Jakarta EE 9 and later JSP is Jakarta Pages with jakarta.* packages and new taglib URIs.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
The container translates a JSP page into Java servlet source, compiles it, and runs the resulting servlet; later requests reuse the compiled class, and in development mode the container recompiles when the file changes. JSTL tags such as c:forEach, c:if and c:out, together with Expression Language (${...}) that reads attributes from the page, request, session and application scopes, replace scriptlets that mix Java into markup. The risk of ${param.name} is that plain EL output is not HTML-escaped, so it allows reflected XSS; we should write <c:out value="${param.name}"/> or use fn:escapeXml instead.
Why this question is asked
Many long-lived Java web applications still render their pages with JSP, and maintaining them safely means understanding how a page becomes a servlet and where escaping does and does not happen. Interviewers ask this because the last part, the XSS risk in a bare EL expression, is a real vulnerability found in production code. In this answer, we will follow a JSP through translation and compilation, compare scriptlets with JSTL and EL, look at the escaping problem with a concrete example, and note what changed with Jakarta Pages.
1How the Container Processes a JSP
A JSP (now officially Jakarta Server Pages, often called Jakarta Pages) is an HTML file with special tags. The container's JSP engine, Jasper in Tomcat, does not interpret it on every request. The first time the page is requested, Jasper translates it into the Java source of a servlet: every piece of static HTML becomes an out.write(...) call, and every tag becomes Java code. Then it compiles that source into a class, loads it and calls it like any other servlet.
Later requests go straight to the compiled servlet, which is why the first request to a page is slow and later ones are fast. In development mode, Jasper checks the JSP file's timestamp and retranslates when it changes. In production, this check is usually turned off or made infrequent, and pages can be precompiled at build time so no user pays for the first compilation.
- Request for /orders.jspFirst time, or file changed?
- Compiled servlet up to date?Dev mode checks timestampsYesRun the servletNoTranslate JSP to Java
- Translate JSP to Java
orders_jsp.java:out.writecalls - Compile to a class
orders_jsp.class, loaded once - Run the servletWrites HTML to the response
5 steps in order. Play them, or step through them one at a time.
Translation and compilation happen once; every later request runs compiled code.
2Scriptlets Versus JSTL and Expression Language
Early JSPs used scriptlets: raw Java code between <% and %>. They work, but they mix business logic, loops and markup in one file, cannot be unit tested, and break easily when the HTML changes. They are discouraged for all new or modified pages.
JSTL, now the Jakarta Standard Tag Library, gives us tags for the common needs: c:forEach for loops, c:if and c:choose for conditions, c:out for output, fmt tags for formatting dates and numbers, and c:url for building URLs. Expression Language, written ${...}, reads values without Java code. ${order.total} finds an attribute named order by searching the page, request, session and application scopes in that order, then calls getTotal() on it. Together they keep the page as a template, while the controller prepares the data.
3The Risk of ${param.name}
param is an implicit EL object that holds the request's query parameters. So ${param.name} prints whatever the visitor put in ?name=. The catch is that EL written directly in the page body is not HTML-escaped. If an attacker sends a victim a link with a script in the parameter, the page writes that script into the HTML and the victim's browser runs it. That is reflected cross-site scripting (XSS): the attacker's input is reflected back as code.
Notice that c:out turns < and > into < and >, so the browser shows the text instead of running it. The rule applies to every value that comes from users, not just param: names, comments and search terms stored in the database are just as dangerous. Escaping for HTML is also not enough inside JavaScript blocks or URLs, which need their own encoding, so the safest design keeps user data out of inline scripts entirely.
4JSP Under Jakarta EE 9 and Later
When Java EE moved to the Eclipse Foundation and became Jakarta EE 9, all packages changed from javax.* to jakarta.*. JSP became Jakarta Pages, its classes moved to jakarta.servlet.jsp, and Jakarta Tags 3.0 introduced short taglib URIs such as jakarta.tags.core in place of http://java.sun.com/jsp/jstl/core. Tomcat 10 and later implement these versions. For new projects, JSP is a legacy choice: Spring Boot discourages it with executable jars, and template engines such as Thymeleaf, which escapes output by default, or a separate front end are more common.
5Follow-up questions
How do you write a custom tag file?
A tag file is a small JSP fragment saved under WEB-INF/tags with the .tag extension, for example WEB-INF/tags/money.tag. It starts with <%@ tag body-content="empty" %> and declares inputs with <%@ attribute name="amount" required="true" type="java.math.BigDecimal" %>, then uses them in EL as ${amount}. A page imports the folder with <%@ taglib prefix="my" tagdir="/WEB-INF/tags" %> and writes <my:money amount="${order.total}"/>. The container compiles tag files like JSPs, so no Java class or TLD file is needed, which makes them the easiest way to reuse a piece of markup.
Why is EL output not escaped by default?
EL was designed as a general expression language used in many places, including tag attributes, and JSP keeps its output unescaped for backward compatibility. Some containers offer options to escape EL in templates, but relying on them is fragile; c:out or fn:escapeXml makes the escaping visible in the page itself.
Where does the translated servlet live?
Tomcat writes the generated Java source and class files under its work directory, for example work/Catalina/localhost/app/org/apache/jsp. Reading the generated orders_jsp.java is a good way to understand exactly what a page does.
6Conclusion
In this answer, we saw that the container translates each JSP into a servlet, compiles it once and reuses it, recompiling in development mode when the file changes. JSTL tags and EL replace scriptlets and read data from the four scopes, keeping pages as templates. Bare ${param.name} output is not escaped and causes reflected XSS, so user data must go through c:out or fn:escapeXml, and under Jakarta EE 9 and later JSP is Jakarta Pages with jakarta.* packages and new taglib URIs.
Open with
The container's JSP engine, Jasper in Tomcat, translates the JSP into the Java source of a servlet, where static HTML becomes out.write calls and tags become code, then compiles it and runs it like any servlet.
Then, if they want more
Later requests reuse the compiled class; in development mode it checks the file's timestamp and recompiles when it changes, and in production we often precompile. Scriptlets put raw Java in the page, which mixes logic and markup and cannot be tested, so we use JSTL instead: c:forEach for loops, c:if for conditions, c:out for output, fmt for formatting.
Expression Language reads attributes by name from page, request, session and application scope, so ${order.total} calls getTotal on the order attribute. The risk with ${param.name} is that EL in the page body is not HTML-escaped, so a link with a script in the name parameter becomes reflected XSS.
The fix is c:out, which escapes by default, or fn:escapeXml inside attributes, for every user-controlled value. And since Jakarta EE 9, JSP is Jakarta Pages, packages are jakarta.*, and the taglib URI is jakarta.tags.core. For new projects I would not choose JSP.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Jakarta EE 9
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Jakarta EE specifications
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "How is a JSP page processed by the servlet container, and what do JSTL tags (c:forEach...": https://scrivio-interview-library.netlify.app/java/servlets/ac7601fb15/