-1

Here's the situation.. 1. I got a home page with options of Login | Register | Contact | About me in jsp.. 2. Its basically a online shopping website.. 3. When the user visits the website.. the Login | Register | should be visible, but when a user logs in with his user id and pass his Username and logout button should replace the Login and Register links..

for example.. Common Home Page : Login | Register | Contact | About me

Logged in Home Page : Welcome abcde | Logout | Contact | About me

i am not getting the logic of it... if sme 1 can demonstrate me it will be of great help.

  • if you using normal html link for the options as shown in the question, then use if else using scriptlet or with jstl – gjman2 Oct 24 '13 at 05:47

1 Answers1

1

You can do this in following way..

 <body>
        <ul id="nav">
            <li><a href="/home.jsp">Home</a></li>
            <li><a href="/aboutus.jsp">About</a></li>
            <li><a href="/contactus.jsp">Contact</a></li>
            <%
                String username= (String) session.getAttribute("user");                     
                if (username == null) {
            %>
            <li><a href="/register.jsp">Register</a></li>
            <li><a href="/login.jsp">Login</a></li>

        <% } else {
         %>
            <li>Hi, <%=username %>  (<a href="/logout.jsp">Logout</a>)</li>
        <% }%>
        </ul>
    </body>

and in your servlet put this code

  HttpSession session = request.getSession(true);
  String user = request.getParameter("username");
  session.setAttribute("user", name);
Developer Desk
  • 2,294
  • 8
  • 36
  • 77
  • also dude if i click logout, the same common home page should be displayed.. for that...? – Siddhartha Pandey Oct 24 '13 at 10:05
  • same common page will be displayed except it will have login and register link on navigation bar.. – Developer Desk Oct 24 '13 at 10:14
  • yah...? coz if i give the link of homepage.jsp.. it will give me a page with the "Welcome abcde | Logout | Contact | About me" and i want it to be like this "Login | Register | Contact | About me" after LOGOUT button is clicked – Siddhartha Pandey Oct 24 '13 at 10:33
  • you need to check whether user session exists or not . i suggest make above navigationbar as header to each page and whithin each page add header i.e <%@include file="header.jsp" %> – Developer Desk Oct 24 '13 at 10:37
  • u mean to say i gotcha request.getSession(false)? – Siddhartha Pandey Oct 24 '13 at 10:41
  • yes logout user when he will click on logout link session.invalidate(); see this http://stackoverflow.com/questions/14445024/how-to-validate-invalidate-sessions-jsp-servlets – Developer Desk Oct 24 '13 at 10:44
  • thanks for ur help really appreciate... :) for that header idea.... i know it might be easy for u bt... it took a lot of time from me earlier coz i was copy pasting the code to every jsp page.. it would be a lot useful ahead.. – Siddhartha Pandey Oct 24 '13 at 11:44
  • Use **username.equals("null")** to compare strings. – Jananath Banuka May 04 '18 at 15:37