论文检索功能
find.jsp文件代码:
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <!DOCTYPE html> 4 <html> 5 <head> 6 <meta charset="ISO-8859-1"> 7 <title>论文查询</title> 8 <link rel="stylesheet" href="css/style.css"> 9 <link rel="stylesheet" href="./css/index.css"> 10 <link rel="stylesheet" href="./css/iconfont.css"> 11 12 <link rel="stylesheet" href="layui/css/layui.css"> 13 14 <script src="./lib/layui/layui.js" charset="utf-8"></script> 15 <script type="text/javascript" src="./js/index.js"></script> 16 <script src="./js/jquery.js"></script> 17 <script src="./js/jquery.min.js"></script> 18 <script src="./js/survey.js"></script> 19 20 </head> 21 <style> 22 input[type=text] { 23 width: 100%; 24 padding: 16px 20px; 25 margin: 8px 0; 26 display: inline-block; 27 border: 1px solid #ccc; 28 border-radius: 4px; 29 box-sizing: border-box; 30 } 31 select{ 32 width: 100%; 33 padding: 16px 20px; 34 border: none; 35 border-radius: 4px; 36 background-color: #66ccff; 37 38 } 39 input[type=submit] { 40 width: 100%; 41 background-color: #66ccff; 42 color: white; 43 padding: 16px 20px; 44 margin: 8px 0; 45 border: none; 46 border-radius: 4px; 47 cursor: pointer; 48 } 49 50 input[type=submit]:hover { 51 background-color: #45a049; 52 } 53 input[type=button], input[type=reset] { 54 background-color: #66ccff; 55 border: none; 56 color: white; 57 padding: 16px 32px; 58 text-decoration: none; 59 margin: 4px 2px; 60 cursor: pointer; 61 } 62 </style> 63 <script type="text/javascript" src="js/jquery.min.js"></script> 64 <script type="text/javascript"> 65 var detail_div = 1; 66 var realtotal=1; 67 function add_div() { 68 var str="<table align='center'> <tr><td><select name='lianjie"+detail_div+"'> <option value='AND'>AND</option> <option value='OR'>OR</option> <option value='NOT'>NOT</option>"; 69 str=str+"</select></td><td><select name='tiaojian"+detail_div+"'><option value='题目'>题目</option><option value='摘要'>摘要</option><option value='关键词'>关键词</option><option value='作者'>作者</option></select></td><td><input type='text' name='input"+detail_div+"'></td><td><select name='jingdu"+detail_div+"'><option value='精准'>精准</option><option value='模糊'>模糊</option><td><input type='button' id='addbtn"+detail_div+"' value='+' onclick='add_div()'></td>"; 70 str=str+"<td><input type='button' id='delebtn"+detail_div+"' value='-' onclick='dele_div(this)'></td></tr></table>"; 71 var div = document.createElement("div"); 72 //div.className = "form-group"; 73 div.id = "lotdiv" + detail_div; 74 div.innerHTML = str; 75 document.getElementById("biaodan").appendChild(div); 76 detail_div++; 77 realtotal++; 78 document.getElementById("sum").value=detail_div; 79 document.getElementById("realsum").value=realtotal; 80 } 81 function dele_div(obj){ 82 var id=obj.id; 83 realtotal--; 84 id=id.substring(7); 85 var e = document.getElementById("lotdiv"+id); 86 document.getElementById("biaodan").removeChild(e); 87 document.getElementById("sum").value=detail_div; 88 document.getElementById("realsum").value=realtotal; 89 } 90 var findnummm=0; 91 var resnum=0; 92 var alldata; 93 function sendAjax(){ 94 var url = "servlet?method=findlunwen&randnum=" + Math.random(); 95 // var name = "nb"; 96 // alert("name:"+name+", url:"+url); 97 //var data = { 98 99 // }; 100 $.ajax({ 101 type: "get", 102 url: url, 103 data: $('#biaodan').serialize(), 104 dataType: "json", 105 success: function(result){ 106 //alert("success--result:"+result[0].title); 107 alldata=result; 108 reslist(result); 109 if(findnummm==0){ 110 addresfinddiv(); 111 findnummm=1; 112 } 113 else changeresnum(); 114 }, 115 error: function(){ 116 alert("error"); 117 } 118 119 }); 120 } 121 function reslist(data){ 122 var tab=document.getElementById("restable"); 123 var str="<tr><td width='35%'>标题</td><td width='35%'>作者</td><td width='15%'>时间</td><td width='15%'>链接</td></tr>"; 124 for(i in data){ 125 str=str+"<tr><td>"+data[i].title+"</td><td>"+data[i].zuozhe+"</td><td>"+data[i].time+"</td><td><a target='_blank' href='"+data[i].lianjie+"'>"+"点此阅读文章"+"</a></td></tr>"; 126 resnum=resnum+1; 127 } 128 tab.innerHTML = str; 129 } 130 function addresfinddiv(){ 131 var formdiv=document.getElementById("tjdiv"); 132 var str="<select id='resselect' onchange='changedata()'><option value='选择年份'>选择年份</option><option value='近三年'>近三年</option><option value='近两年'>近两年</option><option value='去年'>去年</option></select>"; 133 str=str+"<h2 align='center' id='totalnumh'>一共"+resnum+"条</h2>" 134 var div = document.createElement("div"); 135 div.id="changeresdiv"; 136 div.innerHTML = str; 137 formdiv.appendChild(div); 138 resnum=0; 139 } 140 function changeresnum(){ 141 var formdiv=document.getElementById("tjdiv"); 142 //document.getElementById("totalnumh").remove(); 143 document.getElementById("changeresdiv").remove(); 144 var str="<select id='resselect' onchange='changedata()'><option value='选择年份'>选择年份</option><option value='近三年'>近三年</option><option value='近两年'>近两年</option><option value='去年'>去年</option></select>"; 145 str=str+"<h2 align='center' id='totalnumh'>一共"+resnum+"条</h2>" 146 var div = document.createElement("div"); 147 div.id="changeresdiv"; 148 div.innerHTML = str; 149 formdiv.appendChild(div); 150 resnum=0; 151 } 152 function changedata(){ 153 var chdanum=0; 154 var vl=document.getElementById("resselect"); 155 var index = vl.selectedIndex; 156 var chva=vl.options[index].value; 157 document.getElementById("restable").remove(); 158 var str="<table id='restable'>"+"<tr><td width='35%'>标题</td><td width='35%'>作者</td><td width='15%'>时间</td><td width='15%'>链接</td></tr>"; 159 if(chva=="近两年"){ 160 for(i in alldata){ 161 if(alldata[i].time.substring(0,4)!="2018"){ 162 chdanum=chdanum+1; 163 str=str+"<tr><td>"+alldata[i].title+"</td><td>"+alldata[i].zuozhe+"</td><td>"+alldata[i].time+"</td><td><a target='_blank' href='"+alldata[i].lianjie+"'>"+"点此阅读文章"+"</a></td></tr>"; 164 } 165 } 166 } 167 else if(chva=="去年"){ 168 for(i in alldata){ 169 if(alldata[i].time.substring(0,4)=="2020"){ 170 chdanum=chdanum+1; 171 str=str+"<tr><td>"+alldata[i].title+"</td><td>"+alldata[i].zuozhe+"</td><td>"+alldata[i].time+"</td><td><a target='_blank' href='"+alldata[i].lianjie+"'>"+"点此阅读文章"+"</a></td></tr>"; 172 } 173 } 174 } 175 else if(chva=="近三年"){ 176 for(i in alldata){ 177 chdanum=chdanum+1; 178 str=str+"<tr><td>"+alldata[i].title+"</td><td>"+alldata[i].zuozhe+"</td><td>"+alldata[i].time+"</td><td><a target='_blank' href='"+alldata[i].lianjie+"'>"+"点此阅读文章"+"</a></td></tr>"; 179 } 180 181 } 182 var div=document.getElementById("reslunwendiv"); 183 div.innerHTML = str+"</table>"; 184 var formdiv=document.getElementById("tjdiv"); 185 document.getElementById("changeresdiv").remove(); 186 var str="<select id='resselect' onchange='changedata()'><option value='选择年份'>选择年份</option><option value='近三年'>近三年</option><option value='近两年'>近两年</option><option value='去年'>去年</option></select>"; 187 str=str+"<h2 align='center' id='totalnumh'>一共"+chdanum+"条</h2>" 188 var div1 = document.createElement("div"); 189 div1.id="changeresdiv"; 190 div1.innerHTML = str; 191 formdiv.appendChild(div1); 192 } 193 function getTime(){ 194 var myDate = new Date(); 195 var myYear = myDate.getFullYear(); //获取完整的年份(4位,1970-????) 196 var myMonth = myDate.getMonth()+1; //获取当前月份(0-11,0代表1月) 197 var myToday = myDate.getDate(); //获取当前日(1-31) 198 var myDay = myDate.getDay(); //获取当前星期X(0-6,0代表星期天) 199 var myHour = myDate.getHours(); //获取当前小时数(0-23) 200 var myMinute = myDate.getMinutes(); //获取当前分钟数(0-59) 201 var mySecond = myDate.getSeconds(); //获取当前秒数(0-59) 202 var week = ['星期日','星期一','星期二','星期三','星期四','星期五','星期六']; 203 var nowTime; 204 205 nowTime = myYear+'-'+fillZero(myMonth)+'-'+fillZero(myToday)+' '+fillZero(myHour)+':'+fillZero(myMinute)+':'+fillZero(mySecond)+' '+week[myDay]+' '; 206 //console.log(nowTime); 207 $('#time').html(nowTime); 208 }; 209 function fillZero(str){ 210 var realNum; 211 if(str<10){ 212 realNum = '0'+str; 213 }else{ 214 realNum = str; 215 } 216 return realNum; 217 } 218 setInterval(getTime,1000); 219 </script> 220 <body> 221 <div class="layui-col-md12"> 222 <div class="layui-card"> 223 <div class="layui-card-body "> 224 <blockquote class="layui-elem-quote">欢迎: 225 <span class="x-red" >admin</span> 226 <span id="time"></span> 227 </blockquote> 228 </div> 229 </div> 230 </div> 231 <center> 232 <form action="servlet?method=findlunwen" method="post" id="biaodan" > 233 <div align="center" id="firdiv" > 234 <table align="center"> 235 <tr> 236 <td><select name="tiaojian" id="tiaojian"> 237 <option value="题目">题目</option> 238 <option value="摘要">摘要</option> 239 <option value="关键词">关键词</option> 240 <option value="作者">作者</option> 241 </select></td> 242 <td><input type="text" id="firinput" name="firinput"></td> 243 <td><select name="jingzhun" id="jingzhun"><option value="精准">精准</option><option value="模糊">模糊</option></select> 244 <td><input type="button" id="firbut" value="+" onclick="add_div()"></td> 245 </tr> 246 </table> 247 </div> 248 <input type="hidden" name="sum" id="sum" value="1"> 249 <input type="hidden" name="realsum" id="realsum" value="1"> 250 </form> 251 <form method="post" id="tjdiv"> 252 <div align="center"> 253 <input type="button" value="检索" onclick="sendAjax()" class="findbtn"> 254 <input type="reset" value="重置" class="findbtn"></div> 255 256 </form> 257 <div align="center" id="reslunwendiv" class="subbox"> 258 <table id="restable" > 259 260 </table> 261 </center> 262 </div> 263 </body> 264 </html>
在src文件夹下分别由Bean层、dao层、servlet层、DButil层对数据进行操作。
servlet.java文件:
1 package classes; 2 3 import java.io.IOException; 4 import java.io.PrintWriter; 5 import java.sql.SQLException; 6 import java.util.ArrayList; 7 import java.util.List; 8 import java.util.Map; 9 10 import javax.servlet.ServletException; 11 import javax.servlet.annotation.WebServlet; 12 import javax.servlet.http.HttpServlet; 13 import javax.servlet.http.HttpServletRequest; 14 import javax.servlet.http.HttpServletResponse; 15 16 import net.sf.json.JSONArray; 17 import net.sf.json.JSONObject; 18 19 /** 20 * Servlet implementation class servlet 21 */ 22 @WebServlet("/servlet") 23 public class servlet extends HttpServlet { 24 private static final long serialVersionUID = 1L; 25 Dao dao=new Dao(); 26 27 /** 28 * @see HttpServlet#HttpServlet() 29 */ 30 public servlet() { 31 super(); 32 // TODO Auto-generated constructor stub 33 } 34 public void findlunwen(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException { 35 request.setCharacterEncoding("utf-8"); 36 int tonum=Integer.parseInt(request.getParameter("sum")); 37 int realnum=Integer.parseInt(request.getParameter("realsum")); 38 List<demBean> findlist=new ArrayList<>(); 39 demBean dbean=null; 40 dbean=new demBean("AND",request.getParameter("tiaojian"),request.getParameter("firinput"),request.getParameter("jingzhun")); 41 findlist.add(dbean); 42 for(int i=1;i<tonum;i++) { 43 if(request.getParameter("input"+i)!=""&&request.getParameter("input"+i)!=null) { 44 dbean=new demBean(request.getParameter("lianjie"+i),request.getParameter("tiaojian"+i),request.getParameter("input"+i),request.getParameter("jingdu"+i)); 45 findlist.add(dbean); 46 } 47 } 48 49 System.out.println(realnum+" Ϊ"+findlist.size()); 50 for(int i=0;i<findlist.size();i++) { 51 System.out.println(findlist.get(i).toString()); 52 } 53 PrintWriter out = response.getWriter(); 54 //String name = request.getParameter("name").trim(); 55 List<lunwenBean> list = dao.getselectlunwen(findlist); 56 JSONArray json=new JSONArray(); 57 for(int i=0;i<list.size();i++) { 58 JSONObject ob=new JSONObject(); 59 ob.put("title", list.get(i).getTitle()); 60 ob.put("zuozhe", list.get(i).getZuozhe()); 61 ob.put("zhaiyao", list.get(i).getZhaiyao()); 62 ob.put("time", list.get(i).getTime()); 63 ob.put("lianjie", list.get(i).getLianjie()); 64 json.add(ob); 65 } 66 //System.out.println("JSON"+json.toString()); 67 System.out.println("zongshu"+list.size()); 68 out.write(json.toString()); 69 70 } 71 public void reci(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException { 72 request.setCharacterEncoding("utf-8"); 73 int neednum=Integer.parseInt(request.getParameter("neednum")); 74 Map<String, Integer>sortMap=Dao.getrc(); 75 JSONArray json =new JSONArray(); 76 int k=0; 77 for (Map.Entry<String, Integer> entry : sortMap.entrySet()) 78 { 79 JSONObject ob=new JSONObject(); 80 ob.put("name", entry.getKey()); 81 ob.put("value", entry.getValue()); 82 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"))) 83 { 84 json.add(ob); 85 k++; 86 } 87 if(k==neednum) 88 break; 89 } 90 System.out.println(json.toString()); 91 92 response.getWriter().write(json.toString()); 93 } 94 public void aja(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException { 95 PrintWriter out = response.getWriter(); 96 String name = request.getParameter("name").trim(); 97 String type="-1"; 98 try { 99 response.setCharacterEncoding("UTF-8"); 100 response.setContentType("application/json; charset=utf-8"); 101 // out.println("Success---llll"); 102 out.append(type); 103 out.close(); 104 } catch (Exception e){ 105 e.printStackTrace(); 106 } 107 108 109 } 110 /** 111 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) 112 */ 113 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 114 // TODO Auto-generated method stub 115 request.setCharacterEncoding("utf-8"); 116 String method=request.getParameter("method"); 117 if(method.equals("findlunwen")) { 118 try { 119 findlunwen(request,response); 120 } catch (ServletException e) { 121 e.printStackTrace(); 122 } catch (IOException e) { 123 e.printStackTrace(); 124 } catch (SQLException e) { 125 e.printStackTrace(); 126 } 127 128 } 129 else if(method.equals("aja")) { 130 try { 131 aja(request,response); 132 } catch (ServletException e) { 133 e.printStackTrace(); 134 } catch (IOException e) { 135 e.printStackTrace(); 136 } catch (SQLException e) { 137 e.printStackTrace(); 138 } 139 } 140 else if(method.equals("reci")) { 141 try { 142 reci(request,response); 143 } catch (ServletException e) { 144 e.printStackTrace(); 145 } catch (IOException e) { 146 e.printStackTrace(); 147 } catch (SQLException e) { 148 e.printStackTrace(); 149 } 150 } 151 152 } 153 154 /** 155 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) 156 */ 157 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 158 // TODO Auto-generated method stub 159 doGet(request, response); 160 } 161 162 }
dao.java重要代码:
1 public List<lunwenBean> getsize() throws SQLException{ 2 List<lunwenBean> list=new ArrayList<>(); 3 lunwenBean bean=null; 4 String sql="select * from xiangxi limit 2"; 5 Connection conn =Dbutil.getConnection(); 6 Statement st=null; 7 ResultSet rs=null; 8 try { 9 st=conn.createStatement(); 10 st.executeQuery(sql); 11 rs=st.executeQuery(sql); 12 while(rs.next()) { 13 String title=rs.getString("title"); 14 String zhaiyao=rs.getString("abstract"); 15 String zuozhe=rs.getString("zuozhe"); 16 String lianjie=rs.getString("link"); 17 String time=rs.getString("time"); 18 bean=new lunwenBean(title,zhaiyao,zuozhe,lianjie,time); 19 list.add(bean); 20 21 } 22 } catch (SQLException e) { 23 e.printStackTrace(); 24 } 25 finally{ 26 Dbutil.close(st, conn); 27 } 28 return list; 29 } 30 public List<lunwenBean> getselectlunwen(List<demBean> dl) throws SQLException{ 31 List<lunwenBean> list=new ArrayList<>(); 32 lunwenBean bean=null; 33 String sql="select * from xiangxi where "; 34 for(int i=0;i<dl.size();i++) { 35 if(dl.get(i).getXuanze().equals("题目")) { 36 dl.get(i).setXuanze("title"); 37 } 38 else if(dl.get(i).getXuanze().equals("摘要")) { 39 dl.get(i).setXuanze("abstract"); 40 } 41 else if(dl.get(i).getXuanze().equals("关键字")) { 42 dl.get(i).setXuanze("title"); 43 } 44 else if(dl.get(i).getXuanze().equals("作者")) { 45 dl.get(i).setXuanze("zuozhe"); 46 } 47 } 48 for(int i=0;i<dl.size();i++) { 49 if(i==0) { 50 sql=sql+" "+dl.get(i).getXuanze()+" "; 51 } 52 if(dl.get(i).getAndorno().endsWith("NOT")) { 53 if(dl.get(i).getJingmo().equals("精准")) { 54 sql=sql+"and "+dl.get(i).getXuanze()+" "+"!='"+dl.get(i).getNeirong()+"' "; 55 } 56 else if(dl.get(i).getJingmo().equals("模糊")) { 57 sql=sql+"and "+dl.get(i).getXuanze()+" "+"not like '%"+dl.get(i).getNeirong()+"%' "; 58 } 59 } 60 if(i!=0&&!dl.get(i).getAndorno().equals("NOT")) { 61 62 sql=sql+dl.get(i).getAndorno()+" "+dl.get(i).getXuanze()+" "; 63 } 64 if(!dl.get(i).getAndorno().equals("NOT")&&dl.get(i).getJingmo().equals("精准")) { 65 sql=sql+"='"+dl.get(i).getNeirong()+"' "; 66 } 67 else if(!dl.get(i).getAndorno().equals("NOT")&&dl.get(i).getJingmo().equals("模糊")) { 68 sql=sql+"like '%"+dl.get(i).getNeirong()+"%' "; 69 } 70 } 71 sql=sql+" order by time desc "; 72 System.out.println("SQL="+sql); 73 Connection conn =Dbutil.getConnection(); 74 Statement st=null; 75 ResultSet rs=null; 76 try { 77 st=conn.createStatement(); 78 st.executeQuery(sql); 79 rs=st.executeQuery(sql); 80 while(rs.next()) { 81 String title=rs.getString("title"); 82 String zhaiyao=rs.getString("abstract"); 83 String zuozhe=rs.getString("zuozhe"); 84 String lianjie=rs.getString("link"); 85 String time=rs.getString("time"); 86 bean=new lunwenBean(title,zhaiyao,zuozhe,lianjie,time); 87 list.add(bean); 88 89 } 90 } catch (SQLException e) { 91 e.printStackTrace(); 92 } 93 finally{ 94 Dbutil.close(st, conn); 95 } 96 return list; 97 98 }