1. 程式人生 > >ip統計訪問瀏覽器次數

ip統計訪問瀏覽器次數

package com.neuedu.sevrlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import 
java.util.HashMap; import java.util.HashSet; import java.util.Map; import java.util.Set; /** * Created by ttc on 16-11-29. */ @WebServlet(name = "httplServlet",urlPatterns = "/httpl") public class httplServlet extends HttpServlet { Set<String> set=new HashSet<String>(); protected void
doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } private void fillmap(String add,String browsertype){ String str=add+","+browsertype; set.add(str); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException { response.setContentType("text/html;charset=utf-8"); PrintWriter pw=response.getWriter(); String add=request.getRemoteAddr(); String agent=request.getHeader("user-agert"); String browsernow=""; if(agent.contains("Firefox")){ browsernow="Firefox"; fillmap("Firefox",add); } else if(agent.contains("Chrome")){ browsernow="Chrome"; fillmap("Chrome",add); } else if(agent.contains("Trident")){ browsernow="IE"; fillmap("IE",add); } else{ fillmap("other",add); browsernow = "other"; } Map<String,Integer> map=new HashMap<String,Integer>(); for(String s:set){ String []a=s.split(","); if(!map.containsKey(a[1])){ map.put(a[1],1); } else { Integer i=map.get(a[1]); i++; map.put(a[1],i); } } for(Map.Entry<String,Integer> e:map.entrySet()){ pw.print(e.getKey()+":"+e.getValue()); pw.print("您現在使用的是"+browsernow+"瀏覽器"); } } }