是的,我同意@krassib 和@TomR - 使用 queryString 参数,然后您可以编写一个 servlet 过滤器来检查特定参数并增加该附属机构的计数。使用 servlet 过滤器还可以为您提供额外的好处,即可以跟踪每个附属机构的单个链接的数量。
您执行以下操作:
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
public class AffiliateTrackingFilter implements Filter {
private AffiliateTrackingService affiliateTrackingService = null;
@Override
public void destroy() {
}
@Override
public void doFilter(ServletRequest rq, ServletResponse rs,
FilterChain chain) throws IOException, ServletException {
String affililateId = rq.getParameter("affiliateId");
affiliateTrackingService.incrementAffiliateHit(affililateId);
chain.doFilter();
}
@Override
public void init(FilterConfig fc) throws ServletException {
affiliateTrackingService = new AffiliateTrackingService();
}
}
然后将这样的内容添加到您的web.xml:
<filter>
<filter-name>AffiliateTrackingFilter</filter-name>
<filter-class>com.example.AffiliateTrackingFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>AffiliateTrackingFilter</filter-name>
<servlet-name>MyMainServlet</servlet-name>
</filter-mapping>
并为您的所有 servlet 提供过滤器映射。