`

servlet中判断用户是否登录(禁止访问后台页面)

    博客分类:
  • j2ee
阅读更多

public void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {

   response.setCharacterEncoding("GBK");
   HttpSession session = request.getSession(false);

   if(session.getAttribute("UserName")==null) //如果没登陆,模拟数据!
   {
   session.setAttribute("UserName","username"); //模拟数据,模拟注册用户,然后返回到JS端!
         String script ="function status(){alert('登陆用户名为:"+session.getAttribute("UserName")+"')};";
         response.getWriter().write(script);
   }else
   {
         String script ="function status(){alert('尚未登陆,请登陆');window.location='success.jsp'};";
         response.getWriter().write(script);
   }
}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics