Here’s a simple “HttpSessionAttributeListener” example to keep track session’s attribute in a web application. If you want to keep monitor your session’s attribute add , update and remove behavior, then consider this listener. Java Source package com.mkyong; import javax.servlet.http.HttpSessionAttributeListener; import javax.servlet.http.HttpSessionBindingEvent; public class MyAttributeListener implements HttpSessionAttributeListener { @Override public void attributeAdded(HttpSessionBindingEvent event) { String attributeName […]

Read more A simple HttpSessionAttributeListener example

Here’s a simple “HttpSessionListener” example to keep track the total number of active sessions in a web application. If you want to keep monitor your session’s create and remove behavior, then consider this listener. Java Source package com.mkyong; import javax.servlet.http.HttpSessionEvent; import javax.servlet.http.HttpSessionListener; public class SessionCounterListener implements HttpSessionListener { private static int totalActiveSessions; public static int […]

Read more A simple HttpSessionListener example – active sessions counter

Here’s a example to access the Cookies at the previous article “simple cookie example” Java Source package com.mkyong; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class ServletDemoCookie extends HttpServlet{ public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException{ response.setContentType("text/html"); PrintWriter pw = response.getWriter(); Cookie[] cookie = request.getCookies(); pw.println("All Cookies in […]

Read more How to access the Cookies at the Client site

Here’s a simple Cookie example in Servlet Java Source package com.mkyong; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class ServletDemo extends HttpServlet{ public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException{ response.setContentType("text/html"); PrintWriter pw = response.getWriter(); Cookie cookie = new Cookie("url","mkyong dot com"); cookie.setMaxAge(60*60); //1 hour response.addCookie(cookie); pw.println("Cookies created"); } […]

Read more A simple cookie example in servlet

The session timeout in a web application can be configurable in two ways 1) Timeout in the deployment descriptor (web.xml) – Specified the timeout value in “minute” , enclose with “session-config” element. <web-app …> <session-config> <session-timeout>20</session-timeout> </session-config> </web-app> The above setting is apply for the entire web application, and session will be kill by container […]

Read more How to configure the session timeout in servlet

Many servlet’s developers are confuse about the different between “ServletConfig” and “ServletContext”. Actually the “ServletContext” name is quite confusing, it should change to “AppConfig” or “AppContext” in the future release 🙂 ContextConfig 1) This is one per “web application”, access globally by all the servlets’ class 2) web.xml – within the web-app element and outside […]

Read more Different between ServleConfig and ServletContext

There are two ways to detect whether the session is existed. 1) request.getSession(); + session.isNew() – Retrieve a session from “request.getSession();”, this function will always return a session no matter what, it’s equivalent to request.getSession(true);. The only problem is you do not know whether this is new or existed session. – Later you can check […]

Read more How to check whether the session is existed?

Here’s a serlvet code example to demonstrate how to pass a parameter to whole web application by using ServletContext “init-param” in web.xml. In the deployment descriptor (web.xml) Put your parameter value in “init-param” and make sure outside the “servlet” element <servlet> <servlet-name>ServletName</servlet-name> <servlet-class>com.mkyong.ServletDemo</servlet-class> </servlet> <context-param> <param-name>email</param-name> <param-value>[email protected]</param-value> </context-param> Servlet code public void doGet(HttpServletRequest request, HttpServletResponse […]

Read more How to pass parameters to whole web application – ServletContext

Here’s a serlvet code example to demonstrate how to pass a parameter to a servlet by using ServletConfig “init-param” in web.xml In the deployment descriptor (web.xml) Put your parameter value in “init-param” and make sure inside the “servlet” element <servlet> <servlet-name>ServletName</servlet-name> <servlet-class>com.mkyong.ServletDemo</servlet-class> <init-param> <param-name>email</param-name> <param-value>[email protected]</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>ServletName</servlet-name> <url-pattern>/Demo</url-pattern> </servlet-mapping> Servlet code public void […]

Read more How to pass parameters to a servlet – ServletConfig

Here’s a servlet code example to download a text file from a website. For example Let’s say a text file named “testing.txt” , and you want to let user download it with a URL , for example “http://localhost:8080/servlet/DownloadDemo” . 1. Create a text file named “testing.txt” , put it into the project root folder. \–servlet […]

Read more Servlet code to download text file from website – Java

The HTTP status 405 is a very common error message in Java Servlet. HTTP Status 405 – HTTP method GET is not supported by this URL type Status report message HTTP method GET is not supported by this URL description The specified HTTP method is not allowed for the requested resource (HTTP method GET is […]

Read more HTTP Status 405 – HTTP method GET is not supported by this URL

Talking about the web technology, Java developers will keep talking about how powerful the Spring , Struts, Wicket, JSF…..When talking about the deployment, they will say using Ant script or Maven to build or deploy. Ironically, without the IDE or technology help, many Java developers do not know either how to create a simple servlet […]

Read more A Simple Servlet Example – (write, deploy, run)