閱讀器訪問地址:http://easyrss.tk/,歡迎體驗!
閱讀導覽
一、 概述
二、 設計的基本概念和原理
三、 設計方案
四、 主要源代碼
五、 閱讀器使用說明
概述
獲得信息是在人類的生活中是必不可少的環(huán)節(jié)。如果現(xiàn)在的社會對獲得信息不快捷,那么這個社會將不會像如今這般的發(fā)達和進步。在當今網(wǎng)絡技術相當發(fā)達的今天,大量的信息充斥在網(wǎng)上?,F(xiàn)在網(wǎng)絡越來越發(fā)達,用戶在網(wǎng)上既能工作也能娛樂。當用戶在網(wǎng)上需瀏覽很多個網(wǎng)站才能獲取自己多需的信息時,那就感覺很累。因為現(xiàn)在每個網(wǎng)站都有很多信息,找到自己所喜歡的并不是一件輕松的事。這時RSS閱讀器的功用就體現(xiàn)出來了。
RSS 是一種簡單的訂閱方式,它能能夠起到聚合網(wǎng)絡信息的作用。就像訂報紙、雜志一樣,您可以訂閱任何喜歡的內(nèi)容。網(wǎng)絡用戶可以在客戶端借助于支持RSS的新聞聚合軟件在不打開網(wǎng)站內(nèi)容頁面的情況下,將網(wǎng)站提供的支持RSS輸出的網(wǎng)站內(nèi)容(新聞、Blog等)聚集到RSS閱讀器中,用戶可以不必登錄各個提供信息的網(wǎng)站,而通過"RSS閱讀器"來閱讀這些內(nèi)容。
本設計要設計一個RSS閱讀器,本閱讀器將要實現(xiàn)的功能如下:
隨時隨地閱讀:
通過注冊與登錄將用戶信息保存到服務器,用戶在任何時間、任何地方打開網(wǎng)頁就可以閱讀自己的RSS訂閱。
RSS源的分組管理與結構化顯示:
簡潔直觀的分類顯示用戶的RSS訂閱列表,簡化操作、提高用戶的閱讀體驗。
RSS源的添加、修改與刪除:
用戶能夠方便的添加、修改、刪除RSS源及分組。
RSS源的的解析與顯示:
閱讀器可以通過用戶提供的RSS源URL解析出該RSS源的標題、包含的文章列表、文章地址和文章更新時間等信息,并且能正確地以一定格式顯示出來供用戶閱讀。
設計的基本概念和原理
RSS與RSS閱讀器概念
RSS:
RSS(Really Simple Syndication)是一種描述和同步網(wǎng)站內(nèi)容的格式,是使用最廣泛的XML應用【1】。RSS搭建了信息迅速傳播的一個技術平臺,使得每個人都成為潛在的信息提供者。發(fā)布一個RSS文件后,這個RSS Feed中包含的信息就能直接被其他站點調用,而且由于這些數(shù)據(jù)都是標準的XML格式,所以也能在其他的終端和服務中使用,是一種描述和同步網(wǎng)站內(nèi)容的格式。 RSS可以是以下三個解釋的其中一個: Really Simple Syndication;RDF (Resource Description Framework) Site Summary; Rich Site Summary【2】。但其實這三個解釋都是指同一種Syndication的技術。
RSS目前廣泛用于網(wǎng)上新聞頻道,blog和wiki,主要的版本有0.91, 1.0, 2.0。使用RSS訂閱能更快地獲取信息,網(wǎng)站提供RSS輸出,有利于讓用戶獲取網(wǎng)站內(nèi)容的最新更新。網(wǎng)絡用戶可以在客戶端借助于支持RSS的聚合工具軟件,在不打開網(wǎng)站內(nèi)容頁面的情況下閱讀支持RSS輸出的網(wǎng)站內(nèi)容。
RSS閱讀器:
RSS閱讀器基本可以分為三類【3】:
第一類大多數(shù)閱讀器是運行在計算機桌面上的應用程序,通過所訂閱網(wǎng)站的新聞供應,可自動、定時地更新新聞標題。在該類閱讀器中,有Awasu、FeedDemon和RSSReader這三款流行的閱讀器,都提供免費試用版和付費高級版。
第二類新聞閱讀器通常是內(nèi)嵌于已在計算機中運行的應用程序中。例如,NewsGator內(nèi)嵌在微軟的Outlook中,所訂閱的新聞標題位于Outlook的收件箱文件夾中。另外,Pluck內(nèi)嵌在Internet Explorer瀏覽器中!
第三類則是在線的WEB RSS閱讀器,其優(yōu)勢在于不需要安裝任何軟件就可以獲得RSS閱讀的便利,并且可以保存閱讀狀態(tài),推薦和收藏自己感興趣的文章。提供此服務的有兩類網(wǎng)站,一種是專門提供RSS閱讀器的網(wǎng)站,例如國外的feedly,國內(nèi)的有道、鮮果、抓蝦;另一種是提供個性化首頁的網(wǎng)站,例如國外的netvibes、pageflakes,國內(nèi)的雅蛙、闊地。
本閱讀器即是基于JSP的WEB RSS閱讀器。
閱讀器實現(xiàn)原理
本RSS閱讀器使用主要使用JSP技術,根據(jù)用戶請求的Url借助Rome和Jdom開源工具來解析XML文件獲取相應內(nèi)容,然后將獲取到的內(nèi)容以方便閱讀的格式顯示在網(wǎng)頁上【4】。同時用數(shù)據(jù)庫與JSP相應技術實現(xiàn)閱讀器功能的擴展,如:用戶閱讀列表的管理,顯示效果的優(yōu)化等。
設計方案
架構設計
網(wǎng)站前臺使用JSP技術實現(xiàn)頁面與功能,后臺數(shù)據(jù)庫使用SQLServer作數(shù)據(jù)管理,閱讀內(nèi)容從RSS源獲取,如下圖:
數(shù)據(jù)庫設計
數(shù)據(jù)庫使用SQL Server 2008 R2,數(shù)據(jù)表設計如下:
為保護系統(tǒng)安全,數(shù)據(jù)表結構不直接給出
界面設計
登錄與注冊:
登錄界面 login.jsp
注冊界面signup.jsp:
主頁
主頁index.jsp(中間的分割線可以拖動)
主頁采用框架設計,上部為網(wǎng)站信息和當前用戶信息;左邊為RSS源列表;右邊是主框架,用于顯示閱讀內(nèi)容。左右框架大小可以拖動隨意調整。
RSS樹形列表:
dtree
RSS源顯示采用dtree樹形列表【5】展示,能夠直觀展示分組與分組中的RRS源。
文章列表:
以表格形式列出選定RSS訂閱中的文章列表
文章內(nèi)容:
文章內(nèi)容
文章內(nèi)容是直接展示文章原網(wǎng)頁,保持原文排版,并防止被屏蔽。
添加與管理:
添加與管理都采用彈窗的方式,具體如下:
添加源
添加分組
管理分組
管理源
修改界面與添加界面相似,不再貼圖。
提示信息(tips.jsp):
大部分提示信息由tips頁面顯示,舉例如下:
注冊提示
未登錄提示
退出提示
……
提示頁面會根據(jù)獲取到的參數(shù)顯示提示信息
功能設計
注冊:
注冊時,用戶名、密碼以及確認密碼為必填項。由于密碼不可見,設置確認密碼可以防止輸入錯誤。昵稱為選填項,用以更加友好的顯示用戶名稱,若為空顯示時將以用戶名代替。注冊后將注冊信息添加到數(shù)據(jù)庫。同時應注意用戶名不能和數(shù)據(jù)庫中已有的用戶名重復。
登錄:
登錄時首先獲取用戶名,連接數(shù)據(jù)庫,查詢該用戶是否存在,若用戶名不存在,跳轉提示頁面顯示相應提示然后返回至登錄頁面。如果查找到,則再確認密碼是否輸入正確,密碼正確則將用戶uid存入session以備后面使用然后跳轉至該用戶主頁。
主頁:
主頁采用框架布局,主要包括三個部分:
上部:顯示網(wǎng)站logo,當前用戶信息等。用戶信息根據(jù)session中的uid從數(shù)據(jù)庫中查詢該用戶的詳細信息。若該用戶昵稱列表不為空則顯示昵稱,若為空則顯示用戶名。
左部:主要是RSS樹形列表,詳細見后文介紹;
右部:主要顯示區(qū)域,顯示文章列表及文章具體內(nèi)容,詳細見后文介紹。
RSS樹形列表:
RSS源顯示采用開源的dtree項目進行二次開發(fā)。首先根據(jù)之前放入session中的uid獲取當前用戶的RSS分組數(shù)據(jù),每獲取到一個分組就根據(jù)該分組的gid獲取屬于該組內(nèi)的的RSS源數(shù)據(jù),然后將數(shù)據(jù)放入dtree中,依次循環(huán)直至獲取到所有數(shù)據(jù)。最后由dtree根據(jù)獲取到的數(shù)據(jù)按照樹形樣式顯示出來。用戶點擊列表中的項目將會在右側主框架中打開相應的文章列表。
主框架的文章列表:
主框架從左框架中發(fā)來的鏈接中提取feed參數(shù)中的url值,解析該url獲取到相應文章列表。對url和xml文件的解析采用rss解析器(rome.jar和jdom.jar)進行解析獲取相應數(shù)據(jù)。
同時,為了解決部分文章不支持在框架中打開,文章列表設計了在新窗口打開的選項。主要實現(xiàn)方法是:用戶改變"在新窗口中打開"的選項的狀態(tài)后,js立即將該選項狀態(tài)寫入Cookie并發(fā)送刷新請求。服務器根據(jù)Cookies值動態(tài)修改文章鏈接的target屬性并向客戶端發(fā)送新的頁面。
文章內(nèi)容顯示:
文章內(nèi)容未做任何處理直接顯示原文,簡單方便。但是有些文章不支持在框架中顯示,這時需要勾選"在新窗口中打開"的選項,使文章在新開的瀏覽器窗口中顯示。
RSS源的添加與管理:
添加分為添加源與添加分組,兩個界面屬于同一個彈窗,通過頂部tab切換,直觀快捷。
添加源時需要提交:Feed地址、標題、分組。feed地址填寫需要訂閱的rss地址;標題可以從Feed地址中提?。ň唧w實現(xiàn)方式為服務器獲取到feed地址,根據(jù)feed地址解析出訂閱標題,然后向客戶端發(fā)送帶有訂閱標題的新頁面);分組通過列表框選擇用戶已有的分組。客戶端提交表單后,服務器獲取到相應的信息并添加至數(shù)據(jù)庫,然后返回成功信息;用戶可再次添加新的源。
添加分組時只需要提交需要添加的分組名稱即可。
管理界面屬于新的彈窗,初始顯示用戶的所有分組,每個分組包含"展開"、"修改"和"刪除"三個菜單。點擊分組名或"展開"菜單將會跳轉到該分組下的RSS源列表。RSS源列表與分組列表相識,每個分組包含"修改"與"刪除"兩個菜單。
修改時提交需要修改的項目,服務器根據(jù)獲取到的gid、pd以及修改后的信息更新對應項目數(shù)據(jù)并返回相應提示。
刪除時提交對應分組的gid或RSS源的pd,服務器根據(jù)獲取到的id信息輸出相應項目并返回提示。刪除非空分組時將會刪除該分組下所有RSS源(有提示)。
提示信息:
提示信息顯示頁面或根據(jù)獲取到的參數(shù)顯示相應的提示信息并在延遲特定時間后跳轉到相應界面,參數(shù)為空時顯示"未知錯誤"并跳轉至主頁。
網(wǎng)絡安全補充
為了保護網(wǎng)站與用戶數(shù)據(jù)安全,采取了一下輔助安全措施:
注冊輸入限制:
用戶名只能為字母與數(shù)字的組合
密碼長度太短
用戶名限制為字母與數(shù)字組合,防止用戶使用SQL語言中的符號進行SQL注入。密碼長度限制為8~20位。太短,密碼不安全;太長,用戶可以通過密碼框使用SQL注入攻擊
使用過濾器防止SQL注入:
只通過表單的輸入限制來防止SQL注入是遠遠不夠的,用戶依然可一使用URL參數(shù)的形式進行注入攻擊。所以我在原項目中加入了一個過濾器來防護一些簡單的SQL注入攻擊。
該過濾器的原理是,截取用戶的所有輸入,檢測是否是否包含特定關鍵詞,有則重定向到一個錯誤信息頁面error.jsp。沒有則通過過濾器。
當然只是用過濾器是不能完全防護SQL注入攻擊的,更有效的方式是項目中所有SQL語句都采用預編譯語句(PreparedStatement)接口來實現(xiàn)【6】。
面對日益復雜的網(wǎng)絡安全環(huán)境,以上的安全措施只是簡單的做了一些防護,對于一個實際項目是遠遠不夠的。在實際的項目還用采取更加嚴謹,更加有效的措施。
具體方法參考本文:JSP使用過濾器防止SQL注入
主要源代碼
視圖部分 /RSSreader/WebContent/rsscontent.jsp: %@page contentType="text/html"%> %@page pageEncoding="UTF-8" import="java.text.SimpleDateFormat"%> html> script language=javascript> function setCheck(){ var newWindow=document.getElementById("newWindow").value; if(newWindow==0) { document.getElementById("check").checked=true; } else { document.getElementById("check").checked=false; } } function check(){ var check=document.getElementById("check").checked; var feed=document.getElementById("feed").value; var url="rsscontent.jsp?feed="+feed; if(check) { document.cookie="newWindow=0"; } else { document.cookie="newWindow=1"; } self.location=url; } /script> body onload="setCheck()"> % String pageTitle=""; String urlStr = request.getParameter("feed"); String target=""; Cookie cookies[]=request.getCookies(); //讀出用戶硬盤上的Cookie,并將所有的Cookie放到一個cookie對象數(shù)組里面 Cookie sCookie=null; for(int i=0;icookies.length;i++){ //用一個循環(huán)語句遍歷剛才建立的Cookie對象數(shù)組 sCookie=cookies[i]; //取出數(shù)組中的一個Cookie對象 if(sCookie!=null){ if(("newWindow").equals(sCookie.getName())){ pageContext.setAttribute("newWindow",sCookie.getValue()); System.out.println(pageContext.getAttribute("newWindow")); } } } if(pageContext.getAttribute("newWindow")!=null) { if(pageContext.getAttribute("newWindow").equals("0")){ target="_blank"; } else{ target="_self"; } } try{ /* java.util.Properties systemSettings = System.getProperties(); systemSettings.put("http.proxyHost", "mywebcache.com"); systemSettings.put("http.proxyPort", "8080"); System.setProperties(systemSettings); */ if (!urlStr.startsWith("http://")) urlStr = "http://"+request.getParameter("feed"); //String urlStr = "http://feed.cnblogs.com/blog/u/249598/rss"; System.out.println(urlStr); java.net.URLConnection feedUrl = new java.net.URL(urlStr).openConnection(); feedUrl.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)"); com.sun.syndication.io.SyndFeedInput input = new com.sun.syndication.io.SyndFeedInput(); com.sun.syndication.feed.synd.SyndFeed feed = input.build(new com.sun.syndication.io.XmlReader(feedUrl)); pageTitle=feed.getTitle(); %> div align="center"> h1>%=pageTitle%>/h1> input type="hidden" id="feed" value=%=urlStr %>> input type="hidden" id="newWindow" value=%=pageContext.getAttribute("newWindow")%>> input type=checkbox name="check" id="check" onclick="check()">在新窗口中打開(部分網(wǎng)頁不支持在框架中顯示,請嘗試勾選此項)/input> table border=1 cellpadding=3 cellspacing="0"> tr> th>序號/th> th>標題/th> th>發(fā)布時間/th> /tr> % String date="無"; java.util.List list = feed.getEntries(); for (int i=0; i list.size(); i++) { com.sun.syndication.feed.synd.SyndEntry entry = (com.sun.syndication.feed.synd.SyndEntry)list.get(i); SimpleDateFormat sdf = new SimpleDateFormat(); sdf.applyPattern("yyyy年MM月dd日 HH:mm"); if(entry.getPublishedDate()!=null) { date=sdf.format(entry.getPublishedDate()); } %> tr> td>%=i+1%>/td> td>a id="entry" href="%=entry.getLink()%>" target=%=target %>>%=entry.getTitle()%>/a>/td> td>%=date %>/td> /tr> % } } catch (Exception e) { // TODO Auto-generated catch block response.sendRedirect("tips.jsp?type=rssContentfail"); e.printStackTrace(); } %> /table> /div> br> /body> head> meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> title>%=pageTitle%>/title> /head> /html> /RSSreader/WebContent/addGroup.jsp: %@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8" import="com.JDBConnection,java.sql.ResultSet,com.dataHelper,java.util.ArrayList"%> !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> link href="css/tab.css" type="text/css" rel="stylesheet" /> script type="text/javascript" src="js/jquery.js">/script> /head> % String lastAdd = request.getParameter("lastAdd"); String from = request.getParameter("from"); String tip=""; String backurl=""; String backname=""; if(lastAdd!=null){ String name =new String(request.getParameter("name").getBytes("ISO8859_1"), "utf-8"); if(lastAdd.equals("ture")){ tip="成功添加分組:"+name; } if(lastAdd.equals("false")){ tip="添加分組失敗"; } } %> body onunload="javascript:;window.opener.location.reload()"> div id="wrapper"> div align="center"> div>a href="addRss.jsp">添加源/a> | 添加分組/div> form name="form_addGroup" method="post" action="dealAddGroup"> table border="0" align="center"> tr> td height="30">組名:/td> td height="30"> input type="text" size="40" name="gname"> input type="hidden" name="uid" value="%=session.getAttribute("uid")%>"> /td> /tr> tr> td> /td> td>%=tip %>/td> /tr> tr> td height="30"> /td> td height="30" align="right">input type="submit" name="Submit" value="添加">/td> /tr> /table> /form> /div> /div> /body> /html> /RSSreader/WebContent/addRss.jsp: %@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8" import="com.JDBConnection,java.sql.ResultSet,com.dataHelper,java.util.ArrayList"%> !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>添加RSS源/title> link href="css/tab.css" type="text/css" rel="stylesheet" /> script type="text/javascript" src="js/jquery.js">/script> /head> % String lastAdd = request.getParameter("lastAdd"); String title =""; String url =""; String tip=""; String Sgid=""; if(request.getParameter("title")!=null) { title=new String(request.getParameter("title").getBytes("ISO8859_1"), "utf-8"); } if(request.getParameter("url")!=null) { url=request.getParameter("url"); } if(request.getParameter("group")!=null) { Sgid=request.getParameter("group"); } if(lastAdd!=null){ String name =new String(request.getParameter("name").getBytes("ISO8859_1"), "utf-8"); if(lastAdd.equals("ture")){ tip="成功添加RSS源:"+name; } if(lastAdd.equals("false")){ tip="添加RSS源失敗"; } } %> body onload="document.all.group.value = %=Sgid%>" onunload="javascript:;window.opener.location.reload()"> div id="wrapper"> div align="center"> div>添加源 | a href="addGroup.jsp">添加分組/a>/div> form name="form_addRss" method="post" action="dealAddRss"> input type="hidden" name="from" value="addRss"> table border="0" align="center"> tr> td height="30">Feed地址:/td> td height="30">input type="text" size="40" name="url" value=%=url %>>/td> /tr> tr> td height="30">標題:/td> td height="30"> input type="text" name="title" value=%=title%>> input align="right" type=button name="getTitle" value=" 從feed中提取" onclick="form_addRss.action='getFeedTitle';form_addRss.submit()"> /td> /tr> tr> td>分組:/td> td> select name="group"> % int gid; String gname; int uid; Object memo; int i; dataHelper dhp=new dataHelper(); ArrayListdataHelper.Group> groupList =dhp.getGroup((Integer)session.getAttribute("uid")); for(i=0;igroupList.size();i++) { dataHelper.Group group=groupList.get(i); %> option value="%=group.getGid()%>">%=group.getGname()%>/option> % } %> /select> /td> /tr> tr> td> /td> td>%=tip %>/td> /tr> tr> td height="30"> /td> td height="30" align="right">input type="submit" name="Submit" value="添加" onclick="form_addRss.action='dealAddRss'">/td> /tr> /table> /form> /div> div align="center"> /div> /div> /body> /html> /RSSreader/WebContent/delete.jsp: %@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8" import="com.JDBConnection,java.sql.ResultSet"%> !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; utf-8"> title>editGroup/title> /head> body> div align="center"> % String name=(String)session.getAttribute("name"); String nickname=""; if(name==null) { response.sendRedirect("index.jsp"); } %> % String gid =request.getParameter("gid"); String lid =request.getParameter("lid"); String type =request.getParameter("type"); String url=""; //DELETE FROM 表名稱 WHERE 列名稱 = 值 String sSql=""; if(type!=null){ if(type.equals("g")){ sSql="delete from rssGroup where gid="+gid; url="manage.jsp"; } if(type.equals("r")){ sSql="delete from rssList where lid="+lid; url="rssListOfGroup.jsp?gid="+gid; } } System.out.println("rssListOfGroup.Sql:"+sSql); JDBConnection JDBC=new JDBConnection(); if(JDBC.executeUpdate(sSql)){ out.print("刪除成功"); } else{ out.print("刪除失敗"); } %> /div> br> div align="center"> a href=%=url %>>返回/a> /div> /body> /html> JAVA部分 /RSSreader/src/com/JDBConnection.java package com; import java.sql.*; public class JDBConnection { private final String url = "jdbc:sqlserver://localhost:1433;DatabaseName=RSSreader"; private final String userName = "sa"; private final String password = "123456"; private Connection con = null; //通過構造方法加載數(shù)據(jù)庫驅動 static { try { Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); } catch (Exception ex) { System.out.println("數(shù)據(jù)庫加載失敗"); } } //創(chuàng)建數(shù)據(jù)庫連接 public boolean creatConnection() { try { con = DriverManager.getConnection(url, userName, password); con.setAutoCommit(true); } catch (SQLException e) { System.out.println(e.getMessage()); System.out.println("creatConnectionError!"); } return true; } public void close(Connection con,Statement stmt,PreparedStatement pst,ResultSet rs){ if(rs!=null){ try { rs.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if(stmt!=null){ try { stmt.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if(pst!=null){ try { pst.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if(con!=null){ try { con.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } public void closeConnection(){ if(con!=null){ try { con.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } //對數(shù)據(jù)庫的增加、修改和刪除的操作 public boolean executeUpdate(String sql) { if (con == null) { creatConnection(); } try { Statement stmt = con.createStatement(); int iCount = stmt.executeUpdate(sql); System.out.println("操作成功,所影響的記錄數(shù)為" + String.valueOf(iCount)); this.close(con, stmt,null, null); return true; } catch (SQLException e) { System.out.println(e.getMessage()); return false; } } //對數(shù)據(jù)庫的查詢操作 public ResultSet executeQuery(String sql) { ResultSet rs; try { if (con == null) { creatConnection(); } Statement stmt = con.createStatement(); try { rs = stmt.executeQuery(sql); } catch (SQLException e) { System.out.println(e.getMessage()); return null; } } catch (SQLException e) { System.out.println(e.getMessage()); System.out.println("executeQueryError!"); return null; } return rs; } } /RSSreader/src/com/SqlFilter.java package com; import java.io.IOException; import java.util.Enumeration; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; //過濾sql關鍵字的Filter public class SqlFilter implements Filter { public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { HttpServletRequest req = (HttpServletRequest) request; HttpServletResponse res = (HttpServletResponse) response; //獲得所有請求參數(shù)名 Enumeration params = req.getParameterNames(); String sql = ""; while (params.hasMoreElements()) { //得到參數(shù)名 String name = params.nextElement().toString(); //System.out.println("name===========================" + name + "--"); //得到參數(shù)對應值 String[] value = req.getParameterValues(name); for (int i = 0; i value.length; i++) { sql = sql + value[i]; } } System.out.println("被匹配字符串:"+sql); if (sqlValidate(sql)) { res.sendRedirect("error.jsp"); } else { chain.doFilter(req, res); } } //效驗 protected static boolean sqlValidate(String str) { str = str.toLowerCase();//統(tǒng)一轉為小寫 //String badStr = "and|exec"; String badStr = "'|and|exec|execute|insert|select|delete|update|count|drop|chr|mid|master|truncate|char|declare|sitename|net user|xp_cmdshell|or|like"; /*String badStr = "'|and|exec|execute|insert|create|drop|table|from|grant|use|group_concat|column_name|" + "information_schema.columns|table_schema|union|where|select|delete|update|order|by|count|*|" + "chr|mid|master|truncate|char|declare|or|;|-|--|+|,|like|//|/|%|#"; */ //過濾掉的sql關鍵字,可以手動添加 String[] badStrs = badStr.split("\\|"); for (int i = 0; i badStrs.length; i++) { if (str.indexOf(badStrs[i]) !=-1) { System.out.println("匹配到:"+badStrs[i]); return true; } } return false; } public void init(FilterConfig filterConfig) throws ServletException { //throw new UnsupportedOperationException("Not supported yet."); } public void destroy() { //throw new UnsupportedOperationException("Not supported yet."); } } /RSSreader/src/dataCtrl/addGroup.java package dataCtrl; import com.JDBConnection; public class addGroup { private String gname; private String uid; public String getGname() { return gname; } public void setGname(String gname) { this.gname = gname; } public String getUid() { return uid; } public void setUid(String uid) { this.uid = uid; } public boolean doAddGroup(){ String sSql = "insert into rssGroup(gname,uid) values('"+gname+"',"+uid+")"; System.out.println(sSql); JDBConnection JDBC=new JDBConnection(); return JDBC.executeUpdate(sSql); } } /RSSreader/src/dataCtrl/addRss.java package dataCtrl; import java.sql.*; import com.JDBConnection; import javafx.beans.property.adapter.JavaBeanProperty; public class addRss { private String title; private String url; private String gid; public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } public String getGid() { return gid; } public void setGid(String gid) { this.gid = gid; } public boolean doAddRss() throws SQLException{ //INSERT INTO table_name (列1, 列2,...) VALUES (值1, 值2,....); String sSql = "insert into rssList(title,url,gid) values('"+title+"','"+url+"',"+gid+")"; System.out.println(sSql); JDBConnection JDBC=new JDBConnection(); return JDBC.executeUpdate(sSql); } } /RSSreader/src/dataCtrl/dealaddGroup.java package dataCtrl; import java.io.IOException; import java.sql.SQLException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Servlet implementation class dealaddGroup */ @WebServlet("/dealaddGroup") public class dealaddGroup extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public dealaddGroup() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub response.getWriter().append("Served at: ").append(request.getContextPath()); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub String gname=request.getParameter("gname"); String sgname =new String(gname.getBytes("ISO8859_1"), "utf-8"); String uid=request.getParameter("uid"); addGroup adgp=new addGroup(); adgp.setGname(sgname); adgp.setUid(uid); if(adgp.doAddGroup()){ request.getRequestDispatcher("/addGroup.jsp?name="+gname+"lastAdd=ture").forward(request,response); //response.sendRedirect("index.jsp"); } else{ request.getRequestDispatcher("/addGroup.jsp?name="+gname+"lastAdd=false").forward(request,response); } doGet(request, response); } } /RSSreader/src/dataCtrl/dealAddRss.java package dataCtrl; import java.io.IOException; import java.sql.SQLException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Servlet implementation class dealAddRss1 */ @WebServlet("/dealAddRss1") public class dealAddRss extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public dealAddRss() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub response.getWriter().append("Served at: ").append(request.getContextPath()); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub //response.setContentType("text/html;charset=utf-8"); String title=request.getParameter("title"); String stitle =new String(title.getBytes("ISO8859_1"), "utf-8"); String url =request.getParameter("url"); String gid=request.getParameter("group"); addRss adrs=new addRss(); adrs.setTitle(stitle); adrs.setUrl(url); adrs.setGid(gid); try { if(adrs.doAddRss()){ request.getRequestDispatcher("/addRss.jsp?name="+title+"lastAdd=ture").forward(request,response); //response.sendRedirect("index.jsp"); } else{ request.getRequestDispatcher("/addRss.jsp?name="+title+"lastAdd=false").forward(request,response); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } doGet(request, response); } } /RSSreader/src/dataCtrl/getFeedTitle.java package dataCtrl; 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.sun.syndication.io.FeedException; /** * Servlet implementation class getFeedTitle */ @WebServlet("/getFeedTitle") public class getFeedTitle extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public getFeedTitle() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub response.getWriter().append("Served at: ").append(request.getContextPath()); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub String urlStr =request.getParameter("url"); String title=""; String gid=request.getParameter("group"); String lid=request.getParameter("lid"); String Lastgid=request.getParameter("Lastgid"); String from=request.getParameter("from"); if (!urlStr.startsWith("http://")) urlStr = "http://"+request.getParameter("feed"); //String urlStr = "http://feed.cnblogs.com/blog/u/249598/rss"; System.out.print(urlStr); java.net.URLConnection feedUrl = new java.net.URL(urlStr).openConnection(); feedUrl.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)"); com.sun.syndication.io.SyndFeedInput input = new com.sun.syndication.io.SyndFeedInput(); com.sun.syndication.feed.synd.SyndFeed feed; try { feed = input.build(new com.sun.syndication.io.XmlReader(feedUrl)); title=feed.getTitle(); } catch (IllegalArgumentException | FeedException e) { // TODO Auto-generated catch block e.printStackTrace(); } if(from.equals("addRss")){ response.sendRedirect("addRss.jsp?title="+title+"url="+urlStr+"group="+gid+"Lastgid="+Lastgid+"lid="+lid); } if(from.equals("editRss")){ response.sendRedirect("editRss.jsp?title="+title+"url="+urlStr+"group="+gid+"Lastgid="+Lastgid+"lid="+lid); } doGet(request, response); } } /RSSreader/src/loginCtrl/checkUser.java package loginCtrl; import java.sql.ResultSet; import java.sql.SQLException; import com.JDBConnection; public class checkUser { private int uid=0; private String name=" "; private String pwd=" "; public int getUid() { return uid; } public void setUid(int uid) { this.uid = uid; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPwd() { return pwd; } public void setPwd(String pwd) { this.pwd = pwd; } public boolean check() { String sPwd=""; String sSql = "select * from users where uname='"+name+"'"; System.out.println(sSql); try { JDBConnection JDBC=new JDBConnection(); ResultSet rs =JDBC.executeQuery(sSql); System.out.println(rs.isBeforeFirst()); if(rs.next()) { System.out.println("?"); sPwd=rs.getString("upwd"); uid=rs.getInt("uid"); //System.out.println(sPwd); JDBC.closeConnection(); } } catch (SQLException e) { // TODO Auto-generated catch block System.out.println(e.getMessage()); System.out.println("ConnectError!"); } System.out.println(name+"-"+pwd+"-"+sPwd); if(pwd.equals(sPwd)) { return true; } else { return false; } } } /RSSreader/src/loginCtrl/dealsignup.java package loginCtrl; import java.io.IOException; import java.sql.Connection; 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 javax.servlet.http.HttpSession; /** * Servlet implementation class dealsignup */ @WebServlet("/dealsignup") public class dealsignup extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public dealsignup() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub response.getWriter().append("Served at: ").append(request.getContextPath()); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub response.setContentType("text/html;charset=utf-8"); String name =request.getParameter("account"); String url =request.getParameter("url"); String nickname =request.getParameter("nickname"); if (nickname==null||nickname=="") nickname=null; else nickname=new String(request.getParameter("nickname").getBytes("ISO8859_1"), "utf-8"); String pwd= request.getParameter("password"); String enpwd= request.getParameter("ensurepassword"); signup snp=new loginCtrl.signup(); System.out.println("dealsingnup:"+name+"-"+nickname+"-"+pwd+"-"+enpwd); if(!pwd.equals(enpwd)) { response.sendRedirect("tips.jsp?type=signupwithwrongpwd"); System.out.println("兩次密碼不一致"); } else { snp.setName(name); snp.setNickname(nickname); snp.setPwd(pwd); Connection conn=snp.connect(); if(snp.signupcheck(conn)) { if(snp.dosignup(conn)) { response.sendRedirect("tips.jsp?type=signupsuccessstr="+name); System.out.println("注冊成功"); } else { response.sendRedirect("tips.jsp?type=sinupfail"); System.out.println("未知錯誤"); } } else { response.sendRedirect("tips.jsp?type=usernamevalid"); System.out.println("用戶名已被注冊"); } snp.closeConnection(conn); } doGet(request, response); } } /RSSreader/src/loginCtrl/loginCheck.java package loginCtrl; import java.io.IOException; import java.sql.ResultSet; 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 javax.servlet.http.HttpSession; import com.JDBConnection; /** * Servlet implementation class loginCheck */ @WebServlet("/loginCheck") public class loginCheck extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public loginCheck() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub response.getWriter().append("Served at: ").append(request.getContextPath()); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub String name =request.getParameter("account"); String pwd= request.getParameter("password"); checkUser uc=new loginCtrl.checkUser(); System.out.println(name+"-"+pwd); uc.setName(name); uc.setPwd(pwd); if(uc.check()) { HttpSession session=request.getSession(); session.setAttribute("name", name); session.setAttribute("uid", uc.getUid()); response.sendRedirect("index.jsp"); } else { response.sendRedirect("tips.jsp?type=logfail"); } doGet(request, response); } } /RSSreader/src/loginCtrl/signup.java package loginCtrl; import java.sql.*; public class signup { private String name=" "; private String nickname=""; private String pwd=" "; private Connection conn; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getNickname() { return nickname; } public void setNickname(String nickname) { this.nickname = nickname; } public String getPwd() { return pwd; } public void setPwd(String pwd) { this.pwd = pwd; } public Connection connect() { try { String JDriver="com.microsoft.sqlserver.jdbc.SQLServerDriver";//SQL數(shù)據(jù)庫引擎 String connectDB= "jdbc:sqlserver://localhost:1433;DatabaseName=RSSreader";//數(shù)據(jù)源 try { Class.forName(JDriver);//加載數(shù)據(jù)庫引擎,返回給定字符串名的類 }catch(ClassNotFoundException e) { //e.printStackTrace(); System.out.println("加載數(shù)據(jù)庫引擎失?。?+e); System.exit(0); } System.out.println("數(shù)據(jù)庫驅動成功"); String user="sa"; String password="123456"; conn=DriverManager.getConnection(connectDB,user,password);//連接數(shù)據(jù)庫對象 System.out.println("連接數(shù)據(jù)庫成功"); } catch(Exception e) { System.out.println("鏈接數(shù)據(jù)庫失敗:"+e); } return conn; } public boolean signupcheck(Connection conn) { try{ Statement stmt=conn.createStatement(); ResultSet rs=stmt.executeQuery("select * from users where uname='"+name+"'"); if(!rs.next()) return true; } catch(Exception e) { System.out.println("查詢失敗"+e); return false; } return false; } public boolean dosignup(Connection conn) { try { PreparedStatement ps = conn.prepareStatement("insert into users (uname,nickname,upwd) values(?,?,?)"); ps.setString(1, name); ps.setString(2, nickname); ps.setString(3, pwd); int num =ps.executeUpdate(); System.out.println(num); if (num==0) return false; ps.close(); } catch(Exception e) { System.out.println("添加失敗"+e); return false; } return true; } public boolean closeConnection(Connection conn) { try { System.out.println("關閉鏈接成功"); conn.close(); } catch(Exception e) { System.out.println("關閉鏈接失敗"+e); return false; } return true; } }
注冊與登錄
使用閱讀器之前需要注冊用戶并登錄,服務器會保存用戶的RSS列表數(shù)據(jù)。未登錄之前閱讀器會推薦閱讀閱讀列表,并指導用戶尋找RSS源,如下圖:
注冊時輸入為空或輸入錯誤會有相應提示
注冊成功后會提示用戶記住用戶名,然后跳轉登錄頁面:
登錄后來到主頁,同樣閱讀器會引導新用戶尋找RSS源,點擊"尋找RSS源"會跳轉到下面這樣一個神奇的頁面:
然后就是添加自己需要訂閱的RSS源了。
添加與管理RSS源
注冊登錄后,就可以添加自己喜愛的RSS源了。點擊添加,彈出添加對話框,這是我們需要先添加分組再添加RSS源。我們先添加兩個測試分組,如下圖:
添加成功會有提示:
添加好分組后再來添加RSS源,我們測試添加一個"知乎每日精選"的訂閱:www.zhihu.com/rss 直接在瀏覽器中打開是這樣的:
這個當然看不懂,我們將url粘貼到閱讀器中,標題可以不用自己填,點擊"從Feed中獲取",閱讀器會根據(jù)url從RSS源中解析出RSS源的標題。選擇"測試分組1",保存。
第一個源已經(jīng)添加成功了,接下來再添加幾個源用作測試。
添加完成后,關閉對話框,左邊列表會自動刷新,點擊展開顯示已添加的RSS源:
這樣我們就完成了RSS源的添加。如果有些源名字、分組有錯或者源失效了、自己不想再訂閱了,我們可以對其進行編輯或者直接刪除。點擊"管理",彈出管理對話框:
此時我們可以修改分組名或者將其刪除:
注意:刪除分組會刪除該組內(nèi)的所有RSS源。修改之后:
點擊展開,可以顯示該分組下的所有RSS源:
同樣我們可以修改RSS源或者將其刪除,方法與對組的操作類似,修改后:
修改完成后,關閉對話框,同樣左邊列表會自動刷新:
閱讀文章
點擊其中一個RSS源,會在右邊顯示出該訂閱的文章列表:
然后點擊文章的標題就會顯示文章的具體內(nèi)容:
如果勾選了"在新窗口中打開",文章將會在新的瀏覽器窗口中打開。
好了基于JSP的RSS閱讀器的介紹就到這里,閱讀器訪問地址:http://easyrss.tk/
以上這篇基于JSP的RSS閱讀器的設計與實現(xiàn)方法(推薦)就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。