时间:2023-09-19 23:24:01 | 来源:网站运营
时间:2023-09-19 23:24:01 来源:网站运营
博客网站的设计与实现://用户登录 public String login2() throws IOException { HttpServletRequest request = this.getRequest(); PrintWriter writer = this.getPrintWriter(); String username = request.getParameter("username"); String password = request.getParameter("password"); User user = userDao.selectBean(" where username = '" + username + "' and password= '" + password + "' and deletestatus=0 and role=2 and status=0 "); if (user != null) { HttpSession session = request.getSession(); session.setAttribute("user", user); writer.print("<script language=javascript>window.location.href='index.action';</script>"); } else { writer.print("<script language=javascript>alert('用户名或者密码错误或者账户已封禁');window.location.href='indexmethod!login.action';</script>"); } return null; }
说明:本段代码是程序的登陆代码,首先从获取从页面输入的用户名、密码,然后调用userDao.selectBean方法,从数据库里取出该条记录进行判断,如果该条记录存在,则说明:用户登陆成功,同时把用户信息存在session里面方便调用,如果记录为空,则跳转到登陆页面,同时提醒用户错误信息。//博文列表 public String articlelist() { HttpServletRequest request = this.getRequest(); String atitle = request.getParameter("atitle"); StringBuffer sb = new StringBuffer(); sb.append(" where "); if (atitle != null && !"".equals(atitle)) { sb.append("atitle like '%" + atitle + "%'"); sb.append(" and "); request.setAttribute("atitle", atitle); } HttpSession session = request.getSession(); User user = (User)session.getAttribute("user"); sb.append(" articledelete=0 and user.id="+user.getId()+" order by id desc "); String where = sb.toString(); int currentpage = 1; int pagesize = 10; if (request.getParameter("pagenum") != null) { currentpage = Integer.parseInt(request.getParameter("pagenum")); } int total = articleDao.selectBeanCount(where.replaceAll("order by id desc", "")); request.setAttribute("list", articleDao.selectBeanList((currentpage - 1) * pagesize, pagesize, where)); request.setAttribute("pagerinfo", Pager.getPagerNormal(total, pagesize, currentpage, "usermethod!articlelist.action", "共有" + total + "条记录")); request.setAttribute("url", "usermethod!articlelist.action"); request.setAttribute("url2", "usermethod!article"); request.setAttribute("title", "博文管理"); this.setUrl("user/article/articlelist.jsp"); return SUCCESS; }
说明:首先通过 String atitle = request.getParameter("atitle ");这句代码从页面获取查询的选项,然后通过if(atitle!=null &&!"".equals(atitle)){通过这句条件语句分别判断下这个值是否为空,当不为空时则通过StingBuffer 来叠加组装查询的sql语句,最后当sql语句组装好了之后,通过articleDao.selectBeanList方法来调用数据库的查询方法来查到符合条件的记录,在通过request.setAttribute方法把查询到的记录传到显示的页面。关键词:设计,实现