0

I want to hide Signup and Login buttons on every page after user logs in and show only logout, and after logging out there should be those previous buttons signup and login. I'm using Eclipse

How to disable html controls after a user logins in jsp is the best one I've found but it's not working. Why?

    <ul><li><h1 style="font-size:40px">Car Care</h1></li></ul></a>
  </div>
  </div>
  <ul>
    <li><a href="Home.jsp">Home</a></li>
    <li><a href="Aboutus.jsp">About Us</a></li>
    <li><a href="Services.jsp">Services</a></li>
    <li><a href="Branches.jsp">Locations</a></li>
    <li><a href="#">Promotions</a></li>
    <li><a href="Getintouch.jsp">Get in Touch</a></li>   
    </ul>
  </div>
  </div>
  <div class="navigation-bar1">
  <div id="navigation-container1">
  <ul>
 
    <li><a href="Signup.jsp">Sign Up</a></li>
    <li><a href="Signin.jsp">Already A Member? Sign In</a></li>
  
   </ul>
  </div>
  </div>
  <header>
  <div class="main">
  <div class="backp1">
  <img src="${pageContext.request.contextPath}/img/<%="login.png" %>" 
   name="uimage" id="uimage"/>
   </div>
<div id="tableContainer-1">
<div id="tableContainer-2">

     <form  name="signin" method="post" action="LoginServlet">
            
            <table id="myTable">
            
            <hr>
            <caption><h3>Login</h3></caption>   
            <p text align="center">Sign In With Your Details</p>            
            
            <tr>
                <td>Username</td>
                <td><input type="text"  placeholder="Username" name="Uname" 
              id="suname"  required/></td>
                
            </tr>

            <tr>
                <td>Password</td>
                <td><input type="password" placeholder="Password 
                "name="Password" id="spassword" required/></td>
                
            </tr>

            
            <tr>
                
                <td><button type="reset" class="resetbtn" 
         value="reset">Reset</button>
                <td><button type="submit" class="signinbtn" value="Sign Up" 
              name="submit" id="submit">Login</button></td>
                
            </tr>
            </table>
        </form>
        </div>
        </div>

            </tr>
        </table>

This is my list and sign in.

Nimantha
  • 6,405
  • 6
  • 28
  • 69
Shady
  • 96
  • 1
  • 9

1 Answers1

0

After the user has signed in you can store this in a session scope variable

session.setAttribute("Username", Username);

On every page just check if this attribute exists

String Username = session.getAttribute("Username");
if (Username==null) { // show the signin and register buttons
Rey333
  • 350
  • 2
  • 12
  • It's still not working , oh and I'm already using sessions in the servlet with these two codes "String username = request.getParameter("Uname"); session.setAttribute("sessname",username); " maybe I'm doing something wrong – Shady Jun 11 '18 at 18:49
  • 1
    Strange! Sure that Its not a typo (username instead of Username)? Its like the session is invalidated quickly or so.. Have you tried System.out.println(session.getAttribute("Username")); here and there to see if the session is saved. May be Session Persistence is disabled? What container are you using? – Rey333 Jun 11 '18 at 19:17
  • okay I figured it out. I had to do some little changes. Both codes were right(the link in my post). session was already in my servlet and all I had to do was `<%String Username = (String) session.getAttribute("sessname");` cuz in my login servlet I've used `session.setAttribute( "sessname" ,username);` That's All :) – Shady Jun 11 '18 at 20:10
  • 1
    Naming a variable sessname instead of what it actual is is asking for problems and delays like this... That's why I said to check for typos what probably let you to checking the name.. :D – Rey333 Jun 12 '18 at 06:41
  • Yeah Thank You!. I know it's been 11 months. but if you could update the answer with including the existing Answer , I could mark it. :D. – Shady May 24 '19 at 16:15