In Home Page when we navigate to another page and come back to home there is an error returning the username

StackOverflow https://stackoverflow.com/questions/22525617

Question

Hello i am developing a web Application.. Where on login the user will be redirected to his respected login page with a welcome text as WElCOME Username.. But when the user navigates to some other page and comes back it is displaying welcome msg as null... How to keep the username constant on the homepage even after navigation to different pages??

I use this code on JSp to display Welcome msg:

String un = request.getParameter("txtUsername");
out.println("Welcome    " + un);

An d my LoginServlet is this:

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);
        }
    }

Please help me fix this.. Thanks in advance....

Était-ce utile?

La solution

As you put the value in a session attribute, you need to get it from the session not the request

<c:out value="${sessionScope.user}"/>

or

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

Make sure the JSP is allow access session.

<%@ page session="true" %>
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top