java之登录验证
首先给大家抱歉,最近比较忙没有更新博客,家里有点喜事;比较忙加上工作原因没有给大家分享;今天给大家分享一段
首先在登陆页面判断是否登录成功,登陆成功了则把登陆成功的信息放如session中,在login界面中加上这一段
if ("login".equals(command)) { String userId = request.getParameter("userId"); String password = request.getParameter("password"); try { User user = UserManager.getInstance().login(userId, password); //登陆成功将用户信息放到session中 session.setAttribute("login_user", user); //设置超时,单位:秒 session.setMaxInactiveInterval(6000); //重定向到主控页面 response.sendRedirect("marcello.jsp"); }catch(Exception e) { //out.println(e.getMessage()); out.println("<font color=\"red\">用户名或密码错误 </font>"); } }
然后在主页中加上
<% Object username = session.getAttribute("login_user"); if(username== null) { //out.println("未登录"); response.sendRedirect("login.jsp"); } else{ 登陆成功要做的操作 } %>
那么登陆成功才可以访问这些界面;那就要巧用这些内容了
比如:权限 在登陆时候获取该用户的权限,有些页面没有这个权限则不给点击等等;这些后续会继续分享;