package NameService;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class FindNameServlet extends HttpServlet {
private List<String> names = new ArrayList<String>();
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//System.out.println("this is findNameServlet");
String prefix = request.getParameter("names");
//System.out.println(prefix);
NameService service = NameService.getInstance(names);
List matching = service.findNames(prefix);
if(matching.size()>0){
PrintWriter out = response.getWriter();
response.setContentType("text/xml");
response.setHeader("Cache-Control", "no-cache");
response.setHeader("Charset", "UTF-8");
response.setCharacterEncoding("UTF-8");
out.println("<response>");
Iterator iter = matching.iterator();
while(iter.hasNext()){
String name = (String) iter.next();
// name = new String(name.getBytes("ISO-8859-1"),"UTF-8");
System.out.println(" ## 匹配查询结果 :"+name);
out.println("<name>"+name+"</name>");
}
out.println("</response>");
matching = null;
service = null;
out.close();
}else{
response.setStatus(HttpServletResponse.SC_NO_CONTENT);
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
this.doGet(request, response);
}
public void init() throws ServletException {
names.add("中国人民");
names.add("人民币a");
names.add("广东d");
names.add("广东佛山a");
names.add("浦北中学b");
names.add("浦北人b");
names.add("我的浦北世界a");
names.add("美丽的浦北广东a");
names.add("我的故而浦北中国c");
names.add("广西之家b");
names.add("你好,广西a");
names.add("是不是呢");
names.add("来吧,亲");
names.add("a看了");
names.add("abc是的");
names.add("abcdefghigklmnopqrstuvwxyz金正日同志永垂不朽!");
System.out.println("this is init");
System.out.println();
}
}
- 1
- 2
- 3
前往页