新闻发布会
项目所需要的一些实现类 servlet 工具类
1.实现登录功能
前端界面的代码
<form action="<%=path %>/LonginServlet" method="post">
<label> 登录名 </label>
<input type="text" name="uname" value='<%=request.getParameter("uname")==null?"":request.getParameter("uname") %>' class="login_input" />
<label> 密 码 </label>
<input type="password" name="upwd" value='<%=request.getParameter("upwd")==null?"":request.getParameter("upwd") %>' class="login_input" />
<input type="submit" class="login_sub" value="登录" />
<label id="error"> </label>
<img src="data:images/friend_logo.gif" alt="Google" id="friend_logo" />
</form>
登录实现类代码
public boolean loginGetBool(Admin admin) {
rs= executeSelect("select *from admin where name=? and \"pwd\"=?",admin.getAname(),admin.getApwd());
try {
if(rs.next()){
return true;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return false;
}
登录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(**); response.addCookie(cookie); System.out.println("登陆成功!");
HttpSession session= request.getSession();
session.setAttribute("uname", name);
session.setMaxInactiveInterval(*); response.sendRedirect(request.getContextPath()+"/newspages/admin.jsp");
}else{
response.sendRedirect(request.getContextPath()+"/index.jsp");
}
}
2.实现新增新闻
新增实现类方法
public boolean addNews(News news) {
Date date=new Date();
DateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date time = null;
try {
time = format.parse(format.format(date));
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} Object[] obj={news.getNauthor(),news.getNcontent(),time,null,news.getNtitle(),news.getNtypeid()}; return executeUpdate("INSERT INTO newsrecord (`nauthor`,`ncontent`,`startTime`,`endUpdateTime`,`ntitle`,`ntypeid`) values(?,?,?,?,?,?)",obj); }
新增servlet
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { request.setCharacterEncoding("utf-8");
response.setContentType("text/html; charset=utf-8");
//主题
int ntid=Integer.parseInt(request.getParameter("ntid"));
//标题
String ntitle=request.getParameter("ntitle");
//作者
String nauthor=request.getParameter("nauthor");
//摘要
String nsummary=request.getParameter("nsummary");
//内容
String ncontent=request.getParameter("nauthor");
//上传图片 String file=request.getParameter("file");
NewsWeb news=new NewsWeb(nauthor,ncontent,file,ntitle,ntid,nsummary);
NewsWebDaoImpl nw=new NewsWebDaoImpl(); if(nw.addNewsWeb(news)){
System.out.print("成功!");
request.getSession().setAttribute("xi", "<<script type=\"text/javascript\">+alert(\"新增新闻成功!\") </script>>");
response.sendRedirect(request.getContextPath()+"/newspages/admin.jsp");
// out.print("");
}else{
System.out.print("失败!");
request.getSession().setAttribute("xi", "<<script type=\"text/javascript\">+alert(\"新增新闻失败!\") </script>>");
response.sendRedirect(request.getContextPath()+"/newspages/admin.jsp");
}
新增Servlet
动态显示新闻标题内容
动态显示Servlet
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
TopicDaoImpl dao=new TopicDaoImpl(); List<NewsType> alltopic=dao.getAllTopic(); request.setAttribute("Topiclist", alltopic);
String data=request.getParameter("tid"); if (data!=null&&!data.equals("")) {
int tid=Integer.parseInt(data); NewDaoImpl topicdao=new NewDaoImpl();
List<News> list = topicdao.getNewsById(tid); request.setAttribute("newsList",list); }else {
//处理新闻相关内容
NewDaoImpl newsDao=new NewDaoImpl();
List<News> newsList = newsDao.getTopNews();
request.setAttribute("newsList", newsList);
}
//转向DoIndexServlet获取数据 //准发到index.jsp
request.getRequestDispatcher("/index.jsp").forward(request, response);
}
动态显示实现类
public List<NewsType> getAllTopic() {
Connection connection=getConnection();
String sqlString="select typeid,typename from type";
QueryRunner query=new QueryRunner();
List<NewsType> list=null;
try { list=query.query(connection, sqlString, new BeanListHandler<NewsType>(NewsType.class)); System.out.println(list.get().getTypeName());
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return list;
} public List<News> getTopNews() {
Connection connection=getConnection();
QueryRunner query=new QueryRunner();
//select * from newsrecord where rownum<=3 orcal查询前三条语句
String sqlString="select * from newsrecord where nid limit 3";
List<News> list=null;
try {
list=query.query(connection, sqlString, new BeanListHandler<News>(News.class));
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return list;
}
前端代码
<div class="content">
<ul class="class_date">
<li id='class_month'>
<c:forEach var="item" items="${requestScope.Topiclist}">
<a style="color:red;font-size:14px;" href='${pageContext.request.contextPath }/DoIndexServlet?tid=${item.typeid}'><!-- 从域中取值 -->
${item.typeName}
</a>
</c:forEach>
</li> </ul>
<ul class="classlist">
<c:forEach var="item" items="${newsList }">
<li><a href='newspages/news_read.jsp'>${item.ntitle }</a><span>${item.startTime } </span></li>
</c:forEach> <p align="right"> 当前页数:[1/2] <a href="/DoIndexServlet">下一页</a> <a href="#">末页</a> </p>
</ul>
</div>
添加新闻主题
新增新闻类型Servlet
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { request.setCharacterEncoding("utf-8");
response.setContentType("text/html; charset=utf-8");
String tname=request.getParameter("tname");
NewsType newsType=new NewsType(tname);
NewsTypeDaoImpl ntdi=new NewsTypeDaoImpl();
if (ntdi.addNewsType(newsType)) {
request.getSession().setAttribute("xi", "<<script type=\"text/javascript\">+alert(\"新增新闻类型成功!\") </script>>");
}else{
System.out.println("");
request.getSession().setAttribute("xi", "<<script type=\"text/javascript\">+alert(\"新增新闻类型失败!\") </script>>");
}
response.sendRedirect("/news/util/addnewstype.jsp"); }
新增类型实现类
public boolean addNewsType(NewsType newsType){
return executeUpdate("insert into type(typename)
values(?)", newsType.getTypeName());
}