Jsp 清除会话的参数

Jsp 清除会话的参数,jsp,session,Jsp,Session,我有一个登录页面,在那里输入用户名和密码。我在servlet中使用了setAttribute()来设置用户名,在jsp页面中使用了session.getAttribute()来检索用户名并打印欢迎消息 这是我的一页代码片段: `<h2>Welcome <%=request.getParameter("uname")%>! Enter the numbers and the operation that you want to perform: </h2>

我有一个登录页面,在那里输入用户名和密码。我在servlet中使用了
setAttribute()
来设置用户名,在jsp页面中使用了
session.getAttribute()
来检索用户名并打印欢迎消息

这是我的一页代码片段:

`<h2>Welcome <%=request.getParameter("uname")%>! Enter the numbers and the operation 
   that you want to perform: </h2>
   <% session.setAttribute("uname",request.getParameter("uname")); %>`
欢迎!输入数字和操作 您要执行的操作: ` 这是我的另一个jsp页面:

`<h2>Welcome back <%=session.getAttribute("uname")%>! Enter the numbers and 
       the operation that you want to perform: </h2>`
欢迎回来!输入数字和 要执行的操作:` 这样,即使我尝试注销并再次登录,它也会以与前一个用户相同的名称表示欢迎,或者以null表示欢迎。
如何清除会话属性或会话本身,以便每次新用户登录时都会显示Welcome*该用户*,如果同一用户再次登录,则会显示Welcome back

session.removeAttribute(“attributeName”)
将从会话中删除该属性,而
session.invalidate()将使会话无效。

无需
session.removeAttribute()

您只需调用
session.invalidate()
,这将删除属性和会话

哪个位置是删除属性或使会话无效的合适位置?当您注销时,您可以使会话无效。清除/删除属性取决于您的逻辑。如果只有在注销时才需要删除它,请将其包含在您的注销代码中。