新闻发布会
项目所需要的一些实现类 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> 密  码 </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 }