为什么session.getAttribute("loginUser")!=null却获取不到用户名
dologin.jsp代码
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; String username =""; String password =""; //request.setCharacterEncoding("utf-8");//防止中文乱码 username = request.getParameter("username"); password = request.getParameter("password"); //如果用户和密码都等于admin,则登录成功 if("admin".equals(username)&&"admin".equals(password)) { session.setAttribute("loginUser", username);//保存登陆的用户名 request.getRequestDispatcher("login_success.jsp").forward(request, response);//服务器内部转发 }else { response.sendRedirect("login_failure.jsp");//请求重定向 } %>
login_success.jsp的如下代码显示session.getAttribute("loginUser")!=null
<div id="box"> <% String loginUser = ""; if(session.getAttribute(loginUser)!=null){ out.print("dsdasa"); loginUser = session.getAttribute("loginUser").toString(); }else{ out.print("4515"); out.print(session.getAttribute("loginUser")); } %> 登陆成功!欢迎<%=loginUser%><br> <%=request.getParameter("username") %> </div>
输出显示else里面的内容,4515admin