Create a Java web app with a Servlet and Filter in NetBeans

NetBeans provides a practical route from an empty project to a deployed web endpoint. Here, a Servlet handles /greeting, while a Filter wraps requests in the servlet container’s chain. The example is small, but the pattern carries across Java EE and Jakarta EE applications.

The build uses Java with Maven, a supported JDK and GlassFish. Modern projects import jakarta.servlet; older systems may expose javax.servlet. Keeping the namespace, Maven dependency and server aligned prevents common class-loading failures.

The result is a testable WAR suitable for a developer laptop, an Australian corporate data centre or managed cloud hosting, with encoding, debugging and deployment checks included along the way.

Set up a Maven-based Java web project

Choose File > New Project, then Java with Maven > Web Application. Set the group to com.example.au, name the artefact au-web-app, select a JDK and finish the wizard. Java sources belong under src/main/java, while web resources sit under src/main/webapp; GlassFish can be selected as the target server in the project properties.

For a new Jakarta EE deployment, use jakarta.servlet throughout. The archetype usually supplies the Servlet API; if adding it manually, give it provided scope because the container supplies those classes. A legacy javax.servlet project should keep matching imports, dependencies and server rather than mixing APIs.

The IDE’s background on the IDE explains the older Java EE and newer Jakarta branding. Once the project exists, check the Maven dependency tree against the container version before adding further libraries.

Create the servlet and define its route

Create GreetingServlet in com.example.au.web, extend HttpServlet and annotate it with its URL mapping:

package com.example.au.web;

import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "GreetingServlet", urlPatterns = "/greeting")
public class GreetingServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request,
                         HttpServletResponse response) throws IOException {
        String name = request.getParameter("name");

        if (name == null || name.isBlank()) {
            name = "mate";
        }

        response.setContentType("text/plain");
        response.setCharacterEncoding("UTF-8");
        response.getWriter().write("Greetings, " + name + "!");
    }
}

doGet handles HTTP GET requests. When no name is supplied, the default “mate” reflects the relaxed greeting common in Australia, although a caller can still supply another name. The plain-text response avoids injecting the parameter into HTML; an HTML endpoint should escape input or render it through a safe template.

Add a filter to the request path

Create RequestTraceFilter beside the servlet and map it to every application URL, allowing encoding to be established before parameter reading:

package com.example.au.web;

import jakarta.servlet.Filter;
import jakarta.servlet.FilterChain;
import jakarta.servlet.ServletException;
import jakarta.servlet.ServletRequest;
import jakarta.servlet.ServletResponse;
import jakarta.servlet.annotation.WebFilter;
import java.io.IOException;

@WebFilter(urlPatterns = "/*")
public class RequestTraceFilter implements Filter {

    @Override
    public void doFilter(ServletRequest request,
                         ServletResponse response,
                         FilterChain chain)
            throws IOException, ServletException {

        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        response.setHeader("X-Content-Type-Options", "nosniff");
        response.setHeader("Referrer-Policy", "same-origin");

        long started = System.nanoTime();
        try {
            chain.doFilter(request, response);
        } finally {
            long millis = (System.nanoTime() - started) / 1_000_000;
            System.out.println("Request completed in " + millis + " ms");
        }
    }
}

The filter configures the request and response, calls chain.doFilter and records elapsed time in a finally block so failures are included. Because it surrounds the servlet, the same structure can support validation, tracing or throttling. When several filters exist, define their order in WEB-INF/web.xml; relying on annotation order can be brittle.

Run the application in NetBeans

Run the project and start or register GlassFish. Open http://localhost:8080/au-web-app/greeting?name=Ruby to see Greetings, Ruby!. The /greeting route comes from the annotation, while the context path follows the artefact and WAR configuration.

GlassFish provides the Servlet and Filter runtime, keeping the WAR portable to another compatible Jakarta server. Tomcat can also be configured, but its Jakarta or Javax generation must match the imports. Further tutorials and configuration notes are available through NetBeans Blog.

Teams in Sydney and Melbourne often share staging containers across several development groups, making a reproducible WAR valuable even when final hosting differs.

Test routes, parameters and filter behaviour

Use the browser and NetBeans debugger together. Put breakpoints in doGet and doFilter, then test missing, encoded and Unicode names. From a terminal, run curl --get --data-urlencode "name=Café" http://localhost:8080/au-web-app/greeting to confirm decoding and UTF-8 handling agree.

Teams in Brisbane and Perth may test against distant shared staging systems, so compare logged server timings rather than judging from page load alone. Keep tests under src/test/java, and use a real or embedded servlet container when the filter chain itself is the behaviour under examination.

Prepare the WAR for production deployment

Before deployment, replace the sample greeting with the real data flow, cap request bodies where appropriate, validate inputs and keep credentials in managed secret services. Australian requirements also affect the design: the Privacy Act and Australian Privacy Principles commonly govern personal information, while contracts may require data to stay in Australia or another named jurisdiction.

For high-volume IP screening, a simple Bloom filter implementation shows how a compact probabilistic set can reject many unwanted addresses before costly checks. It cannot guarantee that a match is malicious and can produce false positives, so place it at the gateway or ahead of the Servlet rather than treating it as authorisation.

Australian buyers compare AUD licence and hosting costs with GST-inclusive pricing, and frequently require options for managed cloud and on-premises systems. Package the application as a WAR, document its Java and Jakarta requirements, test the final artefact on the target server and retain a rollback path. These checks turn the NetBeans demo into a dependable Java web application.