SSH登陆验证(HQL)

来源:互联网 发布:雷蛇1800鼠标驱动 mac 编辑:程序博客网 时间:2024/05/16 06:08

实现功能:

登陆页面输入:用户名、密码。点击提交,工程根据数据库中userinfo表单中的用户名和密码信息,验证该用户是否存在。存在则返回true登陆成功,否则返回false,登陆失败。

登陆结果出来后,进行页面的跳转,涉及到页面filter过滤功能,前面的博客中已经进行了详细介绍。

涉及文件:

①index.jsp登陆文件,表单提交登陆信息给servlet类

②servlet类,获取信息,调用checkIn(username,psw)函数判断用户信息是否正确,并返回boolean值

③userInfoService类,定义了对userinfo表单进行操作的函数,其中包括checkIn函数。checkIn(username,psw)函数实质上是,根据参数的值,在数据库中寻找符合这两个值的记录,记录保存在一个list中,list的size大小为0说明不存在,否则存在。如果存在返回true,如果不存在返回false。

④userInfoDao类,完成工程和数据库中userinfo表单的映射。这个类中定义一个findByProperty(单个参数),或findByProperties(多个参数),可以根据参数,找到符合条件的记录。寻找的过程,实质是利用HQL语句,类似SQL语句。后面具体代码中可以看到。

一、index.jsp

<html>  <head>    <base href="<%=basePath%>">        <title>首页</title><meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0">    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><!--<link rel="stylesheet" type="text/css" href="styles.css">-->  </head>    <body>     <form name="form" method="post" action="servlet/loginServlet" >     用户名:<input type="text" id="user" name="user">    密码:<input type="text" id="psw" name="psw">    <input type="submit" value="提交" name="tj" id="btn"/>        </form>  </body></html>
jsp文件中有<base href="<%basePath%>">变量,指定该文件的base路径为根路径。那么后面用到的所有的相对路径,都在这个基础上!

②录入信息在表单中,以post方法提交给servlet类进行处理。

二、servlet类

public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {response.setContentType("text/html");response.setCharacterEncoding("UTF-8");String root = request.getContextPath();PrintWriter out = response.getWriter();ServletContext ctx = getServletConfig().getServletContext();BeanFactory factory = (BeanFactory) WebApplicationContextUtils.getWebApplicationContext(ctx); HttpSession session = request.getSession();String username = request.getParameter("user")==null?"":request.getParameter("user");String psw = request.getParameter("psw")==null?"":request.getParameter("psw");userInfoService users = (userInfoService)factory.getBean("userInfoService");boolean flag = users.checkIn(username, psw);if(flag){session.setAttribute("flag", flag);}else{session.setAttribute("flag", flag);}response.sendRedirect(root + "/htgl/ueditor.html");out.println(session);out.flush();out.close();}
①获取jsp参数后,调用users.checkIn函数验证是否存在,这里的返回值为boolean。后面可以利用这个boolean判断跳转

②boolean值的判断方法与String不同

String判断:  if(a.equals("**"))

boolean判断:if(booean)

③response.sendRedirect(root+"/htgl/ueditor.html")为页面重定向,即跳转功能。

root在上面有定义是request.getContextPath();获取了根路径。具体有关路径的介绍也在上一篇博客中进行了详细讲解。

三、userInfoService类

public boolean checkIn(String userInfo, String userPsw){       //返回vo类ArrayList<Userinfo> list = new ArrayList<Userinfo>();List<?> out = null;boolean exist = false;try{out = dao.findByProperties(userInfo, userPsw);for(int i = 0; i < out.size(); i++){list.add((Userinfo)out.get(i));}}catch (Exception e) {e.printStackTrace();}dao.findByProperties(userInfo, userPsw);if(list.size()>0){exist = true;}else exist = false;return exist;}
①checkIn函数的返回值为boolean,有两个参数。标志位exist要首先默认定义为false,boolean的初始默认值要根据实际情况进行定义,但一定要有默认定义!

②调用dao中的fingByProperties找到符合两个参数的记录,放在list中,如果list的大小size不为0,则说明存在符合条件的记录,返回true。

③函数有返回值,最后要return一个符合类型的值。

四、userInfoDao类

public List findByProperty(String propertyName, Object value) {log.debug("finding Userinfo instance with property: " + propertyName+ ", value: " + value);try {String queryString = "from Userinfo as model where model."+ propertyName + "= ?";return getHibernateTemplate().find(queryString, value);} catch (RuntimeException re) {log.error("find by property name failed", re);throw re;}}public List findByProperties(String userName, String userPsw) {log.debug("finding Userinfo by userNmae and userPsw.");try {String queryString = "from Userinfo as model where model.userid = ? and model.upwd = ? " ;return getHibernateTemplate().find(queryString, new String[]{userName,userPsw});} catch (RuntimeException re) {log.error("find by property name failed", re);throw re;}}
①上面有根据一个参数,和两个参数的查询函数。特别注意两个参数的时候,find方法后面两个参数要以数组的形式赋到queryString中。即new String[](username,userpsw)



0 0
原创粉丝点击