package com.testproject.count;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("test")
public class BaseController {
@RequestMapping("tocount")
public String toCount(){
return "count/count";
}
@RequestMapping("count")
public Map<String, Object> count(HttpServletRequest req){
/*
* 在session中保存每次请求的ip与时间
* ip只在第一次请求时保存,date以list集合的形式保存于session中
* */
Date date = new Date();
List<Date> dateList = null;
if(req.getSession().getAttribute("ip")==null){
String ip = req.getRemoteAddr();
req.getSession().setAttribute("ip", ip);
dateList = new ArrayList<Date>();
dateList.add(date);
req.getSession().setAttribute("dates", dateList);
}else{
dateList = (List<Date>) req.getSession().getAttribute("dates");
dateList.add(date);
req.getSession().setAttribute("dates", dateList);
}
/*
* 对文本各数字计数并排序返回
* 返回的map集合中有两个list集合,一个为keyList,一个为valueList
* valueList按照数字数量升序排序,keyList与valueList一一对应,
* 保证在jsp页面中可以循环输出
* */
String text = req.getParameter("text");
//System.out.println(text);
Map<Character, Integer> map = new HashMap<Character, Integer>();
for(int i=0; i<text.length(); i++){
char c = text.charAt(i);
if(c>'0'&&c<'9'){
if(map.containsKey(c)){
map.put(c, map.get(c)+1);
}else{
map.put(c, 1);
}
}
}
List<Map.Entry<Character,Integer>> entry = new ArrayList<Map.Entry<Character,Integer>>(map.entrySet());
Collections.sort(entry, new Comparator<Map.Entry<Character, Integer>>() {
@Override
public int compare(java.util.Map.Entry<Character, Integer> o1,
java.util.Map.Entry<Character, Integer> o2) {
return o1.getValue()-o2.getValue();
}
});
//System.out.println(entry);
List<Character> charList = new ArrayList<Character>();
List<Integer> intList = new ArrayList<Integer>();
Iterator<java.util.Map.Entry<Character, Integer>> itc = entry.iterator();
while(itc.hasNext()){
Map.Entry<Character, Integer> e = itc.next();
charList.add(e.getKey());
intList.add(e.getValue());
}
//System.out.println(charList);
//System.out.println(intList);
Map<String, Object> mapp = new HashMap<String, Object>();
mapp.put("chars", charList);
mapp.put("nums", intList);
//System.out.println(mapp.get("chars"));
return mapp;
}
}
1、对页面提交的<textarea>中的数字进行计数,并以每个数字的数量进行排序
2、对每次会话中提交的请求做记录,包括ip、时间
3、通过异步方式实现请求提交
4、通过jquery对象操作来实现页面响应