您好我正在开发一个Web应用程序..在登录时,用户将被重定向到他所尊重的登录页面,欢迎文本为WElCOME用户名..但是当用户导航到其他页面并返回时,它显示欢迎msg as null ...即使导航到不同的页面后,如何在主页上保持用户名不变?
我在JSp上使用此代码来显示Welcome msg:
String un = request.getParameter("txtUsername");
out.println("Welcome " + un);
我的LoginServlet是这样的:
String username = request.getParameter("txtUsername");
String category = (request.getParameter("txtCategory"));
Login login = new Login();
login.setUserName(username);
login.setPassWord(request.getParameter("txtPassword"));
login.setCategory(category);
LoginService ls = new LoginService();
ls.loginValidate(login);
Boolean check = ls.loginValidate(login);
if (check == true) {
HttpSession session = request.getSession();
// setting attribute on session
session.setAttribute("user", username);
if (category != null) {
if (category.equalsIgnoreCase("Admin")) {
RequestDispatcher rd = request
.getRequestDispatcher("WEB-INF/WebPages/Admin.jsp");
rd.forward(request, response);
} else if (category.equalsIgnoreCase("Affiliate")) {
RequestDispatcher rd = request
.getRequestDispatcher("WEB-INF/WebPages/Affiliate.jsp");
rd.forward(request, response);
} else {
RequestDispatcher rd = request
.getRequestDispatcher("WEB-INF/WebPages/Client.jsp");
rd.forward(request, response);
}
}
}
else {
RequestDispatcher rd = request
.getRequestDispatcher("WEB-INF/WebPages/Error.jsp");
rd.forward(request, response);
}
}
请帮我解决这个问题..提前致谢....
答案 0 :(得分:2)
当您将值放入会话属性时,您需要从session
而非request
<c:out value="${sessionScope.user}"/>
或
<% request.getSession().getAttribute("user") %>
确保JSP是允许访问会话。
<%@ page session="true" %>