zoukankan      html  css  js  c++  java
  • jsp-新闻发布系统

    主页面:

    分层架构图:

    关键代码:控制台输出新闻内容

    实现类:

    复制代码
    复制代码
    复制代码
    package cn.news.impl;
    
    import java.sql.ResultSet;
    import java.util.ArrayList;
    import java.util.List;
    
    import org.junit.Test;
    
    import cn.news.dao.BaseDao;
    import cn.news.dao.NewsDetailDao;
    import cn.news.entity.NewsDetail;
    
    public class NewsDetailDaoImpl extends BaseDao implements NewsDetailDao{
       
        @Test
        public void test() throws Exception
        {
            List<NewsDetail> list=getAllNews();
            for (NewsDetail item : list) {
                System.out.println(item.getNewsContent());
            }
         
        }
        
        @Override
        public List<NewsDetail> getAllNews() throws Exception {
            List<NewsDetail> list=new ArrayList<NewsDetail>();
            
            String sql="select * from newsDetails";
            
            ResultSet rs=executeQuery(sql);
            
            if(rs!=null)
            {
                while(rs.next())
                {
                    NewsDetail news=new NewsDetail();
                    
                    news.setNewsId(rs.getInt("newsId"));
                    news.setNewsTitle(rs.getString("newsTitle"));
                    news.setNewsContent(rs.getString("newsContent"));
                    news.setNewsCreateDate(rs.getDate("newsCreateDate"));
                    news.setNewsAuthor(rs.getString("newsAuthor"));
                    news.setNewsCategoryId(rs.getInt("newsCategoryId"));
                    
                    list.add(news);
                }
            }
            return list;
        }
    
    }
    复制代码
    复制代码
    复制代码

    页面显示新闻标题以及时间:

    新增NewsServlet类继承HttpServlet。关键代码:

    复制代码
    复制代码
    复制代码
    package cn.news.servlet;
    
    import java.io.IOException;
    import java.io.PrintWriter;
    import java.util.List;
    
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    import cn.news.entity.NewsDetail;
    import cn.news.impl.NewsDetailDaoImpl;
    
    public class NewsServlet extends HttpServlet {
    
        /**
    
         */
        public void doGet(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
    
            doPost(request,response);
            
        }
    
        /**
        
         */
        public void doPost(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
    
            //实例化dao
            NewsDetailDaoImpl dao=new NewsDetailDaoImpl();
            
            try {
                List<NewsDetail> list=dao.getAllNews();
                
                request.setAttribute("list", list);
            } catch (Exception e) {
                e.printStackTrace();
            }
            
            request.getRequestDispatcher("/index.jsp").forward(request, response);
        }
    
    }
    复制代码
    复制代码
    复制代码

    index.jsp

    复制代码
    复制代码
    复制代码
       
          <ul class="classlist">
          
          <%
          List<NewsDetail>list=(List<NewsDetail>)request.getAttribute("list");
          
          for(NewsDetail item:list)
          {
    
          %>
          <li><a href='newspages/news_read.jsp'><%=item.getNewsTitle() %></a><span><%=item.getNewsCreateDate() %></span></li>
          
          <%
          }
          
          %>
          
        <p align="right"> 当前页数:[1/2]&nbsp; <a href="#">下一页</a> <a href="#">末页</a> </p>
          </ul>
    复制代码
    复制代码
    复制代码

    web.xml

  • 相关阅读:
    墨者-(RCE)Apache Struts2远程代码执行漏洞(S2-032)
    Apache Struts2远程代码执行漏洞(S2-015)
    墨者-(RCE)Webmin未经身份验证的远程代码执行
    MySQL注入点写入WebShell的几种方式
    墨者-rsync未授权访问漏洞利用
    墨者-Ruby On Rails漏洞复现第二题(CVE-2019-5418)
    墨者 Ruby On Rails漏洞复现第一题(CVE-2018-3760)
    SROP利用技术
    技巧点滴
    S2-045漏洞利用脚本汇总
  • 原文地址:https://www.cnblogs.com/shiguanji/p/6775836.html
Copyright © 2011-2022 走看看