Ending session when Logging out in JSP -
following navigation bar include logout button page.
i want end session when user click logout button.
i found sample codes googling , added code following code shows error.(check error in screenshot)
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1"> <ul class="nav navbar-nav navbar-right"> <li class="dropdown"> <a href="#" class="dropdown-toggle" data-toggle="dropdown">hi, <%=session.getvalue("username") %><b class="caret"></b></a> <ul class="dropdown-menu"> <li align="center" class="well"> <a href="index.jsp" class="btn btn-sm btn-default"><span class="glyphicon glyphicon-log-out"></span> logout</a> <% session.invalidate(); response.sendredirect("index.jsp"); %> </li> </ul> </li> </ul> </div><!-- /.navbar-collapse -->
help me correct code..
by default, session created container if session not available. jsp use avoid create new session:
<%@ page session="false" %>
before accessing session
variables check session
if available not. without session, if call session object
methods raise illeagalstateexcetion
.
<% if (session!=null) {%> <%=session.getvalue("username") %><b class="caret"></b></a> <% } %>
the session getting invalidated every time page loads, not clicking on logout
. clicking on logout
put separate page invalidate session
.
here ma using logout.jsp
, contains session invalidation code:
<!-- not creating new session --> <%@ page session="false" %> <body> <% // checking session available or not, if not available throw exception, "session invalidated." if (sesssion!=null) { session.invalidate(); response.sendredirect("index.jsp"); } %> </body>
and change logout
link following:
<a href="logout.jsp" class="btn btn-sm btn-default"><span class="glyphicon glyphicon-log-out"></span> logout</a>
give try :)
Comments
Post a Comment