lundi 21 août 2017

responnse.setRedirect("index.jsp") not working when placed in header.jsp

I am trying to redirect the user to the main page if session object is null. I am successful when I place my code in my normal jsp page eg. dashboard.jsp but if I remove code from dashboard.jsp and add it to header.jsp, it won't work.

Here is header.jsp

<!DOCTYPE html>
<html lang="en">

    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

        <meta name="description" content="">
        <meta name="author" content="">
        <title>Dashboard</title>

        <!-- Bootstrap core CSS -->
        <link href="vendor/bootstrap/css/bootstrap.min.css" rel="stylesheet">

        <!-- Custom fonts for this template -->
        <link href="vendor/font-awesome/css/font-awesome.min.css" rel="stylesheet" type="text/css">

        <!-- Plugin CSS -->
        <link href="vendor/datatables/dataTables.bootstrap4.css" rel="stylesheet">

        <!-- Custom styles for this template -->
        <link href="css/sb-admin.css" rel="stylesheet">

    </head>
    <body class="fixed-nav" id="page-top">
        <%
            //add header to not cache the page as well as revalidate eveytime before providing access
            response.setHeader("Cache-Control", "no-cache,no-store,must-revalidate");
            //check if logged in, else redirect to login page    
            if (session.getAttribute("username") == null) {

                response.sendRedirect("index.jsp?login=false");
                return;
            }
        %>

Dashboard.jsp

<jsp:include page="/WEB-INF/partials/header.jsp" />

<jsp:include page="/WEB-INF/partials/navigation.jsp" />

//body content here

<jsp:include page="/WEB-INF/partials/footer.jsp" /> 

When the code

  <%
            //add header to not cache the page as well as revalidate eveytime before providing access
            response.setHeader("Cache-Control", "no-cache,no-store,must-revalidate");
            //check if logged in, else redirect to login page    
            if (session.getAttribute("username") == null) {

                response.sendRedirect("index.jsp?login=false");
                return;
            }
        %>

is added to dashboard.jsp the page is redirected successfully but not when added to header.jsp. Why is it so?




Aucun commentaire:

Enregistrer un commentaire