新闻web小酌

首页如上
类图如下:

添加新闻的方法(dao):
public boolean Add(News news) {
boolean flag=false;
Connection con =getConnectionData();
QueryRunner queryRunner = new QueryRunner();
String sql="INSERT INTO newscon(newstype,title,author,abstrac ,content,picture) VALUE(?,?,?,?,?,?)";
Object[] paras={news.getNewstype(),news.getTitle(),news.getAuthor(),news.getAbstrac(),news.getContent(),news.getUrl()};
int count=0;
try {
count= queryRunner.update(con,sql,paras);
} catch (SQLException e) {
throw new RuntimeException(e.getMessage(),e);
}
closeAll();
if(count>0){
flag=true;
}
return flag;
}

servlet方法:
public class AddNews 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 {
request.setCharacterEncoding("utf-8");
if(request.getMethod().equals("GET")){
request.getRequestDispatcher("/newspages/news_add.jsp").forward(request,
response);
}else {
String path = request.getContextPath();
String basePath = request.getScheme() + "://" + request.getServerName()
+ ":" + request.getServerPort() + path + "/";
response.setContentType("text/html;charset=UTF-8");
request.setCharacterEncoding("UTF-8");
HttpSession session = request.getSession();
boolean flag=false;
try {
List<News> list = (List<News>) FormUtil.assembleObjectList(request,News.class);
News news = list.get(0);
NewsDaoimpl dao=new NewsDaoimpl();
flag = dao.Add(news);
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
if (flag) {
response.sendRedirect(path);
}else {
request.getRequestDispatcher("/index.jsp").forward(request,
response);
}
}
}
}
插入标题:

public class AddTitleServlet 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 {
request.setCharacterEncoding("utf-8");
if (request.getMethod().equals("GET")) {
request.getRequestDispatcher("/newspages/news_add.jsp").forward(
request, response);
} else {
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
response.setContentType("text/html;charset=UTF-8");
request.setCharacterEncoding("UTF-8");
HttpSession session = request.getSession();
boolean flag = false;
try {
List<Topic> list = (List<Topic>) FormUtil.assembleObjectList(
request, Topic.class);
Topic topic = list.get(0);
NewsDaoimpl dao = new NewsDaoimpl();
flag = dao.AddTitle(topic);
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
if (flag) {
response.sendRedirect(path);
} else {
request.getRequestDispatcher("/index.jsp").forward(request,
response);
}
}
}
}
标题的动态加载:

index:
<c:forEach var="item" items="${requestScope.Toplist}">
<a style="color:blue;font-size:16px;" href='${pageContext.request.contextPath }/DoIndexServlet?id=${item.nid}'>
${item.newstype}
</a>
</c:forEach>
</li>
</ul>
<ul class="classlist">
<c:forEach var="item" items="${requestScope.newsList }">
<li><a href='newspages/news_read.jsp'>${item.title }</a>
<span>
<c:forEach var="item1" items="${TimeList}">
${item1.beginDatetime}
</c:forEach>
</span>
</li>
</c:forEach>
impl:
public List<Topic> AllTitle() {
Connection con=getConnectionData();
String sql="select nid,newstype from type";
QueryRunner query=new QueryRunner();
List<Topic> list=null;
try {
list= query.query(con, sql,new BeanListHandler<Topic>(Topic.class));
} catch (SQLException e) {
new RuntimeException(e.getMessage());
}
return list;
}
新闻web小酌的更多相关文章
- Java web 小测验
题目要求: 1登录账号:要求由6到12位字母.数字.下划线组成,只有字母可以开头:(1分) 2登录密码:要求显示“• ”或“*”表示输入位数,密码要求八位以上字母.数字组成.(1分) 3性别:要求用单 ...
- 张小龙的野心:用小程序重构web|小程序好处及可能的不足
一:张小龙的野心:用小程序重构web 一 很多年以前,张小龙写了一款软件:Foxmail. 这款软件当年有数百万用户,这是一个相当庞大的量,因为彼时网民也只有千万当量级的规模. 我是一个非常忠实的用户 ...
- WEB 小案例 -- 网上书城(四)
针对于这个小案例我们今天讲解结账操作,也是有关这个案例的最后一次博文,说实话这个案例的博文写的很糟糕,不知道该如何去表述自己的思路,所以内容有点水,其实说到底还是功力不够. 处理思路 点击结账,发送结 ...
- web小知识点
写在前面: 该篇是个人平时对web前端小知识点的总结,会不定时更新...... 如有错误,敬请批评指正. 正文: 1.WWW是World Wide Web的缩写. 2.HTML(Hyper Text ...
- go语言实现一个简单的登录注册web小程序
最近学习golang也有一段时间了,基础差不多学了个大概,因为本人是java程序员,所以对web更感兴趣.根据<go web编程>中的例子改编一个更简单的例子,供新手参考,废话不多说,上菜 ...
- 【WEB小工具】EncodingFilter—设置全局编码
1.我们知道,如果是POST请求,我们需要调用request.setCharacterEncoding("utf-8") 方法来设计编码. public void doGet(Ht ...
- 【WEB小工具】BaseServlet—一个Servlet处理多个请求
package cn.itcast.test.web.servlet; import java.io.IOException; import java.io.PrintWriter; import j ...
- macaca测试web小例子
上午刚把macaca的环境在公司的电脑上吧web 端的环境给搭建好,于是乎,看看网上的例子,看看官方的文档 https://macacajs.github.io/wd.py/ 可以在这个链接看到原滋原 ...
- WEB 小案例 -- 网上书城(一)
距离上次写博客有两周了吧,最多的原因就是自己期末考试了,上课没听就只能在期末狠狠的复习了,毕竟已经挂科了.当然还是因为自己懒吧!!!废话不多说开始我们今天的正题,网上书城! 一. 新建数据表(MySQ ...
随机推荐
- Josephus2
约瑟夫问题升级问题 编号为1~N的N个人按顺时针方向围坐一圈,每个人持有一个密码(正整数,可以自由输入),开始人选一个正整数作为报数上限值M,从第一个人按顺时针方向自1开始顺序报数,报道M是停止报数. ...
- Redux1
Redux 写在前面 写React也有段时间了,一直也是用Redux管理数据流,最近正好有时间分析下源码,一方面希望对Redux有一些理论上的认识:另一方面也学习下框架编程的思维方式. Redux如何 ...
- Java中的自动拆箱装箱(Autoboxing&Unboxing)
一.基本类型打包器 1.基本类型:long.int.double.float.boolean 2.类类型:Long.Integer.Double.Float.Boolean 区别:基本类型效率更高,类 ...
- .NET EntityFramework
http://www.cnblogs.com/lsxqw2004/archive/2009/05/31/1495240.html http://www.cnblogs.com/flowwind/p/3 ...
- Tiling
地址:http://poj.org/problem?id=2506 递推公式::f[x]=f[x-1]+f[x-2]*2 需要用到大数. #include<stdio.h> #includ ...
- Squares<哈希>
Description A square is a 4-sided polygon whose sides have equal length and adjacent sides form 90-d ...
- Maven实战四
转载:http://www.iteye.com/topic/1123231 为什么要定义Maven坐标 在我们开发Maven项目的时候,需要为其定义适当的坐标,这是Maven强制要求的.在这 ...
- 【算法Everyday】第二日 求子数组的最大和
题目 // 3.求子数组的最大和 // 题目: // 输入一个整形数组,数组里有正数也有负数. // 数组中连续的一个或多个整数组成一个子数组,每个子数组都有一个和. // 求所有子数组的和的最大值. ...
- Codeforces Round #316 (Div. 2)
A. Elections time limit per test 1 second memory limit per test 256 megabytes input standard input o ...
- 更改Visual Studio 2010/2012/2008的主题设置
一.更改主题: 主题网站:http://studiostyl.es/ Visual Studio 2010发布也已经有一段时间了,不过安装后默认的白底的主题长时间看代码可能会感觉眼睛酸痛,况且时间长了 ...