Change default homepage in root path to servlet with doGet

How can I change the contents of this page to something else ? Open the underlying JSP/HTML/XHTML file in a text editor. This page is identified by <welcome-file> entry in web.xml. If it’s e.g. <welcome-file>index.jsp</welcome-file>, then you need to open /index.jsp file in your project’s web content in the IDE builtin text editor. Or, at … 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

Difference between / and /* in servlet mapping url pattern

<url-pattern>/*</url-pattern> The /* on a servlet overrides all other servlets, including all servlets provided by the servletcontainer such as the default servlet and the JSP servlet. Whatever request you fire, it will end up in that servlet. This is thus a bad URL pattern for servlets. Usually, you’d like to use /* on a Filter … Read more