request.getQueryString() seems to need some encoding

From the HttpServletRequest#getQueryString() javadoc: Returns: a String containing the query string or null if the URL contains no query string. The value is not decoded by the container. Note the last statement. So you need to URL-decode it youself using java.net.URLDecoder. String queryString = URLDecoder.decode(request.getQueryString(), “UTF-8”); However, the normal way to gather parameters is just … Read more

How to solve this java.lang.NoClassDefFoundError: org/apache/commons/io/output/DeferredFileOutputStream?

The particular exception message is telling you that the mentioned class is missing in the classpath. As the org.apache.commons.io package name hints, the mentioned class is part of the http://commons.apache.org/io project. And indeed, Commons FileUpload has Commons IO as a dependency. You need to download and drop commons-io.jar in the /WEB-INF/lib as well. See also: … Read more

Value passed with request.setAttribute() is not available by request.getParameter()

You’re calling request.getParameter() instead of request.getAttribute() to obtain the value. Since you’ve set it as request attribute, you should also get it as request attribute. So: request.setAttribute(“foo”, foo); is only available by Object foo = request.getAttribute(“foo”); // NOT getParameter(). The getParameter() is only for HTTP request parameters as you can specify in request URL or … Read more

How to find the working folder of a servlet based application in order to load resources

You could use ServletContext#getRealPath() to convert a relative web content path to an absolute disk file system path. String relativeWebPath = “/WEB-INF/static/file1.ext”; String absoluteDiskPath = getServletContext().getRealPath(relativeWebPath); File file = new File(absoluteDiskPath); InputStream input = new FileInputStream(file); // … However, if your sole intent is to get an InputStream out of it, better use ServletContext#getResourceAsStream() instead … Read more

How to define servlet filter order of execution using annotations in WAR

You can indeed not define the filter execution order using @WebFilter annotation. However, to minimize the web.xml usage, it’s sufficient to annotate all filters with just a filterName so that you don’t need the <filter> definition, but just a <filter-mapping> definition in the desired order. For example, @WebFilter(filterName=”filter1″) public class Filter1 implements Filter {} @WebFilter(filterName=”filter2″) … Read more

HTTP request parameters are not available by request.getAttribute()

Here, String url = (String) request.getAttribute(“url”); you’re trying to get a request parameter as a request attribute instead of as a request parameter. This will obviously not do what you want. You need to get a request parameter as a request parameter, not as a request attribute. String url = request.getParameter(“url”); Unrelated to the concrete … Read more

How do I execute multiple servlets in sequence?

Use RequestDispatcher#include() on an URL matching the url-pattern of the Servlet. public class Populate_ALL extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType(“text/plain”); request.getRequestDispatcher(“/populateServlet1”).include(request, response); request.getRequestDispatcher(“/populateServlet2”).include(request, response); request.getRequestDispatcher(“/populateServlet3”).include(request, response); //… } } Note: if those servlets cannot be used independently, then this is the wrong approach and you should be … Read more

HTTP Status 405 – HTTP method is not supported by this URL

This is the default response of the default implementation of HttpServlet#doXxx() method (doGet(), doPost(), doHead(), doPut(), etc). This means that when the doXxx() method is not properly being @Overriden in your servlet class, or when it is explicitly being called via super, then you will face a HTTP 405 “Method not allowed” error. So, you … Read more