在主页中,当我们导航到另一个页面并返回主页时,返回用户名时出错

时间:2014-03-20 06:55:25

标签: java jsp java-ee java-ee-6 java-ee-7

您好我正在开发一个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);
        }
    }

请帮我解决这个问题..提前致谢....

1 个答案:

答案 0 :(得分:2)

当您将值放入会话属性时,您需要从session而非request

获取该值
<c:out value="${sessionScope.user}"/>

<% request.getSession().getAttribute("user") %>

确保JSP是允许访问会话。

<%@ page session="true" %>