JSP通过session判断用户是否登录:
登录的时候把当前用户存到session中:
User user = request.getParameter("User");
session.setAttribute("User",user);
JSP中判断session是否为空,如果为空则跳转到登录页面:
<% User user=(User)session.getAttribute("User");
if(user!=null){%>
显示内容
<%}else{ %>
<%response.sendRedirect("login.jsp"); %>
<%}%>
后台从session中获取登录的用户:
User user = session.getAttribute("User");
if(user==null){
out.println("未登录");
}else{
out.println("已登录");
}