JSP page Variable Scope problem

Source: Internet
Author: User
Tags variable scope

Want to implement a logout function, there is a logout button on the page, I think a bit of it is written off, with the JS to the button added the onclick code, as follows

<%
Session = Request.getsession (true);
if (Session.isnew ())
{%>
<script language= "JavaScript" >
Alert ("Login timeout, please login again!");
top.location= ' main.jsp ';

Invalid session, page Jump here, return to the login 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> login </span></a></li>
<%
}
Else
{
%>

<li class= "L1" ><input class= " Button "type=" button "Name=" Logout "value=" Logoff " onclick= "logout ();"/></li> <li class=" L1 "><a href=" login.jsp "target=" MainFrame "><span> switch identities </span></a></li>
<%
System.out.println ("Navigator has been displayed correctly! "+type);
}
%>

Display different pictures according to different users,

<script language= "JavaScript" > in the same file, not an external function

function logout ()
{
Alert ("logout!");
<%system.ou.println (type); Session.invalidate ();%>
top.location= ' main.jsp ';
return false;
}

However, each time, even if you do not click Logout, the type will be displayed once, shown as null, and refresh the page again, user type display error

That means two.

1. Regardless of the location of the JSP script in the page, it will be executed, not because of the JS function triggered or not triggered by the change

2.JS scripts and JSP pages cannot share variables as if they were on different pages, so from JSP to JS simple, from JS to JSP more difficult

So log off or link to another page with a hyperlink.

JSP page Variable Scope problem

Related Article

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.