程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> JSP編程 >> 關於JSP >> 基於jsp實現新聞管理系統 附完整源碼

基於jsp實現新聞管理系統 附完整源碼

編輯:關於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">新聞發布

(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">新聞發布

(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">新聞發布

(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";
 }
}


到這兒,我們已經實現了對增刪改查功能的實現,當然,基本的新聞發布功能已經實現 

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved