基于jsp实现新闻管理系统 附完整源码
很棒的新闻发布系统分享给大家,希望大家喜欢。
下面就让我们来说一说基于jsp的新闻发布系统,其中使用的技术有javabean、fillter、数据库等,能够实现新闻的发布功能,在发布之后能够进行对每一条新闻的删除、修改、或者继续增加新的文章,最后还能够进行查询功能,其中引用了百度编辑器,能够进行图文并茂的编辑,极大地方便用户的使用。
注:完整项目下载地址:新闻发布系统
一、效果演示
首先让我们来看一看实现的效果:
下面是登陆的首界面:
图1 首界面
管理员登录页面:
图2 管理员登录界面
下面是管理员登陆之后的界面,可以进行添加新闻、删除新闻、查询新闻:
图3 管理员管理界面
下面是添加新闻界面,能够进行新闻的添加:
图4 添加新闻界面
下面是所添加的新闻:
图5 添加后的新闻
下面是修改新闻界面:
图6 修改新闻界面下面是修改后的界面
图7 修改后的界面
下面是删除新闻后的界面:
图8 删除新闻后的界面
对于新闻的查找,与其他类似,在这里我们使用的是利用新闻编号进行查询所需的信息,然后在新的页面上进行显示。
二、具体实现过程
1、环境:编程环境为netbeans8.0和oracle数据库
2、数据库后台的设计
在新闻管理系统中,我们需要数据库后台管理,其具体步骤如下所示:
根据所需的业务进行建表,可参见如下所示:
3、编写步骤:
(1)首先建立以个项目,在此命名为demo_08_newsrealese,项目布局结构如下图所示,根据如下布局对项目进行布局设计,方便后面的编程,项目中web-inf为项目配置文件夹,images文件夹用于储存项目中使用的图片文件,ueditor为百度富文本编辑器文件夹,源包文件夹为java类文件夹:
图9 项目布局结构图
接着,我们进行创建数据库连接层,即dao和vo,分别存于包com.lut.beans和dao,类命名分别命名为newsrealese、newsrealesedao,对于类newsrealese而言,主要是实现字符串的包装,其内容与数据库里边的内容一一对应,其代码分别如下所示:
package com.lut.beans; public class newsrealese { private string newsid; private string classid; private string kindid; private string myother; private string headtitle; private string content; private string connectrealtive; private string author; private string editor; private string newsfrom; private string top; private string newstime; private string hits; private string state; private string tag; public string getnewsid() { return newsid; } public void setnewsid(string newsid) { this.newsid = newsid; } public string getclassid() { return classid; } public void setclassid(string classid) { this.classid = classid; } public string getkindid() { return kindid; } public void setkindid(string kindid) { this.kindid = kindid; } public string getmyother() { return myother; } public void setmyother(string myother) { this.myother = myother; } public string getheadtitle() { return headtitle; } public void setheadtitle(string headtitle) { this.headtitle = headtitle; } public string getcontent() { return content; } public void setcontent(string content) { this.content = content; } public string getconnectrealtive() { return connectrealtive; } public void setconnectrealtive(string connectrealtive) { this.connectrealtive = connectrealtive; } public string getauthor() { return author; } public void setauthor(string author) { this.author = author; } public string geteditor() { return editor; } public void seteditor(string editor) { this.editor = editor; } public string getnewsfrom() { return newsfrom; } public void setnewsfrom(string newsfrom) { this.newsfrom = newsfrom; } public string gettop() { return top; } public void settop(string top) { this.top = top; } public string getnewstime() { return newstime; } public void setnewstime(string newstime) { this.newstime = newstime; } public string gethits() { return hits; } public void sethits(string hits) { this.hits = hits; } public string getstate() { return state; } public void setstate(string state) { this.state = state; } public string gettag() { return tag; } public void settag(string tag) { this.tag = tag; } }
下面是类dao类,命名为newsrealesedao.java,对于dao类,主要用于实现数据库的连接问题,进行数据库的链接,在连接过程中需要用到各种有关数据库的技术,在这里我们使用的是oracle数据库,其获取链接的方式为如下代码,其中第一行为驱动程序名,第二行为数据库地址(orcl为oracle数据库中的sid,在安装oracle数据库时需要记住),第三行为具体的连接,及使用url找到数据库位置,然后使用密码wjk139登录到scott账户
//获取连接 class.forname("oracle.jdbc.driver.oracledriver"); string url = "jdbc:oracle:thin:@localhost:1521:orcl"; conn = drivermanager.getconnection(url, "scott", "wjk139");
其他的操作分别为增删改查的具体代码,分别对应servlet中或jsp中需要实例化的代码,下面就是具体的dao类代码:
package dao; import com.lut.beans.newsrealese; import static java.lang.system.out; import java.sql.connection; import java.sql.drivermanager; import java.sql.preparedstatement; import java.sql.resultset; import java.sql.sqlexception; import java.sql.statement; import java.util.arraylist; public class newsrealesedao { public arraylist queryallnews() throws exception { connection conn = null; arraylist newsrealese = new arraylist(); try { //获取连接 class.forname("oracle.jdbc.driver.oracledriver"); string url = "jdbc:oracle:thin:@localhost:1521:orcl"; conn = drivermanager.getconnection(url, "scott", "wjk139"); //运行sql语句 string sql = "select * from newmessage";//获取 statement stat = conn.createstatement(); resultset rs = stat.executequery(sql); while (rs.next()) { //实例化vo newsrealese news = new newsrealese(); news.setnewsid(rs.getstring("newsid")); news.setclassid(rs.getstring("classid")); news.setkindid(rs.getstring("kindid")); news.setmyother(rs.getstring("myother")); news.setheadtitle(rs.getstring("headtitle")); news.setcontent(rs.getstring("content")); news.setconnectrealtive(rs.getstring("connectrealtive")); news.setauthor(rs.getstring("author")); news.seteditor(rs.getstring("editor")); news.setnewsfrom(rs.getstring("newsfrom")); news.settop(rs.getstring("top")); news.setnewstime(rs.getstring("newstime")); news.sethits(rs.getstring("hits")); news.setstate(rs.getstring("state")); news.settag(rs.getstring("tag")); newsrealese.add(news); } rs.close(); stat.close(); } catch (exception e1) { e1.printstacktrace(); } finally { try {//关闭连接 if (conn != null) { conn.close(); conn = null; } } catch (exception ex) { } return newsrealese; } } //查询一个消息 public arraylist queryonenews(int newsid) throws exception { connection conn = null; arraylist newsrealese = new arraylist(); int temp_id = newsid; try { //获取连接 class.forname("oracle.jdbc.driver.oracledriver"); string url = "jdbc:oracle:thin:@localhost:1521:orcl"; conn = drivermanager.getconnection(url, "scott", "wjk139");//不安全 //运行sql语句 statement stat = conn.createstatement(); string sql = "select * from newmessage where newsid=?";//获取newsid,使用?代替字符串,以免会发生错误 preparedstatement ps = conn.preparestatement(sql); ps.setint(1, integer.valueof(newsid)); resultset rs = ps.executequery();// 之前已经给了sql字符串,所以executeupdate是无参的。 while (rs.next()) { //实例化vo newsrealese news = new newsrealese(); news.setnewsid(rs.getstring("newsid")); news.setclassid(rs.getstring("classid")); news.setkindid(rs.getstring("kindid")); news.setmyother(rs.getstring("myother")); news.setheadtitle(rs.getstring("headtitle")); news.setcontent(rs.getstring("content")); news.setconnectrealtive(rs.getstring("connectrealtive")); news.setauthor(rs.getstring("author")); news.seteditor(rs.getstring("editor")); news.setnewsfrom(rs.getstring("newsfrom")); news.settop(rs.getstring("top")); news.setnewstime(rs.getstring("newstime")); news.sethits(rs.getstring("hits")); news.setstate(rs.getstring("state")); news.settag(rs.getstring("tag")); newsrealese.add(news); } rs.close(); stat.close(); } catch (exception e1) { e1.printstacktrace(); } finally { try {//关闭连接 if (conn != null) { conn.close(); conn = null; } } catch (exception ex) { } return newsrealese; } } //删除数据 public string deleteonenews(int newsid) throws exception { connection conn = null; arraylist newsrealese = new arraylist(); int temp_id = newsid; try { //获取连接 class.forname("oracle.jdbc.driver.oracledriver"); string url = "jdbc:oracle:thin:@localhost:1521:orcl"; conn = drivermanager.getconnection(url, "scott", "wjk139");//不安全 //运行sql语句 statement stat = conn.createstatement(); string sql = "delete from newmessage where newsid =?";//获取newsid,使用?代替字符串,以免会发生错误 preparedstatement ps = conn.preparestatement(sql); ps.setint(1, integer.valueof(newsid)); resultset rs = ps.executequery();// 之前已经给了sql字符串,所以executeupdate是无参的。 rs.close(); stat.close(); } catch (exception e1) { e1.printstacktrace(); } finally { try {//关闭连接 if (conn != null) { conn.close(); conn = null; } } catch (exception ex) { } return newsrealese.tostring(); } } //插入数据 public string insertonenews(arraylist addnews_list) throws exception { connection conn = null; /* for(int i=0,j=1;i<addnews_list.size();i++,j++) { system.out.println("j:"+j+"值:"+addnews_list.get(i).tostring()); }*/ try { //获取连接 class.forname("oracle.jdbc.driver.oracledriver"); string url = "jdbc:oracle:thin:@localhost:1521:orcl"; conn = drivermanager.getconnection(url, "scott", "wjk139");//不安全 //运行sql语句 statement stat = conn.createstatement(); string sql = "insert into newmessage(newsid,classid,kindid,myother,headtitle,content,connectrealtive,author,editor,newsfrom" + ",top,newstime,hits,state,tag) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";//获取newsid,使用?代替字符串,以免会发生错误 preparedstatement ps = conn.preparestatement(sql); ps.setint(1, integer.valueof(addnews_list.get(0).tostring())); ps.setint(2, integer.valueof(addnews_list.get(1).tostring())); ps.setint(3, integer.valueof(addnews_list.get(2).tostring())); ps.setint(4, integer.valueof(addnews_list.get(3).tostring())); ps.setstring(5, addnews_list.get(4).tostring()); ps.setstring(6, addnews_list.get(5).tostring()); ps.setstring(7, addnews_list.get(6).tostring()); ps.setstring(8, addnews_list.get(7).tostring()); ps.setstring(9, addnews_list.get(8).tostring()); ps.setstring(10, addnews_list.get(9).tostring()); ps.setint(11, integer.valueof(addnews_list.get(10).tostring())); ps.setstring(12, addnews_list.get(11).tostring()); ps.setstring(13, addnews_list.get(12).tostring()); ps.setstring(14, addnews_list.get(13).tostring()); ps.setstring(15, addnews_list.get(14).tostring()); for (int i = 0, j = 1; i < addnews_list.size(); i++, j++) { system.out.println("j:" + j + "值:" + addnews_list.get(i).tostring()); } int i = ps.executeupdate(); conn.commit(); system.out.println("成功添加" + i + "行"); stat.close(); conn.close(); return i + "conn:" + conn; } catch (exception e1) { e1.printstacktrace(); } finally { try {//关闭连接 if (conn != null) { conn.close(); conn = null; } } catch (exception ex) { } } return conn.tostring(); } //更新数据 public string updateonenews(arraylist addnews_list) throws exception { connection conn = null; /* for(int i=0,j=1;i<addnews_list.size();i++,j++) { system.out.println("j:"+j+"值:"+addnews_list.get(i).tostring()); }*/ try { //获取连接 class.forname("oracle.jdbc.driver.oracledriver"); string url = "jdbc:oracle:thin:@localhost:1521:orcl"; conn = drivermanager.getconnection(url, "scott", "wjk139");//不安全 //运行sql语句 statement stat = conn.createstatement(); //获取newsid,使用?代替字符串,以免会发生错误 string sql = "update newmessage set classid=?,kindid=?,myother=?,headtitle=?,content=?," + "connectrealtive=?,author=?,editor=?,newsfrom=?,top=?,newstime=?,hits=?,state=?,tag=? where newsid=?"; preparedstatement ps = conn.preparestatement(sql); ps.setint(1, integer.valueof(addnews_list.get(1).tostring())); ps.setint(2, integer.valueof(addnews_list.get(2).tostring())); ps.setint(3, integer.valueof(addnews_list.get(3).tostring())); ps.setstring(4, addnews_list.get(4).tostring()); ps.setstring(5, addnews_list.get(5).tostring()); ps.setstring(6, addnews_list.get(6).tostring()); ps.setstring(7, addnews_list.get(7).tostring()); ps.setstring(8, addnews_list.get(8).tostring()); ps.setstring(9, addnews_list.get(9).tostring()); ps.setint(10, integer.valueof(addnews_list.get(10).tostring())); ps.setstring(11, addnews_list.get(11).tostring()); ps.setstring(12, addnews_list.get(12).tostring()); ps.setstring(13, addnews_list.get(13).tostring()); ps.setstring(14, addnews_list.get(14).tostring()); ps.setint(15, integer.valueof(addnews_list.get(0).tostring())); for (int i = 0, j = 1; i < addnews_list.size(); i++, j++) { system.out.println("j:" + j + "值:" + addnews_list.get(i).tostring()); } int i = ps.executeupdate(); conn.commit(); system.out.println("成功更新" + i + "行"); stat.close(); conn.close(); return i + "conn:" + conn; } catch (exception e1) { e1.printstacktrace(); } finally { try {//关闭连接 if (conn != null) { conn.close(); conn = null; } } catch (exception ex) { } } return conn.tostring(); } public string ischecked(string user, string pass) throws exception { connection conn = null; //获取连接 class.forname("oracle.jdbc.driver.oracledriver"); string url = "jdbc:oracle:thin:@localhost:1521:orcl"; try { conn = drivermanager.getconnection(url,user, pass);//不安全 //建立连接 return conn.tostring(); } catch (sqlexception e) { e.printstacktrace(); } finally { if (conn != null) { try { conn.close(); } catch (sqlexception e) { e.printstacktrace(); } } } return null; } }
(2)首页展示页面的编写,命名为index.html,由于可能有网络的延迟,我们使用渐进的方式跳转(渐进方式跳转:window.location.href="newrealese_brief.jsp",从index.html页面跳转到ewrealese_brief.jsp页面),代码如下所示:
<!doctype html> <html> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8"> <link type="text/css" rel="stylesheet" href="newsrealese.css"/> <title>新闻发布系统--最前沿的时尚信息、最有看点的社会聚焦、最富得浪漫的殿堂</title> </head> <body id="body"> 正在跳转...,请等待... <script language="javascript" type="text/javascript"> window.location.href="newrealese_brief.jsp"; </script> </body> </html>
在缓冲时,会进行跳转,跳转后的页面为newrealese_brief.jsp,其代码如下所示:进入新闻展示页面之后,我们能够对新闻进行搜素,还能够查看详细内容。
<%@page contenttype="text/html" import="java.util.*,java.sql.*,com.lut.beans.newsrealese,dao.newsrealesedao" pageencoding="utf-8" language="java"%> <!doctype html> <html> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8"> <title>新闻发布系统--最前沿的时尚信息、最有看点的社会聚焦、最富得浪漫的殿堂</title> <link type="text/css" rel="stylesheet" href="newsrealese.css"/> </head> <body id="body"> <a href="adminlogin.jsp" style="table-layout: right">管理员登录</a><h1 id="p">最新资讯</h1><br> <%@include file="a_querynews.jsp" %> <table border="0" style="width:100%; height:100%" align="center"> <tr> <td style="width: 10%"> </td> <td> <% newsrealesedao newsrealesedao = new newsrealesedao(); arraylist newsrealese = newsrealesedao.queryallnews(); %> <% for (int i = 0; i < newsrealese.size(); i++) { newsrealese newrealese = (newsrealese) newsrealese.get(i); %> <center><table border="0" width="100%" id="table"> <tr> <td width="128"><p>此处假设无图片</p> <p>此处假设无等级信息</p> </td> <td ><table border="0" width="100%"> <tr> <td width="11%" >原创或转载:</td> <td colspan="3"><%=newrealese.getmyother()%> </td> <td width="10%" >新闻标题:</td> <td width="44%" ><%=newrealese.getheadtitle()%> </td> </tr> <tr> <td colspan="6">新闻内容:</td> </table> <table width="100%" border="0"> <tr> <td> <a target="_blank" href="newrealese_detail.jsp?newsid=<%=newrealese.getnewsid()%>"/> <table width="100%" border="0" align="center"> <tr> <td> <%=newrealese.getcontent()%> </td> </tr> </table> </td> </tr> </table> <table border="0" width="100%"> <tr> <td width="51" height="30">作者:</td> <td width="203"><%=newrealese.getauthor()%> </td> <td width="97">新闻发布时间:</td> <td width="167"><%=newrealese.getnewstime()%> </td> <td width="99">新闻点击次数:</td> <td width="191"><%=newrealese.gethits()%> </td> </tr> </table></td> </tr> <tr> <td height="21" colspan="2"><hr></td> </tr> </table></center> <% } %> </td> <td> </td> </tr> <tr> <td> </td> <td> </td> <td> </td> </tr> </table> </body> </html>
(3)详细新闻页面的设计,命名为newrealese_detail.jsp其代码如下所示:再详细新闻页面上,我们能够进行数据的详细情况的了解,在观看完详细信息之后,我们能够进行点击返回按钮进行返回操作
<%@page contenttype="text/html" import="java.util.*,java.sql.*" pageencoding="utf-8" language="java"%> <%@page import="com.lut.beans.newsrealese" %> <%@page import="dao.newsrealesedao" %> <!doctype html> <html> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8"> <link type="text/css" rel="stylesheet" href="newsrealese.css"/> <title>新闻发布系统--最前沿的时尚信息、最有看点的社会聚焦、最值得浪漫的殿堂</title> </head> <body id="body"> <h1>最新资讯</h1><br> <% string newsid=request.getparameter("newsid"); int newsid_int=integer.parseint(newsid); newsrealesedao newsrealesedao=new newsrealesedao(); arraylist newsrealese=newsrealesedao.queryonenews(newsid_int); %> <table border="1"> <tr > <td >新闻编号</td> <td>所属新闻栏目编号</td> <td>所属新闻分类编号</td> <td>原创或转载</td> <td>新闻标题</td> <td>新闻内容</td> <td>相关文章</td> <td>作者</td> <td>编辑</td> <td>出处</td> <td>是否置顶</td> <td>新闻发布时间</td> <td>新闻点击次数</td> <td>新闻状态</td> <td>新闻标记</td> </tr> <% for(int i=0;i<newsrealese.size();i++) { newsrealese newrealese=(newsrealese)newsrealese.get(i); %> <tr> <td><%=newrealese.getnewsid() %> </td> <td><%=newrealese.getclassid() %> </td> <td><%=newrealese.getkindid() %> </td> <td><%=newrealese.getmyother() %> </td> <td><%=newrealese.getheadtitle()%> </td> <td><%=newrealese.getcontent() %> </td> <td><%=newrealese.getconnectrealtive()%></td> <td><%=newrealese.getauthor() %> </td> <td><%=newrealese.geteditor() %> </td> <td><%=newrealese.getnewsfrom() %> </td> <td><%=newrealese.gettop() %> </td> <td><%=newrealese.getnewstime() %> </td> <td><%=newrealese.gethits() %> </td> <td><%=newrealese.getstate() %> </td> <td><%=newrealese.gettag() %> </td> </tr> </table> <table width="100%" border="0" cellspacing="0" cellpadding="4"> <tr> <td width="100%" style="text-align: center; vertical-align: top"> <table width="90%" border="0" cellpadding="0" cellspacing="0"> <tr> <td> <table width="100%" border="0" cellspacing="1" cellpadding="4" > <tr> <th width="26%" class="titlecolor" style="text-align: right; vertical-align: top" scope="row">新闻标题: </th> <td width="74%" style="vertical-align: top" class="titlecolor" ><p align="left"><%=newrealese.getheadtitle()%><p></td> </tr> <tr> <th scope="row" class="titlecolor" style="text-align: right; vertical-align: top">作者:</th> <td style="vertical-align: top"><p align="left"><%=newrealese.getauthor() %> <p> </td> </tr> <tr> <th scope="row" class="titlecolor" style="text-align: right; vertical-align: top">新闻发布时间: </th> <td style="vertical-align: top" class="titlecolor"><p align="left"><%=newrealese.getnewstime() %><p></td> </tr> <tr> <th scope="row" class="titlecolor" style="text-align: right; vertical-align: top">新闻点击次数: </th> <td style="vertical-align: top" class="titlecolor"><p align="left"><%=newrealese.gethits() %><p></td> </tr> <tr> <td colspan="2" class="storycontentcolor"><p align="center"><%=newrealese.getcontent() %><p></td> </tr> </table> </td> </tr> </table> <p> </p> <table> <tr> <td > <a href="newrealese_brief.jsp">返回</a> </td> <td> <a href="#">发表评论</a> </td> </tr> </table> </td> </tr> </table> <table width="100%" border="0" cellspacing="0" cellpadding="4"> <tr style="text-align: right"> <td> <a href="#">点赞</a> <a href="#">举报</a> <a href="#">投稿,联系作者</a> </td> </tr> </table> <% } %> </body> </html>
(4)管理员登录界面,命名为adminlogin.jsp页面,使用连接数据库的方式进行登录,在这里我们的管理员名称为scott,密码为wjk139,管理员登录界面的实现代码如下所示
<%@ page contenttype="text/html; charset=gb2312" language="java" import="java.sql.*" errorpage="" %> <html> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> <title>新闻发布系统--最前沿的时尚信息、最有看点的社会聚焦、最值得浪漫的殿堂</title> <link type="text/css" rel="stylesheet" href="newsrealese.css"/> </head> <body id="body"> <form method="post" action="checklogin"> <table border="0" align="center" id="table_vertical" > <caption>管理员登录 </caption> <tr> <td width="40">admin</td> <td width="183"> <input type="text" name="user"></td> </tr> <tr> <td height="25">pass</td> <td><input type="password" name="pass"></td> </tr> </table> <table align="center"> <tr> <td> <input type="submit" value="login"> <input type="reset" value="reset"> </td> </tr> </table> </form> </body> </html>
在登录的过程中,我们需要进行判断,命名为checklogin.java,,通过此类进行判断登录是否成功,若成功,则跳转到管理界面,若失败,则停留在当前界面,其代码如下所示:
/* * to change this license header, choose license headers in project properties. * to change this template file, choose tools | templates * and open the template in the editor. */ package servlet; import com.lut.beans.newsrealese; import dao.newsrealesedao; import java.io.ioexception; import java.io.printwriter; import java.util.logging.level; import java.util.logging.logger; import javax.servlet.servletexception; import javax.servlet.http.httpservlet; import javax.servlet.http.httpservletrequest; import javax.servlet.http.httpservletresponse; public class checklogin extends httpservlet { protected void processrequest(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { response.setcontenttype("text/html;charset=utf-8"); string user=request.getparameter("user"); string pass=request.getparameter("pass"); newsrealesedao newsrealesedao=new newsrealesedao(); try { string newsrealese=newsrealesedao.ischecked(user,pass); if(newsrealese!=null) { response.sendredirect("adminmanager.jsp"); } else { response.sendredirect("adminlogin.jsp"); } } catch (exception ex) { logger.getlogger(checklogin.class.getname()).log(level.severe, null, ex); } } @override protected void doget(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { processrequest(request, response); } @override protected void dopost(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { processrequest(request, response); } @override public string getservletinfo() { return "short description"+"public string getservletinfo() "; }// </editor-fold> }
(5)管理员登陆之后的界面,命名为adminmanager.jsp页面,在管理员界面能够进行删除修改增加和查询操作,其代码如下所示:
<%-- document : adminmanager created on : 2016-5-18, 17:10:01 author : administrator --%> <%@page import="java.util.arraylist"%> <%@page import="com.lut.beans.newsrealese"%> <%@page import="dao.newsrealesedao"%> <%@page contenttype="text/html" pageencoding="utf-8"%> <!doctype html> <html> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8"> <title>管理员界面||adminmanager.jsp</title> <link type="text/css" rel="stylesheet" href="newsrealese.css"/> </head> <body id="body" style=" text-orientation: vertical-right"> <h1 id="p">新闻管理</h1><br> <table border="1" style="width:100%; height:100%"> <tr> <td width="10%" style="text-align:top"> <table border="0" style="width: 100%;height: 100%" > <tr> <td><div align="center">新闻信息显示 </div></td> </tr> <tr> <td><img src="./images/dog.jpg" width="100%" height="90%"></td> </tr> <tr> <td><img src="./images/minus.gif" ></td> </tr> </table></td> <td width="50%"> <table border="0"> <tr> <td> <% newsrealesedao newsrealesedao = new newsrealesedao(); arraylist newsrealese = newsrealesedao.queryallnews(); %> <% for (int i = 0; i < newsrealese.size(); i++) { newsrealese newrealese = (newsrealese) newsrealese.get(i); %> <table border="0" width="100%" id="table"> <tr> <td width="128"><p>此处假设无图片</p> <p>此处假设无等级信息</p> </td> <td ><table border="0" width="100%"> <tr> <td >原创或转载:</td> <td ><%=newrealese.getmyother()%> </td> <td >新闻标题:</td> <td ><%=newrealese.getheadtitle()%> </td> </tr> <tr> <td colspan="6">新闻内容:</td> </table> <table width="100%" border="0"> <tr> <td> <a target="_blank" href="newrealese_detail.jsp?newsid=<%=newrealese.getnewsid()%>"/> <table width="100%" border="0" align="center"> <tr> <td> <%=newrealese.getcontent()%> </td> </tr> </table> </td> </tr> </table> <table border="0" width="100%"> <tr> <td width="51" height="30">作者:</td> <td width="203"><%=newrealese.getauthor()%> </td> <td width="97">新闻发布时间:</td> <td width="167"><%=newrealese.getnewstime()%> </td> <td width="99">新闻点击次数:</td> <td width="191"><%=newrealese.gethits()%> </td> </tr> </table></td> </tr> <tr> <td height="21" colspan="2"> <input type="button" name="delete" value="删除" align="middle" onclick="javascrtpt:window.location.href = 'deleteonenews?newsid=<%=newrealese.getnewsid()%>'"> <input type="button" name="delete" value="修改" align="middle" onclick="javascrtpt:window.location.href = 'a_updatenews.jsp?newsid=<%=newrealese.getnewsid()%>'"> <hr> </td> </tr> </table> <% } %> </td> </tr> </table> </td> <td width="5%" style=" text-orientation: vertical-right"> <%@include file="a_querynews.jsp" %> </td> </tr> <tr> <td align="center"><a href="a_addnews.jsp" target="_blank">添加新闻</a></td> <td align="center">二</td> <td align="center">三</td> </tr> </table> </body> </html>
(6)增加新闻页面,命名为a_addnews.jsp,能够进行新闻的增加,在添加完信息之后,我们点击发布之后就可以成功添加信息,其代码如下所示,增加新闻时需要对新闻进行编辑,对于长篇幅新闻内容且含有图片的新闻内容的编写,在这儿我们使用功能强大的百度编辑器,首先从官网下载:百度编辑器下载,下载之后解压(我使用的版本是ueditor1_4_3_2-utf8-jsp),如下图所示结构
图10 百度富文本编辑器文件夹结构图
然后只需将整个文件夹拷贝到项目下即可,在使用时引入和修改路径,在jsp页面里边我已经配置好引用,只需修改路径即可,修改路径时只需将editor_config.js中查找url变量配置编辑器在你项目中的路径。其中./ueditor为项目中的文件夹
var url= window.ueditor_home_url||"./ueditor/";
这样就配置好了编辑器,下面就是增加新闻的jsp页面,在添加完新闻之后,只需点击发布即可进行新闻的发布操作,代码如下所示:
<%@page contenttype="text/html" import="java.util.*,java.sql.*" pageencoding="utf-8" language="java"%> <%@page import="com.lut.beans.newsrealese" %> <%@page import="dao.newsrealesedao" %> <!doctype html> <html> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8"> <title>添加新闻- 新闻发布系统--最前沿的时尚信息、最有看点的社会聚焦、最富得浪漫的殿tang</title> <!-- 配置文件 --> <script type="text/javascript" src="./ueditor/ueditor.config.js"></script> <!-- 编辑器源码文件 --> <script type="text/javascript" src="./ueditor/ueditor.all.js"></script> <link rel="stylesheet" type="text/css" href="./udeditor/themes/default/css" /> <link type="text/css" rel="stylesheet" href="newsrealese.css"/> </head> <body id="body"> <form action="insertonenews" method="post"> <table border="0" id="table"> <tr> <td width="5%">left</td> <td width="90%"> <table border="1" id="table"> <tr > <td >新闻编号</td> <td><input type="text" name="newsid"></td> <td>所属新闻栏目编号</td> <td><input type="text" name="classid"></td> </tr> <tr> <td>所属新闻分类编号</td> <td><input type="text" name="kindid"></td> <td>原创或转载</td> <td><input type="text" name="myother"></td> </tr> <tr> <td>新闻标题</td> <td><input type="text" name="headtitle"></td> <td>相关文章</td> <td><input type="text" name="connectrealtive"></td> </tr> <tr> <td>作者</td> <td><input type="text" name="author"></td> <td>编辑</td> <td><input type="text" name="editor"></td> </tr> <tr> <td>出处</td> <td><input type="text" name="newsfrom"></td> <td>是否置顶</td> <td><input type="text" name="top"></td> </tr> <tr> <td>新闻发布时间</td> <td><input type="text" name="newstime"></td> <td>新闻点击次数</td> <td><input type="text" name="hits"></td> </tr> <tr> <td>新闻状态</td> <td><input type="text" name="state"></td> <td>新闻标记</td> <td><input type="text" name="tag"></td> </tr> <tr> </table> </td> <td width="5%">right</td> </tr> <tr> <td> </td> <td> <div align="center" style="width:80%"> <textarea id="newseditor" name="content" style="height: 80%"></textarea> <br/> <input type="submit" value="发 布"> <script type="text/javascript"> ue.geteditor('newseditor'); // var content = ue.getplaintxt();//content就是编辑器的带格式的内容 //focus时自动清空初始化时的内容 </script> </div> </td> <td> </td> </tr> </table> </form> </body> </html>
在增加新闻时,需要对servlet进行配置,如下图所示所示为配置文件的结构:
插入数据servlet结构图
下面是inseronenews.java代码,能够进行对数据的插入功能
/* * to change this license header, choose license headers in project properties. * to change this template file, choose tools | templates * and open the template in the editor. */ package servlet; import dao.newsrealesedao; import java.io.ioexception; import java.io.printwriter; import java.util.arraylist; import java.util.logging.level; import java.util.logging.logger; import javax.servlet.servletexception; import javax.servlet.http.httpservlet; import javax.servlet.http.httpservletrequest; import javax.servlet.http.httpservletresponse; public class insertonenews extends httpservlet { protected void processrequest(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { response.setcontenttype("text/html;charset=utf-8"); try (printwriter out = response.getwriter()) { arraylist addnews_list = new arraylist(); newsrealesedao newsrealesedao = new newsrealesedao(); addnews_list.add(0, request.getparameter("newsid")); addnews_list.add(1, request.getparameter("classid")); addnews_list.add(2, request.getparameter("kindid")); addnews_list.add(3, request.getparameter("myother")); addnews_list.add(4, request.getparameter("headtitle")); addnews_list.add(5, request.getparameter("content")); addnews_list.add(6, request.getparameter("connectrealtive")); addnews_list.add(7, request.getparameter("author")); addnews_list.add(8, request.getparameter("editor")); addnews_list.add(9, request.getparameter("newsfrom")); addnews_list.add(10, request.getparameter("top")); addnews_list.add(11, request.getparameter("newstime")); addnews_list.add(12, request.getparameter("hits")); addnews_list.add(13, request.getparameter("state")); addnews_list.add(14,request.getparameter("tag")); try { string newsrealese = newsrealesedao.insertonenews(addnews_list); system.out.println("string:"+newsrealese); if (newsrealese!= null) { response.sendredirect("adminmanager.jsp"); } else { response.sendredirect("a_addnews.jsp"); } } catch (exception ex) { logger.getlogger(checklogin.class.getname()).log(level.severe, null, ex); } } } @override protected void doget(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { processrequest(request, response); } @override protected void dopost(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { processrequest(request, response); } @override public string getservletinfo() { return "short description"; } }
至此,我们完整的实现了插入工作 (7)现在完成删除操作,在servlet包中建立如下所示代码,命名为deleteonenews.java:
/* * to change this license header, choose license headers in project properties. * to change this template file, choose tools | templates * and open the template in the editor. */ package servlet; import dao.newsrealesedao; import java.io.ioexception; import java.io.printwriter; import java.util.arraylist; import java.util.logging.level; import java.util.logging.logger; import javax.servlet.servletexception; import javax.servlet.http.httpservlet; import javax.servlet.http.httpservletrequest; import javax.servlet.http.httpservletresponse; public class deleteonenews extends httpservlet { protected void processrequest(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { response.setcontenttype("text/html;charset=utf-8"); try (printwriter out = response.getwriter()) { string str_id=request.getparameter("newsid"); int newsid_int=integer.valueof(str_id); newsrealesedao newsrealesedao=new newsrealesedao(); try { string newsrealese = newsrealesedao.deleteonenews(newsid_int); system.out.println("string:"+newsrealese); if (newsrealese!= null) { response.sendredirect("adminmanager.jsp"); } else { response.sendredirect("a_addnews.jsp"); } } catch (exception ex) { logger.getlogger(checklogin.class.getname()).log(level.severe, null, ex); } } } @override protected void doget(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { processrequest(request, response); } /** * handles the http <code>post</code> method. * * @param request servlet request * @param response servlet response * @throws servletexception if a servlet-specific error occurs * @throws ioexception if an i/o error occurs */ @override protected void dopost(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { processrequest(request, response); } /** * returns a short description of the servlet. * * @return a string containing servlet description */ @override public string getservletinfo() { return "short description"; }// </editor-fold> }
(8)实现查询操作,命名为a_querynews.jsp,其代码如下所示:
<%-- document : a_querynews created on : 2016-5-18, 17:08:40 author : administrator --%> <%@page contenttype="text/html" pageencoding="utf-8"%> <!doctype html> <html> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8"> <title>查询页面</title> </head> <body> <table style="width:100%;height:100%" border="0"> <tr> <td> <form action="queryonenews" method="post" target="_blank" > 请输入新闻编号:<input type="text" name="query"> <input type="submit" value="查询" ><br> </form> <hr></td> </tr> <tr> <td> </td> </tr> </table> </body> </html>
对于查询这件事,有成功,有失败,在这儿我们进行失败后文件的设置,代码如下所示,命名为a_querynewsfailed.jsp,使用超级连接的方式,连接到主页面
<%-- document : a_queryneesfailed created on : 2016-6-6, 8:43:35 author : administrator --%> <%@page contenttype="text/html" pageencoding="utf-8"%> <!doctype html> <html> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8"> <title>查询失败</title> </head> <body> <h1>没有找到你要查找的内容,但是你可以进入</h1> <a href="index.html">主页进行查看</a> </body> </html>
实现了以上显示层的操作之后,下面就是控制层的实现,命名为queryonenews.java,其代码如下所示:
/* * to change this license header, choose license headers in project properties. * to change this template file, choose tools | templates * and open the template in the editor. */ package servlet; import dao.newsrealesedao; import java.io.ioexception; import java.io.printwriter; import java.util.arraylist; import java.util.logging.level; import java.util.logging.logger; import javax.servlet.servletexception; import javax.servlet.http.httpservlet; import javax.servlet.http.httpservletrequest; import javax.servlet.http.httpservletresponse; public class queryonenews extends httpservlet { protected void processrequest(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { response.setcontenttype("text/html;charset=utf-8"); try (printwriter out = response.getwriter()) { string str_id=request.getparameter("query"); int newsid_int=integer.valueof(str_id); newsrealesedao newsrealesedao=new newsrealesedao(); try { arraylist newsrealese = newsrealesedao.queryonenews(newsid_int); char [] s1=newsrealese.tostring().tochararray(); system.out.println("string+newsrealese:"+newsrealese); system.out.println("string+newsrealese:"+s1[0]+" -------"+s1[1]); if(s1[0]=='['&&s1[1]==']') { response.sendredirect("a_querynewsfailed.jsp"); } else { response.sendredirect("newrealese_detail.jsp?newsid="+str_id); } } catch (exception ex) { logger.getlogger(checklogin.class.getname()).log(level.severe, null, ex); } } } @override protected void doget(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { processrequest(request, response); } @override protected void dopost(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { processrequest(request, response); } @override public string getservletinfo() { return "short description"; } }
(9)下面实现更新数据的操作,命名为a_updatenews.jsp,其代码如下所示:
<%@page contenttype="text/html" import="java.util.*,java.sql.*" pageencoding="utf-8" language="java"%> <%@page import="com.lut.beans.newsrealese" %> <%@page import="dao.newsrealesedao" %> <!doctype html> <html> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8"> <title>更改新闻-</title> <!-- 配置文件 --> <script type="text/javascript" src="./ueditor/ueditor.config.js"></script> <!-- 编辑器源码文件 --> <script type="text/javascript" src="./ueditor/ueditor.all.js"></script> <link rel="stylesheet" type="text/css" href="./udeditor/themes/default/css" /> <link type="text/css" rel="stylesheet" href="newsrealese.css"/> </head> <body id="body"> <% newsrealesedao newsrealesedao=new newsrealesedao(); string newsid_s=request.getparameter("newsid"); int news_int=integer.valueof(newsid_s); arraylist news_list=newsrealesedao.queryonenews(news_int); %> <% for(int i=0;i<news_list.size();i++) { newsrealese newrealese=(newsrealese)news_list.get(i); %> <form action="updateonenews" method="post"> <table border="0" id="table" style="width:95%"> <tr> <td width="5%">left</td> <td width="90%"> <table border="1" id="table" style="width:100%"> <tr > <tr> <td >新闻编号</td> <td><input type="text" width="100%" name="newsid" value="<%=newrealese.getnewsid() %>"></td> <td>所属新闻栏目编号</td> <td><input type="text" width="100%" name="classid" value="<%=newrealese.getclassid() %>"></td> </tr> <tr> <td>所属新闻分类编号</td> <td><input type="text" width="100%" name="kindid" value="<%=newrealese.getkindid() %>"></td> <td>原创或转载</td> <td><input type="text" width="100%" name="myother" value="<%=newrealese.getmyother() %>"></td> </tr> <tr> <td>新闻标题</td> <td><input type="text" width="100%" name="headtitle" value="<%=newrealese.getheadtitle()%>"></td> <td>相关文章</td> <td><input type="text" width="100%" name="connectrealtive" value="<%=newrealese.getconnectrealtive()%>"></td> </tr> <tr> <td>作者</td> <td><input type="text" width="100%" name="author" value="<%=newrealese.getauthor() %>"></td> <td>编辑</td> <td><input type="text" width="100%" name="editor" value="<%=newrealese.geteditor() %>"></td> </tr> <tr> <td>出处</td> <td><input type="text" width="100%" name="newsfrom" value="<%=newrealese.getnewsfrom() %>"></td> <td>是否置顶</td> <td><input type="text" width="100%" name="top" value="<%=newrealese.gettop() %>"></td> </tr> <tr> <td>新闻发布时间</td> <td><input type="text" width="100%" name="newstime" value="<%=newrealese.getnewstime() %> "></td> <td>新闻点击次数</td> <td><input type="text" width="100%" name="hits" value="<%=newrealese.gethits() %>"></td> </tr> <tr> <td>新闻状态</td> <td><input type="text" name="state" width="100%" value="<%=newrealese.getstate() %>"></td> <td>新闻标记</td> <td><input type="text" width="100%" name="tag" value="<%=newrealese.gettag() %>"></td> </tr> <tr> </table> </td> <td width="5%">right</td> </tr> <tr> <td> </td> <td> <div align="center" style="width:80%"> <textarea id="newseditor" name="content" style="height: 80%"><%=newrealese.getcontent() %></textarea> <br/> <input type="submit" value="发 布"> <script type="text/javascript"> ue.geteditor('newseditor'); var content=ue.getplaintxt();//s就是编辑器的带格式的内容 </script> </div> </td> <td> </td> </tr> </table> </form> <% } %> </body> </html>
在servlet中进行如下代码所示配置:
/* * to change this license header, choose license headers in project properties. * to change this template file, choose tools | templates * and open the template in the editor. */ package servlet; import dao.newsrealesedao; import java.io.ioexception; import java.io.printwriter; import java.util.arraylist; import java.util.logging.level; import java.util.logging.logger; import javax.servlet.servletexception; import javax.servlet.http.httpservlet; import javax.servlet.http.httpservletrequest; import javax.servlet.http.httpservletresponse; public class updateonenews extends httpservlet { protected void processrequest(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { response.setcontenttype("text/html;charset=utf-8"); try (printwriter out = response.getwriter()) { arraylist addnews_list = new arraylist(); newsrealesedao newsrealesedao = new newsrealesedao(); addnews_list.add(0, request.getparameter("newsid")); addnews_list.add(1, request.getparameter("classid")); addnews_list.add(2, request.getparameter("kindid")); addnews_list.add(3, request.getparameter("myother")); addnews_list.add(4, request.getparameter("headtitle")); addnews_list.add(5, request.getparameter("content")); addnews_list.add(6, request.getparameter("connectrealtive")); addnews_list.add(7, request.getparameter("author")); addnews_list.add(8, request.getparameter("editor")); addnews_list.add(9, request.getparameter("newsfrom")); addnews_list.add(10, request.getparameter("top")); addnews_list.add(11, request.getparameter("newstime")); addnews_list.add(12, request.getparameter("hits")); addnews_list.add(13, request.getparameter("state")); addnews_list.add(14,request.getparameter("tag")); try { string newsrealese = newsrealesedao.updateonenews(addnews_list); system.out.println("string:"+newsrealese); if (newsrealese!= null) { response.sendredirect("adminmanager.jsp"); } else { response.sendredirect("a_addnews.jsp"); } } catch (exception ex) { logger.getlogger(checklogin.class.getname()).log(level.severe, null, ex); } } } @override protected void doget(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { processrequest(request, response); } @override protected void dopost(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { processrequest(request, response); } @override public string getservletinfo() { return "short description"; } }
到这儿,我们已经实现了对增删改查功能的实现,当然,基本的新闻发布功能已经实现
关于管理系统的更多内容请点击《管理系统专题》进行学习
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
上一篇: python基础语法学习大纲总结