0

I need to try and read a value from one JSP file in another. I came across a forum while searching or an answer and found that if I declare a variable in one file and include that JSP file in the new one. So, that's what I tried but am getting a null value on my second JSP file.

JSP1:

  <% String value =(String)request.getAttribute("loggedIn"); %>

"loggedIn" is a value returned from a java servlet and when i use the variable value in this page it outputs the correct result.

JSP2:

<%@include file="JSP1.jsp"%>

<div id="navbar">
                <div id="logo">
                <img SRC="logo.png" ALT="Unable to load image" WIDTH=150 HEIGHT=90>
            </div>
                <div id="navbar-right">
                    <a style="margin-right:20px;border-radius: 0px;background-color:#c1d6a7"><%=value%></a>
                    <a class="active" href="Login.jsp">Logout</a> 
                    <a href="ViewData.jsp">View Data</a>
                </div>
            </div>

using this method, the <%=value%> gives a null value.

Charlie Ansell
  • 451
  • 1
  • 7
  • 22

1 Answers1

0

My JSP is a bit old, but are you doing any transformation in the JSP1? If not, why don't you just redeclare String value =(String)request.getAttribute("loggedIn"); in JSP2?

Also if I remember there are multiple flavors of include. You want the one that add the JSP1 code to JSP2

  • Hi Stephen, I cant redeckare the variable as it is picking up a response from a servlet that is only delivering that response to Jsp1. Which is why I'm trying to access it through JSP2 because I need that certain value in that page also – Charlie Ansell Aug 10 '19 at 00:18
  • Not sure I follow you. What is the request flow. Servlet -> JSP1 -> JSP2 is what I assumed and wanted clarified. It looks like it's not that. Can you clarify? – Stephan Bourges Aug 10 '19 at 18:00
  • The flow is that JSP1 has a form to login the user, when the user fills out that form the details are sent to a servlet which does its processing then returns request and response info that is displayed in the page JSP1. This data is then a variable called value. I need access to that variable value in JSP2 to display it there aswel. Hope this clarified my logic – Charlie Ansell Aug 11 '19 at 02:00
  • Put into a variable called value* – Charlie Ansell Aug 11 '19 at 02:01