Dao.java
package com.dao; import com.DBUtil.Dbutil; import com.bean.lunwenBean; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.*; import java.util.stream.Collectors; public class Dao { public List<lunwenBean> getsize() throws SQLException{ List<lunwenBean> list=new ArrayList<>(); lunwenBean bean=null; String sql="select * from cvpr limit 2"; Connection conn = Dbutil.getConnection(); Statement st=null; ResultSet rs=null; try { st=conn.createStatement(); st.executeQuery(sql); rs=st.executeQuery(sql); while(rs.next()) { String title = rs.getString("title"); String link = rs.getString("link"); String zuozhe = rs.getString("zuozhe"); String as = rs.getString("abstract"); String keywords = rs.getString("keywords"); String time = rs.getString("time"); bean=new lunwenBean(title,link,zuozhe,as,keywords,time); list.add(bean); } } catch (SQLException e) { e.printStackTrace(); } finally{ Dbutil.close(st, conn); } return list; } public List<lunwenBean> getselectlunwen(String a, String b, String c) throws SQLException{ List<lunwenBean> list=new ArrayList<>(); lunwenBean bean=null; String sql="select * from cvpr where abstract like '%"+a+"%' and title like '%"+b+"%' and zuozhe like '%"+c+"%'"; sql=sql+" order by time desc "; System.out.println("SQL="+sql); Connection conn = Dbutil.getConnection(); Statement st=null; ResultSet rs=null; try { st=conn.createStatement(); st.executeQuery(sql); rs=st.executeQuery(sql); while(rs.next()) { String title = rs.getString("title"); String link = rs.getString("link"); String zuozhe = rs.getString("zuozhe"); String as = rs.getString("abstract"); String keywords = rs.getString("keywords"); String time = rs.getString("time"); bean=new lunwenBean(title,link,zuozhe,as,keywords,time); list.add(bean); } } catch (SQLException e) { e.printStackTrace(); } finally{ Dbutil.close(st, conn); } return list; } public static Map<String,Integer> getrc() { int tnumi=0; String sql="select * from cvpr "; Map<String, Integer>map= new HashMap<String, Integer>(); Map<String, Integer>results= new LinkedHashMap<String, Integer>(); Connection conn = Dbutil.getConnection(); Statement st=null; ResultSet rs=null; try { st=conn.createStatement(); st.executeQuery(sql); rs=st.executeQuery(sql); while(rs.next()) { String keywords=rs.getString("title"); keywords=keywords.substring(1, keywords.length()); String[] split = keywords.split(" "); for(int i=0;i<split.length;i++) { if(map.get(split[i])==null) { map.put(split[i],1); } else { map.replace(split[i], map.get(split[i])+1); } } tnumi++; } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } // System.out.println("of的个数为"+map.get("of")+"MAP个数"+map.size()); Dbutil.close(rs, st, conn); map.entrySet() .stream() .sorted((p1, p2) -> p2.getValue().compareTo(p1.getValue())) .collect(Collectors.toList()) .forEach(ele -> results.put(ele.getKey(), ele.getValue())); return results; } }
servlet.java
package com.servlet; import java.io.IOException; import java.io.PrintWriter; import java.sql.SQLException; import java.util.List; import java.util.Map; 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 com.bean.lunwenBean; import com.dao.Dao; import net.sf.json.JSONArray; import net.sf.json.JSONObject; /** * Servlet implementation class servlet */ @WebServlet("/servlet") public class servlet extends HttpServlet { private static final long serialVersionUID = 1L; Dao dao=new Dao(); /** * @see HttpServlet#HttpServlet() */ public servlet() { super(); // TODO Auto-generated constructor stub } public void findlunwen(HttpServletRequest req, HttpServletResponse response) throws ServletException, IOException, SQLException { req.setCharacterEncoding("utf-8"); String as=req.getParameter("as"); String title=req.getParameter("title"); String zuozhe = req.getParameter("zuozhe"); List<lunwenBean> list = dao.getselectlunwen(as,title,zuozhe); PrintWriter out = response.getWriter(); JSONArray json=new JSONArray(); for(int i=0;i<list.size();i++) { JSONObject ob=new JSONObject(); ob.put("title", list.get(i).getTitle()); ob.put("link", list.get(i).getLink()); ob.put("zuozhe", list.get(i).getZuozhe()); ob.put("as", list.get(i).getAs()); ob.put("keywords",list.get(i).getKeywords()); ob.put("time", list.get(i).getTime()); json.add(ob); } //System.out.println("JSON"+json.toString()); System.out.println("zongshu"+list.size()); out.write(json.toString()); } public void reci(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException { request.setCharacterEncoding("utf-8"); int shuzi=Integer.parseInt(request.getParameter("shuzi")); Map<String, Integer>sortMap=Dao.getrc(); JSONArray json =new JSONArray(); int k=0; for (Map.Entry<String, Integer> entry : sortMap.entrySet()) { JSONObject ob=new JSONObject(); ob.put("name", entry.getKey()); ob.put("value", entry.getValue()); if(!(entry.getKey().equals("for")||entry.getKey().equals("and")||entry.getKey().equals("With")||entry.getKey().equals("of")||entry.getKey().equals("in")||entry.getKey().equals("From")||entry.getKey().equals("A")||entry.getKey().equals("to")||entry.getKey().equals("a")||entry.getKey().equals("the")||entry.getKey().equals("by"))) { json.add(ob); k++; } if(k==shuzi) break; } System.out.println(json.toString()); response.getWriter().write(json.toString()); } public void cld(HttpServletRequest req, HttpServletResponse response) throws ServletException, IOException, SQLException{ System.out.println("Rcservlet"); response.setContentType("text/html;charset=UTF-8"); req.setCharacterEncoding("UTF-8"); int shuzi=Integer.parseInt(req.getParameter("shuzi")); System.out.println(shuzi); Map<String, Integer>sortMap=Dao.getrc(); JSONArray json =new JSONArray(); int k=0; for (Map.Entry<String, Integer> entry : sortMap.entrySet()) { JSONObject ob=new JSONObject(); ob.put("name", entry.getKey()); ob.put("value", entry.getValue()); if(!(entry.getKey().equals("for")||entry.getKey().equals("and")||entry.getKey().equals("With")||entry.getKey().equals("of")||entry.getKey().equals("in")||entry.getKey().equals("From")||entry.getKey().equals("A")||entry.getKey().equals("to")||entry.getKey().equals("a")||entry.getKey().equals("the")||entry.getKey().equals("by"))) { json.add(ob); k++; } if(k==shuzi) break; } System.out.println(json.toString()); response.getWriter().write(json.toString()); } public void aja(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException { PrintWriter out = response.getWriter(); String name = request.getParameter("name").trim(); String type="-1"; try { response.setCharacterEncoding("UTF-8"); response.setContentType("application/json; charset=utf-8"); // out.println("Success---llll"); out.append(type); out.close(); } catch (Exception e){ e.printStackTrace(); } } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub request.setCharacterEncoding("utf-8"); String method=request.getParameter("method"); if(method.equals("findlunwen")) { try { findlunwen(request,response); } catch (ServletException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } } else if(method.equals("aja")) { try { aja(request,response); } catch (ServletException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } } else if(method.equals("cld")) { try { cld(request,response); } catch (ServletException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } } else if(method.equals("reci")) { try { reci(request,response); } catch (ServletException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } } } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }