CookieCounter cookie, .
/* # 4: cookie cookie: CookieCounter.java */
package chapt16;
import java.io.IOException;
import java.io.Writer;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class CookieCounter {
/* ,
cookie ( 30 ) */
public static final int MAX_AGE_COOKIE = 3600 * 24 * 30;
public static void printToBrowser(
HttpServletResponse response, HttpServletRequest request) {
try {
Writer out = response.getWriter();
out.write("My Cookie counter: ");
/* */
out.write(String.valueOf(prepareCookieCounter(
request,response)));
out.flush();
out.close();
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException("Failed: " + e);
}
}
// ookie
private static int prepareCookieCounter(
HttpServletRequest request, HttpServletResponse response) {
Cookie[] cookies = request.getCookies();
Cookie counterCookie;
if (cookies!= null) {
for (int i = 0; i < cookies.length; i++) {
if ("counter".equals(cookies[i].getName())) {
String counterStr = cookies[i].getValue();
int counterValue;
try {
counterValue = Integer.parseInt(counterStr);
} catch (NumberFormatException e) {
counterValue = 0;
}
counterValue++;
counterCookie = new Cookie("counter",
String.valueOf(counterValue));
counterCookie.setMaxAge(MAX_AGE_COOKIE);
response.addCookie(counterCookie);
return counterValue;
} //end if
} //end for
} //end if
counterCookie = new Cookie("counter", "1");
counterCookie.setMaxAge(MAX_AGE_COOKIE);
response.addCookie(counterCookie);
return 1;
}
}
cookie :
Counter
localhost/FirstProject/
*
:
My session counter: 1
, , , , Web-:
- javax.servlet.ServletContextListener / ;
- javax.servlet.http.HttpSessionListener / HTTP-;
- javax.servlet.ServletContextAttributeListener // ;
- javax.servlet.http.HttpSessionAttributeListener // HTTP-;
- javax.servlet.http.HttpSessionBindingListener / HTTP-;
- javax.servlet.http.HttpSessionActivationListener / HTTP-;
- javax.servlet.ServletRequestListener / ;
- javax.servlet.ServletRequestAttributeListener // .
ServletContextListener contextDestroyed(ServletContextEvent e) contextInitialized(ServletContextEvent e) |
HttpSessionListener sessionCreated(HttpSessionEvent e) sessionDestroyed(HttpSessionEvent e) |
ServletContextAttributeListener attributeAdded(ServletContextAttributeEvent e) attributeRemoved(ServletContextAttributeEvent e) attributeReplaced(ServletContextAttributeEvent e) |
HttpSessionAttributeListener attributeAdded(HttpSessionBindingEvent e) attributeRemoved(HttpSessionBindingEvent e) attributeReplaced(HttpSessionBindingEvent e) |
HttpSessionBindingListener valueBound(HttpSessionBindingEvent e) valueUnbound(HttpSessionBindingEvent e) |
HttpSessionActivationListener sessionWillPassivate(HttpSessionEvent e) sessionDidActivate(HttpSessionEvent e) |
ServletRequestListener requestDestroyed(ServletRequestEvent e) requestInitialized(ServletRequestEvent e) |
ServletRequestAttributeListener attributeAdded(ServletRequestAttributeEvent e) attributeRemoved(ServletRequestAttributeEvent e) attributeReplaced(ServletRequestAttributeEvent e) |
.
|
|
1 . URL, counter. , HttpSessionAttributeListener .
/* # 5: : MyAttributeListener.java */
package chapt21;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;
public class MyAttributeListener
implements HttpSessionAttributeListener {
private String counterAttr = "counter";
public void attributeAdded(HttpSessionBindingEvent ev) {
String currentAttributeName = ev.getName();
String urlAttr = "URL";
if (currentAttributeName.equals(counterAttr)) {
Integer currentValueInt = (Integer) ev.getValue();
System. out. println(" Session ="
+ currentValueInt);
} else if (currentAttributeName.equals(urlAttr)) {
StringBuffer currentValueStr = (StringBuffer)ev.getValue();
System. out. println(" Session URL="
+ currentValueStr);
} else System. out. println(" ");
}
public void attributeRemoved(HttpSessionBindingEvent ev) {
}
public void attributeReplaced(HttpSessionBindingEvent ev) {
String currentAttributeName = ev.getName();
// , ,
//
if (currentAttributeName.equals(counterAttr)) {
Integer currentValueInt = (Integer) ev.getValue();
System. out. println(" Session c="
|
|
+ currentValueInt);
}
}
}
, <web-app> web.xml.
<listener>
<listener-class>chapt21.MyAttributeListener </listener-class>
</listener>
SessionServlet :
Session URL= http://localhost:8080/FirstProject/SessionServlet
Session =1
Session c=1
Session c=2
Session c=3
ServletRequestListener Servlet API
2.4. .
/* # 6: : MyRequestListener.java */
package chapt21;
import javax.servlet.ServletContext;
import javax.servlet.ServletRequest;
import javax.servlet.ServletRequestEvent;
import javax.servlet.ServletRequestListener;
import javax.servlet.http.HttpServletRequest;
public class MyRequestListener
implements ServletRequestListener {
//
private static int reqCount;
public void requestInitialized(ServletRequestEvent e) {
// log-
ServletContext context = e.getServletContext();
//
ServletRequest req = e.getServletRequest();
synchronized (context) {
String name = "";
name = ((HttpServletRequest) req).getRequestURI();
// log-
context.log("Request for " + name
+ "; Count=" + ++reqCount);
}
}
public void requestDestroyed(ServletRequestEvent e) {
//
System. out. println("Request ");
}
}
SessionServlet, log-, /logs , :