Realize:
Hierarchical architecture Diagram:
Key code: Console Output News content
Implementation class:
Package Cn.news.impl;import Java.sql.resultset;import Java.util.arraylist;import java.util.list;import Org.junit.test;import Cn.news.dao.basedao;import Cn.news.dao.newsdetaildao;import Cn.news.entity.NewsDetail; public class Newsdetaildaoimpl extends Basedao implements newsdetaildao{@Test public void Test () throws Exceptio n {list<newsdetail> list=getallnews (); for (Newsdetail item:list) {System.out.println (item.getnewscontent ()); }} @Override public list<newsdetail> getallnews () throws Exception {List<newsdetail> ; List=new arraylist<newsdetail> (); String sql= "SELECT * from Newsdetails"; ResultSet rs=executequery (SQL); if (Rs!=null) {while (Rs.next ()) {Newsdetail news=new newsdetail (); News.setnewsid (Rs.getint ("newsId")); News.setnewstitle (Rs.getstring ("NewsTitle ")); News.setnewscontent (rs.getstring ("newscontent")); News.setnewscreatedate (Rs.getdate ("newscreatedate")); News.setnewsauthor (rs.getstring ("Newsauthor")); News.setnewscategoryid (Rs.getint ("Newscategoryid")); List.add (news); }} return list; }}
Page displays news headlines and time:
New Newsservlet class inherits HttpServlet. Key code:
Package Cn.news.servlet;import Java.io.ioexception;import Java.io.printwriter;import java.util.list;import Javax.servlet.servletexception;import Javax.servlet.http.httpservlet;import Javax.servlet.http.httpservletrequest;import Javax.servlet.http.httpservletresponse;import Cn.news.entity.newsdetail;import Cn.news.impl.newsdetaildaoimpl;public class Newsservlet extends HttpServlet {/** */public void doget (HttpServletRequest request, httpservletresponse response) throws Servletexception, Ioex ception {doPost (request,response); }/** */public void DoPost (HttpServletRequest request, httpservletresponse response) throws Serv Letexception, IOException {//Instantiate DAO Newsdetaildaoimpl dao=new Newsdetaildaoimpl (); try {list<newsdetail> list=dao.getallnews (); Request.setattribute ("list", list); } catch (Exception e) {e.printstacktrace (); } Request.getrequestdispatcher ("/index.jsp"). Forward (request, response); }}
index.jsp
<ul class= "Classlist" > <% list<newsdetail>list= (list<newsdetail>) Request.getattribute ("list"); for (Newsdetail item:list) { %> <li><a href= ' newspages/news_read.jsp ' ><%= Item.getnewstitle ()%></a><span><%=item.getnewscreatedate ()%></span></li> <% } %> <p align= "right" > Current page:[1/2] <a href= "#" > Next </a> <a href = "#" > End </a> </p> </ul>
Xml
News Release System architecture