问题
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