How to disable html controls after a user logins in jsp

荒凉一梦 提交于 2019-12-22 01:09:55

问题


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.


回答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);


来源:https://stackoverflow.com/questions/19557874/how-to-disable-html-controls-after-a-user-logins-in-jsp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!