Hiten Hiten - 2 months ago 25
Java Question

How to redirect jsp page after session expired?

<%
if(session == null) {
System.out.println("Expire");
response.sendRedirect("/login.jsp");
}else{
System.out.println("Not Expire");
}
%>

<%
HttpSession sess = request.getSession(false);
String email = sess.getAttribute("email").toString();
Connection conn = Database.getConnection();
Statement st = conn.createStatement();
String sql = "select * from login where email = '" + email + "' ";
ResultSet rs = st.executeQuery(sql);
%>


I tried to redirect the login.jsp page when session is expired.
But I am geeting error in "String email = sesss.getAttribute("email").toString();".

So anyone please help me to solve this error.

Basically I want to redirect to login.jsp page when the session is expired.

Answer

I tried this and its working fine for me.

<%
    if(session.getAttribute("email") == null) {
       response.sendRedirect("login.jsp");
       return ;
    }
%>

I just put return statement and It will redirect to "login.jsp" when the session is expired.

Comments