基于jsp的新闻发布系统

Posted suwu150

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了基于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账户
            //1、注册驱动 
            Class.forName("oracle.jdbc.driver.OracleDriver");
            //2、url地址
            String url = "jdbc:oracle:thin:@localhost:1521:orcl";
           //3、获取连接
            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字符串,所以executeQuery是无参的。              
            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字符串,所以executeQuery是无参的。              
            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(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
            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();
            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页面跳转到NewRealese_brief.jsp页面,其实这里的跳转功能使用的是javascript中的window对象进行跳转的),代码如下所示:
<!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,,通过此类进行判断登录是否成功,若成功,则跳转到管理界面,若失败,则停留在当前界面,其代码如下所示:
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() ";
    

(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" 
                                                   οnclick="javascrtpt:window.location.href = 'DeleteOneNews?newsid=<%=newRealese.getNewsId()%>'"> 
                                            <input type="button" name="delete" value="修改"  align="middle"  
                                                   οnclick="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代码,能够进行对数据的插入功能
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:
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";
    

(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,其代码如下所示:
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

以上是关于基于jsp的新闻发布系统的主要内容,如果未能解决你的问题,请参考以下文章

java+jsp基于ssm多用户新闻发布管理系统

基于java+jsp+ssm足球新闻发布网站

基于jsp+servlet的新闻管理系统。

基于SSM的新闻发布系统的设计与实现文末(文末附源码论文)

基于SSM实现新闻发布系统

新闻发布系统