码迷,mamicode.com
首页 > Web开发 > 详细

JSP新闻显示

时间:2020-06-25 11:33:52      阅读:76      评论:0      收藏:0      [点我收藏+]

标签:exe   sele   png   jstl   dbutils   return   依赖   ext   连接数   

MYSQL数据库创建新闻表,用户登陆时使用SERVLET获取用户名,效验通过后直接跳转新闻列表页面,JSP使用EL显示新闻列表

1.首先创建数据库及用户、新闻表

CREATE DATABASE /*!32312 IF NOT EXISTS*/`NewsDB` /*!40100 DEFAULT CHARACTER SET utf8 */;

USE `NewsDB`;

/*Table structure for table `news` */

DROP TABLE IF EXISTS `news`;

CREATE TABLE `news` (
  `id` INT(11) NOT NULL AUTO_INCREMENT,
  `title` VARCHAR(100) DEFAULT NULL,
  `conetnt` VARCHAR(1000) DEFAULT NULL,
  `author` VARCHAR(50) DEFAULT NULL,
  `addtime` DATETIME DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=INNODB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;

/*Data for the table `news` */

INSERT  INTO `news`(`id`,`title`,`conetnt`,`author`,`addtime`) VALUES (1,‘端午节的来源‘,‘端午节,又称端阳节、龙舟节、重午节、龙节、正阳节、天中节等,节期在农历五月初五,是中国民间的传统节日。端午节源自天象崇拜,由上古时代祭龙演变而来。仲夏端午,苍龙七宿飞升至正南中天,是龙飞天的日子,即如《易经·乾卦》第五爻的爻辞曰:“飞龙在天”。端午日龙星既“得中”又“得正”,乃大吉大利之象。端午节的起源涵盖了古老星象文化、人文哲学等方面内容,蕴含着深邃丰厚的文化内涵;在传承发展中杂揉了多种民俗为一体,节俗内容丰富。扒龙舟与食粽是端午节的两大礼俗,这两大礼俗在中国自古传承,至今不辍。‘,‘test‘,‘2020-06-25 10:19:42‘);

/*Table structure for table `user` */

DROP TABLE IF EXISTS `user`;

CREATE TABLE `user` (
  `uid` INT(11) NOT NULL AUTO_INCREMENT,
  `userName` VARCHAR(30) DEFAULT NULL,
  `pw` VARCHAR(15) DEFAULT NULL,
  PRIMARY KEY (`uid`)
) ENGINE=INNODB AUTO_INCREMENT=6 DEFAULT CHARSET=gb2312;

/*Data for the table `user` */

INSERT  INTO `user`(`uid`,`userName`,`pw`) VALUES (1,‘admin‘,‘admin‘);

2.新建web项目,项目名称为:JspNewsList   并添加JAR包依赖:mysql-connector-java-5.0.8-bin.jar  jstl.jar  standard.jar

 

 

最终项目结构为

技术图片

 

首先新增分别新增login.jsp页面用户登录,failure.jsp用户显示登录出错信息,ShowNews.jsp用于显示新闻详情,ShowNewsList.jsp用于显示新闻列表

详情代码会可以参照码云仓库代码

3.新增Servlet : NewsController.java 并配置在WEB.XML中

<servlet>
<servlet-name>NewsController</servlet-name>
<servlet-class>servlet.NewsController</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>NewsController</servlet-name>
<url-pattern>/NewsController</url-pattern>
</servlet-mapping>

在NewsController.java 中doGet中重写doPost方法并在doPost方法中获取action参数,根据参数不同分别进行不同请求

技术图片

 

 

 关键代码:

技术图片View Code

 

4.创建service类   NewsService.java,在类中调用DAO中并返回新闻信息

public class NewsService {


    public List<NewsBean> query_all_news() {
        NewsDao newsDao = new NewsDao();
        
          return newsDao.query_all_news();
    }
    
    public NewsBean getNews(String id) {
        NewsDao newsDao = new NewsDao();
        return newsDao.getNews(id);
    }
}

5.新增DBUtils.java 及 NewsDao.java类

在DBUtils.java中添加连接数据库及关闭数据库代码

public static Connection getConnection(){
        String dbUserName = "root";
        String dbUserPasswd = "123456";
        
        String dbURL = "jdbc:mysql://localhost:3406/NewsDB?serverTimezone=Asia/Shanghai";
        Connection conn = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn = (Connection) DriverManager.getConnection(dbURL,dbUserName,dbUserPasswd);
        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        } 
        return conn;
    }
    

    public static void closeConnection(Connection conn) {

        if(conn != null){
            try {
                conn.close();
            } catch (SQLException e) {
                // TODO: handle exception
                e.printStackTrace();
            }
        }
    }
public List<NewsBean> query_all_news() {
        Connection conn = DBUtils.getConnection();
        String sql = "select * from news";
        ArrayList<NewsBean> results = new ArrayList<NewsBean>();
        
        try {
            PreparedStatement ps = (PreparedStatement) conn.prepareStatement(sql);
            ResultSet rs = ps.executeQuery();

            while(rs.next()){
                
                NewsBean temp = new NewsBean();
                temp.setId(rs.getString("id"));
                temp.setTitle(rs.getString("title"));
                temp.setConetnt(rs.getString("conetnt"));
                temp.setAuthor(rs.getString("author"));
                temp.setAddtime(rs.getDate("addtime"));
                results.add(temp);
            }
            rs.close();
            ps.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DBUtils.closeConnection(conn);
        }
        return results;
    }
    
    public NewsBean getNews(String id) {
        Connection conn = DBUtils.getConnection();
        String sql = "select * from news where id=‘"+id+"‘ ";
        NewsBean result = new NewsBean();
        
        try {
            PreparedStatement ps = (PreparedStatement) conn.prepareStatement(sql);
            ResultSet rs = ps.executeQuery();

            while(rs.next()){

                result.setId(rs.getString("id"));
                result.setTitle(rs.getString("title"));
                result.setConetnt(rs.getString("conetnt"));
                result.setAuthor(rs.getString("author"));
                result.setAddtime(rs.getDate("addtime"));
            }

            rs.close();
            ps.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DBUtils.closeConnection(conn);
        }
        return result;
    }

 

6.前端显示页面

<c:forEach items="${newsList }" var="ctxt">
            <tr>
                <td>
                    ${ctxt.title } </td>
                   <td> ${ctxt.author } </td>
                   <td> ${ctxt.addtime } </td>
                    <td><a href="/JspNewsList/NewsController?action=toShowNews&id=${ctxt.id }">查看详情</a></td>
            
          
            </tr>        
            </c:forEach>    

 

最后全部源码放在   https://gitee.com/lg_ui/JspNewsList.git  请有需要的同学参照

 

JSP新闻显示

标签:exe   sele   png   jstl   dbutils   return   依赖   ext   连接数   

原文地址:https://www.cnblogs.com/beginner1/p/13191363.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!