In your website to do?
Abstract: they are in your website to do?
</ Td> </ tr> <tr> <td height="35" valign="top" class="ArticleTeitle"> read online: a "filter Servlet 2.3 Programming" original "Filter code with Servlet 2.3 model "from http://www.javawold.com/. Stresses inside the four examples, there are three, not very meaningful, not a close look at.
Below the filter sample tracking the request of users, who report to the network administrator at her site, as well as each user is visiting the page. The test result of the following:
<table Width="681" height="41" border="0"> <tr> <td width="393">
First, web.xml file
<? Xml version = "1.0" encoding = "ISO-8859-1">
Web-app
PUBLIC "- / / Sun Microsystems, Inc / / DTD Web Application 2.3 / / EN"
"Http://java.sun.com/j2ee/dtds/web-app_2_3.dtd">
</ Td> <td width="278"> </ td> </ tr> </ table>
This registered ClickstreamFilter and set its handling *. html *. jsp and to the request. This will also be registered as a ClickstreamLogger sniffers to in the application of the time of the incident accept them.
Second, both ServletContextListener is HttpSessionListener sniffers ClickstreamLogger category
Function TempSave (ElementID) (CommentsPersistDiv.setAttribute ( "CommentContent" document.getElementById (ElementID). Value); CommentsPersistDiv.save ( "CommentXMLStore");) function Restore (ElementID) (CommentsPersistDiv.load ( "CommentXMLStore"); document . getElementById (ElementID). CommentsPersistDiv.getAttribute value = ( "CommentContent");)
Package com.java3z.examples; import java.util .*; import javax.servlet .*; import javax.servlet.http .*; public class ClickstreamLogger implements ServletContextListener, HttpSessionListener (Map clickstreams = new HashMap (); public ClickstreamLogger () (/ / System.out.println ( "ClickstreamLogger constructed");) public void contextInitialized (ServletContextEvent sce) (/ / System.out.println ( "ServletContext Initialised"); sce.getServletContext (). setAttribute ( "clickstreams" clickstreams);) public void contextDestroyed (ServletContextEvent sce) (sce.getServletContext (). setAttribute ( "clickstreams", null); / / System.out.println ( "ServletContext Destroyed");) public void sessionCreated (HttpSessionEvent hse) ( / / System.out.println ( "Session Created"); hse.getSession HttpSession session = (); Clickstream clickstream = new Clickstream (); session.setAttribute ( "clickstream" clickstream); clickstreams.put (session.getId ( ), clickstream);) public void sessionDestroyed (HttpSessionEvent hse) (/ / System.out.println ( "Session Destroyed"); hse.getSession HttpSession session = (); Clickstream stream = (Clickstream) session.getAttribute ( "clickstream" ); clickstreams.remove (session.getId ()); System.out.println ( "Final session clickstream: \ n" + stream);)) ClickstreamLogger servlet environment in the creation of settings, the overall properties getServletContext.setAttribute ( "clickstreams" , clickstreams), a
Map, visit the Web site at the user attributes session.setAttribute conversational settings ( "clickstream" clickstream), and can record the conversation object clickstream data
Add to Map, in order to JSP pages of information from site visitors in output. Third, filter type ClickstreamFilter
Package com.java3z.examples;
Import java.io.IOException;
Import javax.servlet .*;
Import javax.servlet.http .*;
Public class ClickstreamFilter implements Filter (
Protected FilterConfig filterConfig;
Private final static String FILTER_APPLIED = "_clickstream_filter_applied";
Public void init (FilterConfig config) throws ServletException (
This.filterConfig = filterConfig;
)
Public void doFilter (ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException (
/ / Ensure that the filters in each request by the one-time use only
If (request.getAttribute (FILTER_APPLIED) == null) (
Request.setAttribute (FILTER_APPLIED, Boolean.TRUE);
HttpSession session = ((HttpServletRequest) request). GetSession ();
Clickstream stream = (Clickstream) session.getAttribute ( "clickstream");
Stream.addRequest (((HttpServletRequest) request));
)
/ / Transfer request
Chain.doFilter (request, response);
)
Public void destroy () ()
)
DoFilter () method the user's session, the gain Clickstream, and the current request data in the Add Clickstream. Which uses a special marker attributes FILTER_APPLIED tagging of this filter is whether the current request has been used (which is likely to occur in the request scheduling) and ignore all other acts of filters.
Fourth, the preservation of Clickstream data request
Package com.java3z.examples; import java.util .*; import java.io. *; import javax.servlet .*; import javax.servlet.http .*; public class Clickstream implements Serializable (List clickstream = new ArrayList () ; String hostname = null; HttpSession session = null; String initialReferrer = null; Date start = new Date (); Date lastRequest = new Date (); boolean bot = false; / / Robot visit is whether public Clickstream () () public void addRequest (HttpServletRequest request) (lastRequest = new Date (); if (hostname == null) (hostname = request.getRemoteHost (); request.getSession session = ();) / / if this is the first request in the click stream if (clickstream.size () == 0) (/ / setup initial referrer if (request.getHeader ( "REFERER")! = null) (initialReferrer = request.getHeader ( "REFERER");) else (initialReferrer = "";) / / decide whether this is a bot bot = BotChecker.isBot (request, this);) clickstream.add (request.getServerName () + (request.getServerPort ()! = 80 ":" + request. getServerPort (): "") + request.getRequestURI () + (request.getQueryString ()! = null? "?" + request.getQueryString (): "")); / / System.out.println (this.toString ());) public String getHostname () (return hostname;) public boolean isBot () (return bot;) public void setBot (boolean value) (this.bot = value;) public HttpSession getSession () (return session;) public String getInitialReferrer () (return initialReferrer;) public Date getStart () (return start;) public Date getLastRequest () (return lastRequest;) public List getStream () (return clickstream;) public String toString () (= new StringBuffer output StringBuffer (); output.append ( "Clickstream for:" hostname + + "\ n"); output.append ( "Session ID:" + (session! = null? session.getId () + "": "") + "\ n"); output.append (the "Initial Referrer:" initialReferrer + + "\ n"); output.append ( "Stream started:" + start + "\ n"); output.append ( "Last request : "lastRequest + +" \ n "); long streamLength = lastRequest.getTime () - start.getTime (); output.append (" Stream length: "+ (streamLength> 3600000?" "+ (streamLength / 3600000) + "hours": "") + (streamLength> 60000? "" + ((streamLength / 60000)% 60) + "minutes": "") + (streamLength> 1000? "" + ((streamLength / 1000) 60% ) + "seconds": "") + "\ n"); Iterator clickstreamIt = clickstream.iterator (); int count = 0; while (clickstreamIt.hasNext ()) (count + +; output.append (count + ":" clickstreamIt.next + () + "\ n");) return output.toString ();))
5, a robot?
BotChecker categories used to determine whether the client is a robot.
Package com.java3z.examples; import java.util .*; import java.io. *; import javax.servlet .*; import javax.servlet.http .*; public class BotChecker (public static String [] = (botHosts " inktomi.com "," inktomisearch.com "," googlebot.com "," linuxtoday.com.au "); public static boolean isBot (HttpServletRequest request, Clickstream stream) (String requestURI = request.getRequestURI () / / if it requested robots.txt, it's a bot if (requestURI.indexOf ( "robots.txt")> = 0) (return true;) / / it requested a RSS feed from our backend, it's a bot if (requestURI.indexOf ( "/ backend /")> = 0) (return true;) for (int i = 0; i <botHosts.length; i + +) (if (request.getRemoteHost (). indexOf (botHosts [i])> = 0) (return true;)) return false;)) 6. test: Please download this WEB Application testing of all documents.
</ Td> </ tr> <tr>
↑ Back






