Autowiring in servlet

I followed the solution in the following link, and it works fine: Access Spring beans from a servlet in JBoss public class MyServlet extends HttpServlet { @Autowired private MyService myService; public void init(ServletConfig config) { super.init(config); SpringBeanAutowiringSupport.processInjectionBasedOnServletContext(this, config.getServletContext()); } }

Using ServletOutputStream to write very large files in a Java servlet without memory issues

The average decent servletcontainer itself flushes the stream by default every ~2KB. You should really not have the need to explicitly call flush() on the OutputStream of the HttpServletResponse at intervals when sequentially streaming data from the one and same source. In for example Tomcat (and Websphere!) this is configureable as bufferSize attribute of the … Read more

Using ServletOutputStream to write very large files in a Java servlet without memory issues

The average decent servletcontainer itself flushes the stream by default every ~2KB. You should really not have the need to explicitly call flush() on the OutputStream of the HttpServletResponse at intervals when sequentially streaming data from the one and same source. In for example Tomcat (and Websphere!) this is configureable as bufferSize attribute of the … Read more

How to get host name with port from a http or https request

You can use HttpServletRequest.getScheme() to retrieve either “http” or “https”. Using it along with HttpServletRequest.getServerName() should be enough to rebuild the portion of the URL you need. You don’t need to explicitly put the port in the URL if you’re using the standard ones (80 for http and 443 for https). Edit: If your servlet … Read more

How to set session timeout dynamically in Java web applications?

Instead of using a ServletContextListener, use a HttpSessionListener. In the sessionCreated() method, you can set the session timeout programmatically: public class MyHttpSessionListener implements HttpSessionListener { public void sessionCreated(HttpSessionEvent event){ event.getSession().setMaxInactiveInterval(15 * 60); // in seconds } public void sessionDestroyed(HttpSessionEvent event) {} } And don’t forget to define the listener in the deployment descriptor: <webapp> … … Read more

Is there a static way to get the current HttpServletRequest in Spring

If you are using spring you can do the following: public static HttpServletRequest getCurrentHttpRequest(){ RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes(); if (requestAttributes instanceof ServletRequestAttributes) { HttpServletRequest request = ((ServletRequestAttributes)requestAttributes).getRequest(); return request; } logger.debug(“Not called in the context of an HTTP request”); return null; }

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