Get full URL and query string in Servlet for both HTTP and HTTPS requests

By design, getRequestURL() gives you the full URL, missing only the query string. In HttpServletRequest, you can get individual parts of the URI using the methods below: // Example: http://myhost:8080/people?lastname=Fox&age=30 String uri = request.getScheme() + “://” + // “http” + “:// request.getServerName() + // “myhost” “:” + // “:” request.getServerPort() + // “8080” request.getRequestURI() + … Read more

XSS prevention in JSP/Servlet web application

XSS can be prevented in JSP by using JSTL <c:out> tag or fn:escapeXml() EL function when (re)displaying user-controlled input. This includes request parameters, headers, cookies, URL, body, etc. Anything which you extract from the request object. Also the user-controlled input from previous requests which is stored in a database needs to be escaped during redisplaying. … Read more

Difference between each instance of servlet and each thread of servlet in servlets? [duplicate]

When the Servlet container starts, it: reads web.xml; finds the declared Servlets in the classpath; and loads and instantiates each Servlet only once. Roughly, like this: String urlPattern = parseWebXmlAndRetrieveServletUrlPattern(); String servletClass = parseWebXmlAndRetrieveServletClass(); HttpServlet servlet = (HttpServlet) Class.forName(servletClass).newInstance(); servlet.init(); servlets.put(urlPattern, servlet); // Similar to a map interface. Those Servlets are stored in memory and … Read more

Browser can’t access/find relative resources like CSS, images and links when calling a Servlet which forwards to a JSP

All relative URLs in the HTML page generated by the JSP file are relative to the current request URL (the URL as you see in the browser address bar) and not to the location of the JSP file in the server side as you seem to expect. It’s namely the webbrowser who has to download … 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

init-param and context-param

<init-param> and <context-param> are static parameters which are stored in web.xml file. If you have any data which doesn’t change frequently you can store it in one of them. If you want to store particular data which is confined to a particular servlet scope, then you can use <init-param> .Anything you declare inside <init-param> is … Read more

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