devxlogo

Tip Bank

Inject an EJB 3.0 into the init() Method of a Servlet

This tip shows you how to inject an EJB 3.0 into the init() method of a servlet. //place all the required imports hereimport package.to.EJB.interface.myInterface;public class EJB30Servlet extends HttpServlet {@EJBmyInterface myobj;public

Destroy a Java Logger using the contextDestroyed Method

public void contextDestroyed(ServletContextEvent e) { //get the servlet context ServletContext servletContext = e.getServletContext(); //get the real path String root = servletContext.getRealPath(“/”); //get all handlers Handler[] handlers = logger.getHandlers(); //remove all

Be Careful of Negative Array Indices

While porting a 32-bit application to a 64-bit version, I discovered an interesting effect. The original code used negative array indexes frequently, which worked fine on a 32-bit machine, but

Build a Custom Formatter for a Java.util.logging Logger

For a simple custom logger, you can inherit from the base java.util.logging.Formatter class and override its format method to customize the output. Here’s an example: import java.io.*;import java.util.*;import java.util.logging.*;public class