Servlet returns “HTTP Status 404 The requested resource (/servlet) is not available”

Introduction This can have a lot of causes which are broken down in following sections: Put servlet class in a package Set servlet URL in url-pattern @WebServlet works only on Servlet 3.0 or newer javax.servlet.* doesn’t work anymore in Servlet 5.0 or newer Make sure compiled *.class file is present in built WAR Test the … Read more

Why do Java webapps use .do extension? Where did it come from?

To my knowledge, this convention has been spread by Struts1. The user guide puts it like this: 5.4.2 Configure the ActionServlet Mapping Note: The material in this section is not specific to Struts. The configuration of servlet mappings is defined in the Java Servlet Specification. This section describes the most common means of configuring a … Read more

How to test my servlet using JUnit

You can do this using Mockito to have the mock return the correct params, verify they were indeed called (optionally specify number of times), write the ‘result’ and verify it’s correct. import static org.junit.Assert.*; import static org.mockito.Mockito.*; import java.io.*; import javax.servlet.http.*; import org.apache.commons.io.FileUtils; import org.junit.Test; public class TestMyServlet extends Mockito{ @Test public void testServlet() throws … Read more

Spring: how do I inject an HttpServletRequest into a request-scoped bean?

Spring exposes the current HttpServletRequest object (as well as the current HttpSession object) through a wrapper object of type ServletRequestAttributes. This wrapper object is bound to ThreadLocal and is obtained by calling the static method RequestContextHolder.currentRequestAttributes(). ServletRequestAttributes provides the method getRequest() to get the current request, getSession() to get the current session and other methods … Read more

ContextLoaderListener or not?

In your case, no, there’s no reason to keep the ContextLoaderListener and applicationContext.xml. If your app works fine with just the servlet’s context, that stick with that, it’s simpler. Yes, the generally-encouraged pattern is to keep non-web stuff in the webapp-level context, but it’s nothing more than a weak convention. The only compelling reasons to … 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

Can I exclude some concrete urls from inside ?

The standard Servlet API doesn’t support this facility. You may want either to use a rewrite-URL filter for this like Tuckey’s one (which is much similar Apache HTTPD’s mod_rewrite), or to add a check in the doFilter() method of the Filter listening on /*. String path = ((HttpServletRequest) request).getRequestURI(); if (path.startsWith(“/specialpath/”)) { chain.doFilter(request, response); // … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)