Jsp page variable scope problem, jsp page variable
To implement the logout function, there is a Logout button on the page. I want to unregister it with a single click. I used js to add the onclick code to the button, as shown below:
<%
Session = request. getSession (true );
If (session. isNew ())
{%>
<Script LANGUAGE = "JavaScript">
// Alert ("Logon times out. Please log on again! ");
Top. location = 'main. jsp ';
// The session is invalid. Go to the page and return to the logon page.
</Script>
<%
}
String type = (String) session. getAttribute ("type ");
String show = null;
System. out. println ("navigator user type:" + type );
If (type = null)
{
%>
<Li class = "L1"> <a href = "register. jsp "target =" mainFrame "> <span> Registration </ span> </a> </li>
<Li class = "L1"> <a href = "login. jsp "target =" mainFrame "> <span> logon </ span> </a> </li>
<%
}
Else
{
%>
<Li class = "L1"> <input class = "button "type =" button "name =" logout "value =""
Onclick = "logout ();"/> </Li> <li class =" L1 "> <a href =" login. jsp "target =" mainFrame "> <span> switch identities </span> </a> </li>
<%
System. out. println ("navigator is correctly displayed! "+ Type );
}
%>
Different images are displayed based on different users,
<Script language = "JavaScript"> In the same file, it is not an external function.
Function logout ()
{
Alert ("logout! ");
<% System. ou. println (type); session. invalidate (); %>
Top. location = 'main. jsp ';
Return false;
}
However, even if you do not click logout, the type will be displayed once, displayed as null, and the page is refreshed again. The user type is displayed incorrectly.
Two points
1. No matter where the JSP script is located on the page, it will be executed and will not change because of the trigger or no trigger of JS functions.
2. JS scripts and JSp pages cannot share variables, as if they are on different pages. Therefore, it is easy to use JSp to js, and it is difficult to share variables from js to JSp.
So you can log out and use a hyperlink to connect to another page.