zoukankan      html  css  js  c++  java
  • JSP丶新闻发布会系统

    新闻发布会

    项目所需要的一些实现类 servlet 工具类

    1.实现登录功能

    前端界面的代码

    1 <form action="<%=path %>/LonginServlet" method="post">
    2       <label> 登录名 </label>
    3       <input type="text" name="uname" value='<%=request.getParameter("uname")==null?"":request.getParameter("uname") %>' class="login_input" />
    4       <label> 密&#160;&#160;码 </label>
    5       <input type="password" name="upwd" value='<%=request.getParameter("upwd")==null?"":request.getParameter("upwd") %>' class="login_input" />
    6       <input type="submit" class="login_sub" value="登录" />
    7       <label id="error"> </label>
    8       <img src="images/friend_logo.gif" alt="Google" id="friend_logo" />
    9     </form>

    登录实现类代码

     1 public boolean loginGetBool(Admin admin) {
     2       rs=  executeSelect("select *from admin where name=? and "pwd"=?",admin.getAname(),admin.getApwd());
     3       try {
     4         if(rs.next()){
     5               return true;
     6           }
     7       } catch (SQLException e) {
     8         // TODO Auto-generated catch block
     9         e.printStackTrace();
    10       }
    11       return false;
    12     }

    登录servlet

    public void doPost(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            //接收请求时的编码utf-8
              request.setCharacterEncoding("utf-8");
              response.setContentType("text/html;charset=utf-8");
              String  name=request.getParameter("uname");
              String  pwd=request.getParameter("upwd");
        
                    Admin admin=new Admin(name,pwd);
                    System.out.println(admin.getAname());
                   
                   AdminDaoImpl adi=new AdminDaoImpl();
    
                    String dbn=adi.login(admin);
                 
                    if(dbn!=null){
                         
                        Cookie cookie=new Cookie("unameCookie",name);
                        cookie.setMaxAge(60*60*24);
                        
                        response.addCookie(cookie);
                    
                        System.out.println("登陆成功!");
                        HttpSession session= request.getSession();
                        session.setAttribute("uname", name);
                        session.setMaxInactiveInterval(60*10);
                        
                        response.sendRedirect(request.getContextPath()+"/newspages/admin.jsp");
                    }else{
                        response.sendRedirect(request.getContextPath()+"/index.jsp");
                    }
        }

    2.实现新增新闻

    新增实现类方法

     1 public boolean addNews(News news) {
     2         Date date=new Date();
     3         DateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
     4         Date time = null;
     5         try {
     6             time = format.parse(format.format(date));
     7         } catch (ParseException e) {
     8             // TODO Auto-generated catch block
     9             e.printStackTrace();
    10         }
    11         
    12             Object[] obj={news.getNauthor(),news.getNcontent(),time,null,news.getNtitle(),news.getNtypeid()};
    13     
    14         
    15       
    16       return  executeUpdate("INSERT INTO newsrecord (`nauthor`,`ncontent`,`startTime`,`endUpdateTime`,`ntitle`,`ntypeid`) values(?,?,?,?,?,?)",obj);
    17         
    18       
    19     }
    View Code

    新增servlet

     1 public void doPost(HttpServletRequest request, HttpServletResponse response)
     2             throws ServletException, IOException {
     3           
     4            request.setCharacterEncoding("utf-8");
     5            response.setContentType("text/html; charset=utf-8");
     6            //主题
     7            int ntid=Integer.parseInt(request.getParameter("ntid"));
     8          //标题
     9            String ntitle=request.getParameter("ntitle");
    10           //作者 
    11           String nauthor=request.getParameter("nauthor");
    12           //摘要 
    13           String nsummary=request.getParameter("nsummary");
    14         //内容
    15           String ncontent=request.getParameter("nauthor");
    16         //上传图片
    17           
    18           
    19           String file=request.getParameter("file");  
    20           NewsWeb news=new NewsWeb(nauthor,ncontent,file,ntitle,ntid,nsummary);
    21           NewsWebDaoImpl nw=new  NewsWebDaoImpl();
    22           
    23           if(nw.addNewsWeb(news)){
    24               System.out.print("成功!");
    25               request.getSession().setAttribute("xi", "<<script type="text/javascript">+alert("新增新闻成功!") </script>>");
    26               response.sendRedirect(request.getContextPath()+"/newspages/admin.jsp");
    27              // out.print("");
    28           }else{
    29               System.out.print("失败!");
    30              request.getSession().setAttribute("xi", "<<script type="text/javascript">+alert("新增新闻失败!") </script>>");
    31               response.sendRedirect(request.getContextPath()+"/newspages/admin.jsp");
    32           }
    新增Servlet
    动态显示新闻标题内容
     
    动态显示Servlet
     1 public void doPost(HttpServletRequest request, HttpServletResponse response)
     2             throws ServletException, IOException {
     3             TopicDaoImpl dao=new    TopicDaoImpl();
     4             
     5             List<NewsType> alltopic=dao.getAllTopic();
     6     
     7             request.setAttribute("Topiclist", alltopic);
     8             String data=request.getParameter("tid");
     9             
    10             if (data!=null&&!data.equals("")) {
    11                 int tid=Integer.parseInt(data);
    12                 
    13                 NewDaoImpl topicdao=new NewDaoImpl();
    14                 List<News> list = topicdao.getNewsById(tid);
    15                 
    16                 request.setAttribute("newsList",list);
    17                 
    18             }else {
    19                 //处理新闻相关内容
    20                 NewDaoImpl newsDao=new NewDaoImpl();
    21                 List<News> newsList = newsDao.getTopNews();
    22                 request.setAttribute("newsList", newsList);
    23             }
    24             //转向DoIndexServlet获取数据
    25             
    26             //准发到index.jsp
    27             request.getRequestDispatcher("/index.jsp").forward(request, response);
    28     }
    View Code

    动态显示实现类

     1 public List<NewsType> getAllTopic() {
     2         Connection connection=getConnection();
     3         String sqlString="select  typeid,typename from type";
     4         QueryRunner query=new QueryRunner();
     5         List<NewsType> list=null;
     6         try {
     7             
     8             list=query.query(connection, sqlString, new BeanListHandler<NewsType>(NewsType.class));
     9     
    10             System.out.println(list.get(0).getTypeName());
    11         } catch (SQLException e) {
    12             // TODO Auto-generated catch block
    13             e.printStackTrace();
    14         }
    15         return list;
    16     }
    17 
    18 
    19 public List<News> getTopNews() {
    20         Connection connection=getConnection();
    21         QueryRunner query=new    QueryRunner();
    22         //select * from newsrecord where rownum<=3   orcal查询前三条语句
    23         String sqlString="select * from newsrecord where nid limit 3";
    24         List<News> list=null;
    25         try {
    26             list=query.query(connection, sqlString, new BeanListHandler<News>(News.class));
    27         } catch (SQLException e) {
    28             // TODO Auto-generated catch block
    29             e.printStackTrace();
    30         }
    31         return list;
    32     }

    前端代码

     1  <div class="content">
     2       <ul class="class_date">
     3         <li id='class_month'>
     4               <c:forEach var="item"  items="${requestScope.Topiclist}">
     5                  <a style="color:red;font-size:14px;" href='${pageContext.request.contextPath }/DoIndexServlet?tid=${item.typeid}'><!-- 从域中取值 -->
     6                     ${item.typeName}
     7                  </a>
     8              </c:forEach>
     9         </li>
    10         
    11       </ul>
    12       <ul class="classlist">
    13             <c:forEach var="item" items="${newsList }">
    14                <li><a href='newspages/news_read.jsp'>${item.ntitle }</a><span>${item.startTime }  </span></li>
    15             </c:forEach>
    16             
    17             <p align="right"> 当前页数:[1/2]&nbsp; <a href="/DoIndexServlet">下一页</a> <a href="#">末页</a> </p>
    18           </ul>    
    19     </div>
    View Code
    添加新闻主题

    新增新闻类型Servlet
     1 public void doPost(HttpServletRequest request, HttpServletResponse response)
     2             throws ServletException, IOException {
     3 
     4           request.setCharacterEncoding("utf-8");
     5           response.setContentType("text/html; charset=utf-8");
     6           String tname=request.getParameter("tname");
     7           NewsType newsType=new NewsType(tname);
     8           NewsTypeDaoImpl ntdi=new NewsTypeDaoImpl();
     9           if (ntdi.addNewsType(newsType)) {
    10               request.getSession().setAttribute("xi", "<<script type="text/javascript">+alert("新增新闻类型成功!") </script>>");
    11           }else{
    12               System.out.println("");
    13               request.getSession().setAttribute("xi", "<<script type="text/javascript">+alert("新增新闻类型失败!") </script>>");
    14           }
    15           response.sendRedirect("/news/util/addnewstype.jsp");
    16           
    17     }
    View Code

    新增类型实现类

    1 public boolean addNewsType(NewsType newsType){    
    2       return    executeUpdate("insert into type(typename)   
    3            values(?)", newsType.getTypeName());
    4     }
    View Code

      

     
  • 相关阅读:
    大规模web服务读书笔记 狼
    MVC3如果虚拟目录中有点号,会导致静态文件404 狼
    CDN服务商和CDN常见问题 狼
    中文字段名,问题根源查询无聊话题。 狼
    NET下Session共享的几种实现方式 狼
    企业应用架构读书笔记与总结 狼
    Redis简单本机测试 狼
    你是否经历过这些,求如何继续才能提升 狼
    WinDbg配置和使用基础
    Python IDLE入门
  • 原文地址:https://www.cnblogs.com/PGYXZ/p/4996244.html
Copyright © 2011-2022 走看看