How to get the Servlet Context from ServletRequest in Servlet 2.5?

You can get it by the HttpSession#getServletContext(). ServletContext context = request.getSession().getServletContext(); This may however unnecessarily create the session when not desired. But when you’re already sitting in an instance of the HttpServlet class, just use the inherited GenericServlet#getServletContext() method. @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { ServletContext context = getServletContext(); // … Read more

cvc-complex-type.2.4.a: Invalid content was found starting with element ‘init-param’

The order of elements in web.xml matters and in all examples I’ve come across, the <load-on-startup> comes after <init-param>. <servlet> <servlet-name>spring1</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet>

ServletConfig vs ServletContext

The ServletConfig parameters are specified for a particular servlet and are unknown to other servlets. It is used for intializing purposes. The ServletContext parameters are specified for an entire application outside of any particular servlet and are available to all the servlets within that application. It is application scoped and thus globally accessible across the … Read more

What does WEB-INF stand for in a Java EE web application? [closed]

As far as I know, “INF” stands for “Information”, as you said. It probably was named WEB-INF for similarity with the META-INF directory in JAR files. Sometimes the meaning of a directory changes so much over time that it no longer makes sense. For example, bin directories in Unix/Linux often contain non-binary “executable” files, such … Read more

How do I get the remote address of a client in servlet?

try this: public static String getClientIpAddr(HttpServletRequest request) { String ip = request.getHeader(“X-Forwarded-For”); if (ip == null || ip.length() == 0 || “unknown”.equalsIgnoreCase(ip)) { ip = request.getHeader(“Proxy-Client-IP”); } if (ip == null || ip.length() == 0 || “unknown”.equalsIgnoreCase(ip)) { ip = request.getHeader(“WL-Proxy-Client-IP”); } if (ip == null || ip.length() == 0 || “unknown”.equalsIgnoreCase(ip)) { ip = … Read more

Giving multiple URL patterns to Servlet Filter

If an URL pattern starts with /, then it’s relative to the context root. The /Admin/* URL pattern would only match pages on http://localhost:8080/EMS2/Admin/* (assuming that /EMS2 is the context path), but you have them actually on http://localhost:8080/EMS2/faces/Html/Admin/*, so your URL pattern never matches. You need to prefix your URL patterns with /faces/Html as well … 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