前言
前段时间做项目时,涉及到调用第三方IP查询接口用来统计网站访问,以及访问地址。经过各方百度后,最后总结出一个可以实现的方法。项目背景:spring+springMvc+mybatis,使用的前后端分离架构
一、实现思路?
之前一直以为当用户访问项目时,系统就会为用户创建一个HttpSession对象,其实不然,存在误区。
实际上,经过百度之后和经过大佬的解答,HttpSession对象只有在主动调用request.getSession(true)或request.getSession()方法时,tomcat才会为其创建session。
本文是在一整个项目下都没有涉及到session的使用下使用。使用到springmvc的拦截器和tomcat的监听器
二、使用步骤
1.maven下的SSM项目或springMVC项目
2.拦截器实现
代码如下(示例):
package com.lhy21.interceptor;
import org.springframework.web.servlet.HandlerInterceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
/**
* @Author xpdxz
* @ClassName MyInterceptor
* @Description TODO
* @Date 2021/5/21 20:47
* @Version 1.0
*/
public class MyInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
HttpSession session = request.getSession(false);
if (session == null) {
session = request.getSession();
System.out.println("session:" + session + "创建成功");
session.setAttribute("ip", request.getRemoteAddr());
}
return true;
}
}
上述代码保证,同一用户在session有效期范围内访问不会再触发setAttribute方法,因为此项目只需统计访问量。具体查看if语句
当然,拦截器需要在spring-mvc的配置文件配置拦截路径,由于纯前后分离,配置了静态资源Handle也就是<mvc:resources />。因此,用户所有访问都会经过DispatcherServlet,用户需配置<mvc:interceptor />。例如:我需要实现用户一访问网站就统计,则:
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/*.html"/>
<mvc:mapping path="/html/*.html"/>
<bean class="com.lhy21.interceptor.MyInterceptor"/>
</mvc:interceptor>
</mvc:interceptors>
3.监听器实现
本方法实现统计的核心为session的设值监听,即session.setAttribute(),因此实现HttpSessionAttributeListener接口,我们只需实现attributeAdded方法即可。
package com.lhy21.listener;
import com.lhy21.pojo.Address;
import com.lhy21.service.AddressService;
import com.lhy21.util.AddressUtil;
import lombok.extern.log4j.Log4j2;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.WebApplicationContext;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;
import java.sql.Timestamp;
/**
* @Author xpdxz
* @ClassName VisitedListener
* @Description TODO
* @Date 2021/5/21 20:51
* @Version 1.0
*/
@Log4j2
public class VisitedListener implements HttpSessionAttributeListener {
@Override
public void attributeAdded(HttpSessionBindingEvent event) {
HttpSession session = event.getSession();
ApplicationContext applicationContext = (ApplicationContext) session.getServletContext().getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
AddressService addressService = applicationContext.getBean(AddressService.class);
String ip = (String) session.getAttribute("ip");
log.info(ip);
try {
Address address = AddressUtil.getAddress(ip);
if (address != null) {
address.setTime(new Timestamp(System.currentTimeMillis()));
log.info(address);
addressService.insertAddress(address);
}
} catch (Exception e) {
log.error(e.getMessage());
e.printStackTrace();
}
}
}
最后在web.xml文件中注册监听器即可
总结
上述只是提供一个思路以及一个简单的例子,其他功能,可根据此思路拓展开发