Moving resources under WEB-INF

What is the easiest method for me to safely move all html/js/images folders under the WEB-INF without breaking all links/forwarding to resources in these folders and make sure these resources are not directly accessible? You’re making a thiniking mistake here. HTML/JS/image (and CSS) resources need to be directly accessible anyway. For JSPs the story is … Read more

could not find Factory: javax.faces.context.FacesContextFactory

This listener is since JSF 1.x supposed to be automatically registered by the jsf_core.tld tag library definition file. You can find it in the /META-INF folder of the JSF implementation JAR file. In case of Mojarra 2.1.3 (which you seem to be using according the logs), the listener is registered as follows from line 80 … Read more

Servlet filter runs in infinite redirect loop when user is not logged in

This AuthenticationFilter also runs when login.html is being requested. However, the code is redirecting to login.html once again instead of continuing the filter chain. This explains the infinite redirect loop. You need to let the filter just continue the request if the currently requested page is already the login page itself. E.g. public void doFilter(ServletRequest … Read more

Google Recaptcha v3 example demo

Simple code to implement ReCaptcha v3 The basic JS code <script src=”https://www.google.com/recaptcha/api.js?render=your reCAPTCHA site key here”></script> <script> grecaptcha.ready(function() { // do request for recaptcha token // response is promise with passed token grecaptcha.execute(‘your reCAPTCHA site key here’, {action:’validate_captcha’}) .then(function(token) { // add token value to form document.getElementById(‘g-recaptcha-response’).value = token; }); }); </script> The basic HTML … Read more

How do delete a HTTP response header?

You can’t delete headers afterwards by the standard Servlet API. Your best bet is to just prevent the header from being set. You can do this by creating a Filter which replaces the ServletResponse with a custom HttpServletResponseWrapper implementation which skips the setHeader()‘s job whenever the header name is Content-Disposition. Basically: @Override public void doFilter(ServletRequest … Read more

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