Realize:
Hierarchical architecture Diagram:
Key code: Console Output News content
Implementation class:
PackageCn.news.impl;ImportJava.sql.ResultSet;Importjava.util.ArrayList;Importjava.util.List;Importorg.junit.Test;ImportCn.news.dao.BaseDao;ImportCn.news.dao.NewsDetailDao;ImportCn.news.entity.NewsDetail; Public classNewsdetaildaoimplextendsBasedaoImplementsnewsdetaildao{@Test Public voidTest ()throwsException {List<NewsDetail> list=getallnews (); for(Newsdetail item:list) {System.out.println (item.getnewscontent ()); }} @Override PublicList<newsdetail> Getallnews ()throwsException {List<NewsDetail> list=NewArraylist<newsdetail>(); String SQL= "SELECT * FROM Newsdetails"; ResultSet RS=executeQuery (SQL); if(rs!=NULL) { while(Rs.next ()) {Newsdetail News=NewNewsdetail (); 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); } } returnlist; }}
Page displays news headlines and time:
New Newsservlet class inherits HttpServlet. Key code:
PackageCn.news.servlet;Importjava.io.IOException;ImportJava.io.PrintWriter;Importjava.util.List;Importjavax.servlet.ServletException;ImportJavax.servlet.http.HttpServlet;Importjavax.servlet.http.HttpServletRequest;ImportJavax.servlet.http.HttpServletResponse;ImportCn.news.entity.NewsDetail;ImportCn.news.impl.NewsDetailDaoImpl; Public classNewsservletextendsHttpServlet {/** */ Public voiddoget (httpservletrequest request, httpservletresponse response)throwsservletexception, IOException {doPost (request,response); } /** */ Public voidDoPost (httpservletrequest request, httpservletresponse response)throwsservletexception, IOException {//Instantiate DAONewsdetaildaoimpl dao=NewNewsdetaildaoimpl (); Try{List<NewsDetail> list=dao.getallnews (); Request.setattribute ("List", list); } Catch(Exception e) {e.printstacktrace (); } request.getrequestdispatcher ("/index.jsp"). Forward (request, response); }}
index.jsp
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= "#" > Last </a > </p> </ul>
Xml
News Release System architecture