Where to place and how to read configuration resource files in servlet based application?

It’s your choice. There are basically three ways in a Java web application archive (WAR): 1. Put it in classpath So that you can load it by ClassLoader#getResourceAsStream() with a classpath-relative path: ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); InputStream input = classLoader.getResourceAsStream(“foo.properties”); // … Properties properties = new Properties(); properties.load(input); Here foo.properties is supposed to be placed … Read more

Recommended way to save uploaded files in a servlet application

Store it anywhere in an accessible location except of the IDE’s project folder aka the server’s deploy folder, for reasons mentioned in the answer to Uploaded image only available after refreshing the page: Changes in the IDE’s project folder does not immediately get reflected in the server’s work folder. There’s kind of a background job … Read more

How do I import the javax.servlet / jakarta.servlet API in my Eclipse project?

Ensure you’ve the right Eclipse and Server version Ensure that you’re using at least Eclipse IDE for Enterprise Java developers (with the Enterprise). It contains development tools to create dynamic web projects and easily integrate servletcontainers (those tools are part of Web Tools Platform, WTP). In case you already had Eclipse IDE for Java (without … Read more

Why does Spring MVC respond with a 404 and report “No mapping found for HTTP request with URI […] in DispatcherServlet”?

Your standard Spring MVC application will serve all requests through a DispatcherServlet that you’ve registered with your Servlet container. The DispatcherServlet looks at its ApplicationContext and, if available, the ApplicationContext registered with a ContextLoaderListener for special beans it needs to setup its request serving logic. These beans are described in the documentation. Arguably the most … Read more

How do servlets work? Instantiation, sessions, shared variables and multithreading

ServletContext When the servlet container (like Apache Tomcat) starts up, it will deploy and load all its web applications. When a web application is loaded, the servlet container creates the ServletContext once and keeps it in the server’s memory. The web app’s web.xml and all of included web-fragment.xml files is parsed, and each <servlet>, <filter> … Read more