本帖最后由 CrazyProgram 于 2013-4-26 20:11 编辑
- package zhiwei.deng.web.listener;
- import java.util.HashMap;
- import java.util.Map;
- import javax.servlet.ServletRequestEvent;
- import javax.servlet.ServletRequestListener;
- import javax.servlet.http.HttpServletRequest;
- public class VisitorListener implements ServletRequestListener {
- private int num;
- private static Map<String,String> map = new HashMap<String,String>();
- static{
- map.put("127.0.0.1", "中国北京");
- }
- public void requestInitialized(ServletRequestEvent sre) {
- //锁住大家共享的东西
- synchronized(this){//这个this代表的是什么,我打印了这个类的hashcode还想不是他~给个答案谢谢
- num++;
- }
- HttpServletRequest request = (HttpServletRequest) sre.getServletRequest();
- //设置得到ip
- String ip = request.getRemoteAddr();
- request.setAttribute("ip", ip);
- //设置得到地址
- String address = map.get(ip);
- if(address==null){
- address = "中国深圳";
- }
- request.setAttribute("address", address);
- }
- public void requestDestroyed(ServletRequestEvent sre) {
- }
- }
复制代码 很久没想这个问题了,今天突然发现有点不对。。求解释
19行不理解的地方在那 |