新闻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 ...
随机推荐
- CodeForces 554B(扫房间)
CodeForces 554B Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u ...
- 省队集训Day3 light
[问题描述] “若是万一琪露诺(俗称 rhl)进行攻击,什么都好,冷静地回答她的问题来吸引她.对方表现出兴趣的话,那就慢慢地反问.在她考虑答案的时候,趁机逃吧.就算是很简单的问题,她一定也答不上来.” ...
- SSH三种框架及表示层、业务层和持久层的理解
Struts(表示层)+Spring(业务层)+Hibernate(持久层) SSH:Struts(表示层)+Spring(业务层)+Hibernate(持久层) Struts:Struts是一个表示 ...
- 为Ubuntu配置ssh服务 方便远程登陆
Ubuntu系统必须开启ssh服务后,XP或者其他的主机才可以远程登陆到Ubuntu系统. 1,安装软件包,执行sudo apt-get install openssh-server Ubuntu缺省 ...
- etErrorMode(SEM_NOGPFAULTERRORBOX); 去除错误对话框.
etErrorMode(SEM_NOGPFAULTERRORBOX); 去除错误对话框. http://www.cnblogs.com/-clq/archive/2012/01/22/2328783 ...
- struts2之防止表单重复提交
struts.xml配置文件 <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts ...
- ArrayList的toArray
ArrayList提供了一个将List转为数组的一个非常方便的方法toArray.toArray有两个重载的方法: 1.list.toArray(); 2.list.toArray(T[] a); ...
- .net ref关键字在引用类型上的使用
只接上干货. namespace ConsoleApplication1 { class Person { public string UserName { get; set; } } class P ...
- 数学:UVAoj 11174 Stand in a Line
Problem J Stand in a Line Input: Standard Input Output: Standard Output All the people in the bytela ...
- 线性代数(矩阵乘法):POJ 2778 DNA Sequence
DNA Sequence Description It's well known that DNA Sequence is a sequence only contains A, C, T and ...