博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
JSP丶新闻发布会系统
阅读量:6821 次
发布时间:2019-06-26

本文共 8054 字,大约阅读时间需要 26 分钟。

新闻发布会

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

 

1.实现登录功能

 

前端界面的代码

 

 

 

1 
2
3
4
5
6
7
8
9

登录实现类代码

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", "<>");26               response.sendRedirect(request.getContextPath()+"/newspages/admin.jsp");27              // out.print("");28           }else{29               System.out.print("失败!");30              request.getSession().setAttribute("xi", "<>");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
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
list = topicdao.getNewsById(tid);15 16 request.setAttribute("newsList",list);17 18 }else {19 //处理新闻相关内容20 NewDaoImpl newsDao=new NewDaoImpl();21 List
newsList = newsDao.getTopNews();22 request.setAttribute("newsList", newsList);23 }24 //转向DoIndexServlet获取数据25 26 //准发到index.jsp27 request.getRequestDispatcher("/index.jsp").forward(request, response);28 }
View Code

动态显示实现类

1 public List
getAllTopic() { 2 Connection connection=getConnection(); 3 String sqlString="select typeid,typename from type"; 4 QueryRunner query=new QueryRunner(); 5 List
list=null; 6 try { 7 8 list=query.query(connection, sqlString, new BeanListHandler
(NewsType.class)); 9 10 System.out.println(list.get(0).getTypeName());11 } catch (SQLException e) {12 // TODO Auto-generated catch block13 e.printStackTrace();14 }15 return list;16 }17 18 19 public List
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
list=null;25 try {26 list=query.query(connection, sqlString, new BeanListHandler
(News.class));27 } catch (SQLException e) {28 // TODO Auto-generated catch block29 e.printStackTrace();30 }31 return list;32 }

前端代码

1  
2
12
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           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", "<>");11           }else{12               System.out.println("");13               request.getSession().setAttribute("xi", "<>");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

 

  

 

转载于:https://www.cnblogs.com/PGYXZ/p/4996244.html

你可能感兴趣的文章
Zing 5.0发布,包含原生支持Linux的无停顿垃圾回收器
查看>>
解决Fiddler不能监听Java HttpURLConnection请求的方法
查看>>
串的模式匹配算法---Horspool
查看>>
Python基础07 函数
查看>>
Android学习
查看>>
Windows Phone 7 EKB系列文章发布
查看>>
Jmeter(六)-关联设置
查看>>
Mvc 3 中的分部视图
查看>>
android学习笔记之ProgressDialog的使用
查看>>
hdu 4557 非诚勿扰 vector的应用 原来vector 可以删除指定位置元素 不过消耗大
查看>>
tomcat支持中文文件名下载
查看>>
PHP 时区设置
查看>>
UVALive 5790 Ball Stacking 解题报告
查看>>
深入redis内部--事件处理机制
查看>>
[再寄小读者之数学篇](2014-05-23 $\ln x-ax=0$ 有两个根时的估计)
查看>>
(弃)解读Openstack Identity API v2.0
查看>>
Linux目录结构
查看>>
微信分享JS接口失效说明及解决方案
查看>>
ThinkPHP项目笔记之RBAC(权限)下篇
查看>>
log4j配置具体解释
查看>>