Submitting form to Servlet which interacts with database results in blank page

You need to properly handle exceptions. You should not only print them but really throw them. Replace } catch (Exception e) { e.printStackTrace(); // Or System.out.println(e); } by } catch (Exception e) { throw new ServletException(“Login failed”, e); } With this change, you will now get a normal error page with a complete stacktrace about … 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

How to save generated file temporarily in servlet based web application

Never use relative local disk file system paths in a Java EE web application such as new File(“filename.xml”). For an in depth explanation, see also getResourceAsStream() vs FileInputStream. Never use getRealPath() with the purpose to obtain a location to write files. For an in depth explanation, see also What does servletcontext.getRealPath(“/”) mean and when should … Read more

java.lang.IllegalStateException: Cannot (forward | sendRedirect | create session) after response has been committed

A common misunderstanding among starters is that they think that the call of a forward(), sendRedirect(), or sendError() would magically exit and “jump” out of the method block, hereby ignoring the remnant of the code. For example: protected void doXxx() { if (someCondition) { sendRedirect(); } forward(); // This is STILL invoked when someCondition is … Read more

Using special auto start servlet to initialize on startup and share application data

None of both is the better approach. Servlets are intended to listen on HTTP events (HTTP requests), not on deployment events (startup/shutdown). CDI/EJB unavailable? Use ServletContextListener @WebListener public class Config implements ServletContextListener { public void contextInitialized(ServletContextEvent event) { // Do stuff during webapp’s startup. } public void contextDestroyed(ServletContextEvent event) { // Do stuff during webapp’s … Read more

Where to place and how to read configuration resource files in servlet based application?

It’s your choice. There are basically three ways in a Java web application archive (WAR): 1. Put it in classpath So that you can load it by ClassLoader#getResourceAsStream() with a classpath-relative path: ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); InputStream input = classLoader.getResourceAsStream(“foo.properties”); // … Properties properties = new Properties(); properties.load(input); Here foo.properties is supposed to be placed … Read more