SERVLET / CATATAN</>↗Servlet20 Jun 2007
Implementing the HttpSessionAttributeListener will make the servlet container inform you about session attribute changes. The notification is in a form of HttpSessionBindingEvent object. The getName() on this object tell the name of the attribute while the getValue() method tell about the value that was added, replaced or removed. package org.kodejava.servlet; import javax.servlet.annotation.WebListener; import javax.servlet.http.HttpSessionAttributeListener; import […]
SERVLET / CATATAN</>↗Servlet19 Jun 2007
The Servlet specification define an HttpSessionListener interface that can be implemented if we want to listen to session creation and removal events. The interface has two methods that we can implement, the sessionCreated(HttpSessionEvent event) and sessionDestroyed(HttpSessionEvent event) methods. To activate the listener we need to register it in the servlet container. To register the listener […]
SERVLET / CATATAN</>↗Servlet16 Jun 2007
In a web application you might want to invalidate user session, for instance in a logout Servlet or JSP. There is an invalidate() method in the HttpSession interface, this method invalidates the session, and it removes all attributes from the session object. package org.kodejava.servlet; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.ht...
SERVLET / CATATAN</>↗Servlet13 Jun 2007
package org.kodejava.servlet; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.PrintWriter; @WebServlet(name = "ClientAddressServlet", urlPatterns = "/client-ip") public class ClientAddressServlet extends HttpServlet { @Override protected void doPost(HttpServletRequest r...
CORE API / CATATAN</>↗Core API12 Jun 2007
package org.kodejava.util; import java.util.Calendar; import java.util.GregorianCalendar; import java.util.TimeZone; public class TimeZoneExample { public static void main(String[] args) { // Create a calendar object and set it time based on the local // time zone Calendar localTime = Calendar.getInstance(); localTime.set(Calendar.HOUR, 17); localTime.set(Calendar.MINUTE, 15); localTime.set(Calendar.SECOND, 20); int hour = localTime.get(Calendar.HOUR); int minute = localTime....
CORE API / CATATAN</>↗Core API09 Jun 2007
package org.kodejava.util; import java.text.DecimalFormat; import java.text.NumberFormat; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Locale; import java.util.Random; public class DefaultLocaleExample { public static void main(String[] args) { // Use Random class to generate some random number Random random = new Random(); // We use the system default locale to format a number and a date. NumberFormat formatter = […]