package com.lunwen.bean; public class data { private String title; private String abs; private String link; public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getAbs() { return abs; } public void setAbs(String abs) { this.abs = abs; } public String getLink() { return link; } public void setLink(String link) { this.link = link; Data.java package com.lunwen.bean; public class lunwen { private String abs; private String title; public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getAbs() { return abs; } public void setAbs(String abs) { this.abs = abs; lunwen.java package com.lunwen.bean; public class word { private String name; private int value; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getValue() { return value; } public void setValue(int value) { this.value = value; }
word.java package com.lunwen.Dao; import java.sql.Connection; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import com.lunwen.bean.data; import com.lunwen.bean.lunwen; import com.mysql.jdbc.PreparedStatement; import com.mysql.jdbc.ResultSet; import com.mysql.jdbc.Statement; import com.lunwen.DBUtil.DBUtil; import com.lunwen.bean.data;; public class ShowDao { public List<lunwen> select(){ Connection conn = DBUtil.getConn(); List<lunwen> list = new ArrayList<lunwen>(); try { String sql="select * from lunwen"; Statement pstmt = (Statement) conn.createStatement(); ResultSet rs = (ResultSet) pstmt.executeQuery(sql); while(rs.next()) { lunwen lunwen=new lunwen(); lunwen.setTitle(rs.getString("title")); list.add(lunwen); } System.out.println("ShowDao Success!!"); rs.close(); pstmt.close(); conn.close(); }catch(SQLException e) { e.printStackTrace(); } return list; } public List<data> select1(String name){ Connection conn = DBUtil.getConn(); List<data> list = new ArrayList<data>(); try { String sql="select * from lunwen where title like '%"+name+"%'"; Statement pstmt = (Statement) conn.createStatement(); ResultSet rs = (ResultSet) pstmt.executeQuery(sql); while(rs.next()) { data data=new data(); data.setTitle(rs.getString("title")); data.setLink(rs.getString("link")); System.out.println(data.getTitle()+":"+data.getLink()); list.add(data); } System.out.println("ShowDao select1 Success!!"); rs.close(); pstmt.close(); conn.close(); }catch(SQLException e) { e.printStackTrace(); } return list; } }
DBUtils.java;连接数据库 package com.lunwen.DBUtil; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class DBUtil { public static String db_url = "jdbc:mysql://localhost:3306/lunwen?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true"; public static String db_user = "root"; public static String db_pass = "101032"; public static Connection getConn () { Connection conn = null; try { Class.forName("com.mysql.jdbc.Driver"); conn = DriverManager.getConnection(db_url, db_user, db_pass); } catch (Exception e) { e.printStackTrace(); } return conn; } public static void main(String[] args) { getConn(); } /** * @param state * @param conn */ public static void close (Statement state, Connection conn) { if (state != null) { try { state.close(); } catch (SQLException e) { e.printStackTrace(); } } if (conn != null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } public static void close (ResultSet rs, Statement state, Connection conn) { if (rs != null) { try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } } if (state != null) { try { state.close(); } catch (SQLException e) { e.printStackTrace(); } } if (conn != null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } }
showDao.java
DBUtils.java;连接数据库 package com.lunwen.DBUtil; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class DBUtil { public static String db_url = "jdbc:mysql://localhost:3306/lunwen?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true"; public static String db_user = "root"; public static String db_pass = "101032"; public static Connection getConn () { Connection conn = null; try { Class.forName("com.mysql.jdbc.Driver"); conn = DriverManager.getConnection(db_url, db_user, db_pass); } catch (Exception e) { e.printStackTrace(); } return conn; } public static void main(String[] args) { getConn(); } /** * @param state * @param conn */ public static void close (Statement state, Connection conn) { if (state != null) { try { state.close(); } catch (SQLException e) { e.printStackTrace(); } } if (conn != null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } public static void close (ResultSet rs, Statement state, Connection conn) { if (rs != null) { try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } } if (state != null) { try { state.close(); } catch (SQLException e) { e.printStackTrace(); } } if (conn != null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } } DBUtils.java package com.lunwen.Servlet; import java.io.IOException; import java.util.List; 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.lunwen.bean.data; import com.lunwen.Dao.*; /** * Servlet implementation class ClickServlet */ @WebServlet("/ClickServlet") public class ClickServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public ClickServlet() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8"); response.setContentType("text/html;charset=utf-8"); request.setCharacterEncoding("UTF-8"); String name=request.getParameter("name"); System.out.println(name+"123456"); ShowDao sd = new ShowDao(); List<data> list2 = sd.select1(name); request.setAttribute("list2", list2); request.getRequestDispatcher("ciyun.jsp").forward(request, response); } /** * @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); } }
ClickServlet.javapackage com.lunwen.Servlet;
import java.io.IOException; 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.lunwen.bean.*; import com.lunwen.Dao.*; import com.google.gson.Gson; import com.lunwen.DBUtil.*; import java.nio.file.NoSuchFileException; import java.util.*; import java.util.Map.Entry; import java.util.regex.Matcher; import java.util.regex.Pattern; import com.lunwen.bean.*; /** * Servlet implementation class ShowServlet */ @WebServlet("/ShowServlet") public class ShowServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public ShowServlet() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8"); response.setContentType("text/html;charset=utf-8"); ShowDao sd = new ShowDao(); List<lunwen> list = sd.select(); int n = 0; int[] array = new int[10000000]; String[] sarray = new String[10000000]; StringBuffer sb = new StringBuffer(); HashMap<String, Integer> has = new int r=0; for(lunwen l : list) { // System.out.println(l.getAbs()); n++; Pattern Matcher m=p.matcher(l.getTitle()); String first=m.replaceAll(" "); p=Pattern.compile(" {2,}"); m=p.matcher(first); String second=m.replaceAll(" "); // System.out.println(second); for(int i=0; i<sList.length; i++){ if(!has.containsKey(sList[i])){ has.put(sList[i], 1); }else{ has.put(sList[i], has.get(sList[i])+1); } } } for(Entry<String, Integer> entry:has.entrySet()){ sarray[r] = entry.getKey(); array[r] = entry.getValue(); r++; // System.out.println(entry.getKey()+":"+entry.getValue()); } int p; int[] y=new int[50]; int[] array1 = new int[10000000]; for(int c=0;c<array.length;c++) {array1[c]=array[c]; // System.out.println(array[c]+" "+sarray[c]); } for(int t=0;t<y.length;t++) { int max=0; for(p=0;p<array1.length;p++){ if(array1[p]>max){ max=array1[p]; y[t]=p; } array1[y[t]]=0; } } List<word> list1 = new ArrayList<word>(); for(int t=0;t<y.length;t++) { word word=new word(); word.setName(sarray[y[t]]); word.setValue(array[y[t]]); list1.add(word); // System.out.println(array[y[t]]+" "+sarray[y[t]]); } System.out.println(n); // request.setAttribute("list1", list1); Gson gson = new Gson(); String json = gson.toJson(list1); response.getWriter().write(json); // request.getRequestDispatcher("ciyun.jsp").forward(request, response); } /** * @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); } }
showServlet.java
page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>主界面</title> </head> <body> <div align="center"> <h2>管理界面</h2> <a href="ShowServlet">查看热词</a> </div> </body> </html> c.jsp @ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <%request.setCharacterEncoding("utf-8"); response.setCharacterEncoding("utf-8");%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta charset="utf-8"> <base href="<%=basePath%>"> <!--// echarts CDN--> <script src='https://cdn.bootcss.com/echarts/3.7.0/echarts.simple.js'></script> <!--// 下载wordcloud.js文件 // https://github.com/ecomfe/echarts-wordcloud--> <script src="${pageContext.request.contextPath}/plug-ins/js/echarts-wordcloud.js"></script> <script src="${pageContext.request.contextPath}/plug-ins/js/jquery-1.10.2.min.js"></script> </head> <body> <style> html, body{ 100%; height: 100%; margin: 0; } #main{ 600px; height: 500px; background: rgba(70, 120, 200, 0.2) } </style> <div id='main'></div> <script> var chart = echarts.init(document.getElementById('main')); var postURL = "/lunwen/ShowServlet"; var mydata = new Array(); $.ajaxSettings.async = false; $.post(postURL, {}, function(rs) { var dataList = JSON.parse(rs); for (var i = 0; i < dataList.length; i++) { var d = {}; d['name'] = dataList[i].name; d['value'] = dataList[i].value; mydata.push(d); } }); $.ajaxSettings.async = true; var option = { tooltip : {}, series : [ { type : 'wordCloud', gridSize : 2, sizeRange : [ 20, 50 ], rotationRange : [ -90, 90 ], shape : 'pentagon', width : 800, height : 600, drawOutOfBound : false, textStyle : { normal : { color : function() { return 'rgb(' + [ Math.round(Math.random() * 160), Math.round(Math.random() * 160), Math.round(Math.random() * 160) ] .join(',') + ')'; } }, emphasis : { shadowBlur : 10, shadowColor : '#333' } }, data : mydata } ] }; chart.setOption(option); chart.on('click', function(params) { var url = "ClickServlet?name=" + params.name; window.location.href = url; }); </script> <div> <table class="table table-hover"> <thead> <tr> <td style="font-size: 20px;">论文链接</td> </tr> </thead> <tbody> <c:forEach items="${list2}" var="data" varStatus="vs"> <tr> <td><a href="${data.link}">${data.title}</a></td> </tr> </c:forEach> </tbody> </table> </div> </body> </html>
ciyun.jsp