Why do we use web.xml? [closed]

Generally speaking, this is the configuration file of web applications in java. It instructs the servlet container (tomcat for ex.) which classes to load, what parameters to set in the context, and how to intercept requests coming from browsers. There you specify: what servlets (and filters) you want to use and what URLs you want … Read more

Disable all default HTTP error response content in Tomcat

If you do not want tomcat to show an error page, then do not use sendError(…). Instead use setStatus(…). e.g. if you want to give a 405 response, then you do response.setStatus(HttpServletResponse.SC_METHOD_NOT_ALLOWED); response.getWriter().println(“The method ” + request.getMethod() + ” is not supported by this service.”); Also remember not to throw any Exceptions from your servlet. … Read more

How can I know if the request to the servlet was executed using HTTP or HTTPS?

HttpSerlvetRequest.isSecure() is the answer. The ServletContainer is responsible for returning true in the following cases: If the ServletContainer can itself accept requests on https. If there is a LoadBalancer in front of ServletContainer. And , the LoadBlancer has got the request on https and has dispatched the same to the ServletContainer on plain http. In … Read more

Return a stream with Spring MVC’s ResponseEntity

Spring’s InputStreamResource works well. You need to set the Content-Length manually, or it appears that Spring attempts to read the stream to obtain the Content-Length. InputStreamResource inputStreamResource = new InputStreamResource(inputStream); httpHeaders.setContentLength(contentLengthOfStream); return new ResponseEntity(inputStreamResource, httpHeaders, HttpStatus.OK); I never found any web pages suggesting using this class. I only guessed it because I noticed there were … 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

Hidden features of JSP/Servlet [closed]

Note: I find it hard to think of any “hidden features” for JSP/Servlet. In my opinion “best practices” is a better wording and I can think of any of them. It also really depends on your experience with JSP/Servlet. After years of developing you don’t see those “hidden features” anymore. At any way, I’ll list … Read more

How to use relative paths without including the context root name?

If your actual concern is the dynamicness of the webapp context (the “AppName” part), then just retrieve it dynamically by HttpServletRequest#getContextPath(). <head> <link rel=”stylesheet” href=”https://stackoverflow.com/questions/4764405/${pageContext.request.contextPath}/templates/style/main.css” /> <script src=”${pageContext.request.contextPath}/templates/js/main.js”></script> <script>var base = “${pageContext.request.contextPath}”;</script> </head> <body> <a href=”${pageContext.request.contextPath}/pages/foo.jsp”>link</a> </body> If you want to set a base path for all relative links so that you don’t need to … Read more

Creating a mock HttpServletRequest out of a url string?

Here it is how to use MockHttpServletRequest: // given MockHttpServletRequest request = new MockHttpServletRequest(); request.setServerName(“www.example.com”); request.setRequestURI(“/foo”); request.setQueryString(“param1=value1&param”); // when String url = request.getRequestURL() + ‘?’ + request.getQueryString(); // assuming there is always queryString. // then assertThat(url, is(“http://www.example.com:80/foo?param1=value1&param”));

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