Jsp page variable scope problem, jsp page variable

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.